| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Mustache.php. |
| 5 |
* |
| 6 |
* (c) 2010-2014 Justin Hileman |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Mustache class autoloader. |
| 14 |
*/ |
| 15 |
class Mustache_Autoloader |
| 16 |
{ |
| 17 |
private $baseDir; |
| 18 |
|
| 19 |
/** |
| 20 |
* Autoloader constructor. |
| 21 |
* |
| 22 |
* @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..') |
| 23 |
*/ |
| 24 |
public function __construct($baseDir = null) |
| 25 |
{ |
| 26 |
if ($baseDir === null) { |
| 27 |
$baseDir = dirname(__FILE__).'/..'; |
| 28 |
} |
| 29 |
|
| 30 |
// realpath doesn't always work, for example, with stream URIs |
| 31 |
$realDir = realpath($baseDir); |
| 32 |
if (is_dir($realDir)) { |
| 33 |
$this->baseDir = $realDir; |
| 34 |
} else { |
| 35 |
$this->baseDir = $baseDir; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Register a new instance as an SPL autoloader. |
| 41 |
* |
| 42 |
* @param string $baseDir Mustache library base directory (default: dirname(__FILE__).'/..') |
| 43 |
* |
| 44 |
* @return Mustache_Autoloader Registered Autoloader instance |
| 45 |
*/ |
| 46 |
public static function register($baseDir = null) |
| 47 |
{ |
| 48 |
$loader = new self($baseDir); |
| 49 |
spl_autoload_register(array($loader, 'autoload')); |
| 50 |
|
| 51 |
return $loader; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Autoload Mustache classes. |
| 56 |
* |
| 57 |
* @param string $class |
| 58 |
*/ |
| 59 |
public function autoload($class) |
| 60 |
{ |
| 61 |
if ($class[0] === '\\') { |
| 62 |
$class = substr($class, 1); |
| 63 |
} |
| 64 |
|
| 65 |
if (strpos($class, 'Mustache') !== 0) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
$file = sprintf('%s/%s.php', $this->baseDir, str_replace('_', '/', $class)); |
| 70 |
if (is_file($file)) { |
| 71 |
require $file; |
| 72 |
} |
| 73 |
} |
| 74 |
} |
| 75 |
|