| 1 |
<?php |
| 2 |
|
| 3 |
namespace Box\Spout\Autoloader; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Psr4Autoloader |
| 7 |
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md#class-example |
| 8 |
* |
| 9 |
* @package Box\Spout\Autoloader |
| 10 |
*/ |
| 11 |
class Psr4Autoloader |
| 12 |
{ |
| 13 |
/** |
| 14 |
* An associative array where the key is a namespace prefix and the value |
| 15 |
* is an array of base directories for classes in that namespace. |
| 16 |
* |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
protected $prefixes = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Register loader with SPL autoloader stack. |
| 23 |
* |
| 24 |
* @return void |
| 25 |
*/ |
| 26 |
public function register() |
| 27 |
{ |
| 28 |
spl_autoload_register(array($this, 'loadClass')); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Adds a base directory for a namespace prefix. |
| 33 |
* |
| 34 |
* @param string $prefix The namespace prefix. |
| 35 |
* @param string $baseDir A base directory for class files in the |
| 36 |
* namespace. |
| 37 |
* @param bool $prepend If true, prepend the base directory to the stack |
| 38 |
* instead of appending it; this causes it to be searched first rather |
| 39 |
* than last. |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
public function addNamespace($prefix, $baseDir, $prepend = false) |
| 43 |
{ |
| 44 |
// normalize namespace prefix |
| 45 |
$prefix = trim($prefix, '\\') . '\\'; |
| 46 |
|
| 47 |
// normalize the base directory with a trailing separator |
| 48 |
$baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR) . '/'; |
| 49 |
|
| 50 |
// initialize the namespace prefix array |
| 51 |
if (isset($this->prefixes[$prefix]) === false) { |
| 52 |
$this->prefixes[$prefix] = array(); |
| 53 |
} |
| 54 |
|
| 55 |
// retain the base directory for the namespace prefix |
| 56 |
if ($prepend) { |
| 57 |
array_unshift($this->prefixes[$prefix], $baseDir); |
| 58 |
} else { |
| 59 |
array_push($this->prefixes[$prefix], $baseDir); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Loads the class file for a given class name. |
| 65 |
* |
| 66 |
* @param string $class The fully-qualified class name. |
| 67 |
* @return mixed The mapped file name on success, or boolean false on |
| 68 |
* failure. |
| 69 |
*/ |
| 70 |
public function loadClass($class) |
| 71 |
{ |
| 72 |
// the current namespace prefix |
| 73 |
$prefix = $class; |
| 74 |
|
| 75 |
// work backwards through the namespace names of the fully-qualified |
| 76 |
// class name to find a mapped file name |
| 77 |
while (false !== $pos = strrpos($prefix, '\\')) { |
| 78 |
|
| 79 |
// retain the trailing namespace separator in the prefix |
| 80 |
$prefix = substr($class, 0, $pos + 1); |
| 81 |
|
| 82 |
// the rest is the relative class name |
| 83 |
$relativeClass = substr($class, $pos + 1); |
| 84 |
|
| 85 |
// try to load a mapped file for the prefix and relative class |
| 86 |
$mappedFile = $this->loadMappedFile($prefix, $relativeClass); |
| 87 |
if ($mappedFile !== false) { |
| 88 |
return $mappedFile; |
| 89 |
} |
| 90 |
|
| 91 |
// remove the trailing namespace separator for the next iteration |
| 92 |
// of strrpos() |
| 93 |
$prefix = rtrim($prefix, '\\'); |
| 94 |
} |
| 95 |
|
| 96 |
// never found a mapped file |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Load the mapped file for a namespace prefix and relative class. |
| 102 |
* |
| 103 |
* @param string $prefix The namespace prefix. |
| 104 |
* @param string $relativeClass The relative class name. |
| 105 |
* @return mixed Boolean false if no mapped file can be loaded, or the |
| 106 |
* name of the mapped file that was loaded. |
| 107 |
*/ |
| 108 |
protected function loadMappedFile($prefix, $relativeClass) |
| 109 |
{ |
| 110 |
// are there any base directories for this namespace prefix? |
| 111 |
if (isset($this->prefixes[$prefix]) === false) { |
| 112 |
return false; |
| 113 |
} |
| 114 |
|
| 115 |
// look through base directories for this namespace prefix |
| 116 |
foreach ($this->prefixes[$prefix] as $baseDir) { |
| 117 |
|
| 118 |
// replace the namespace prefix with the base directory, |
| 119 |
// replace namespace separators with directory separators |
| 120 |
// in the relative class name, append with .php |
| 121 |
$file = $baseDir |
| 122 |
. str_replace('\\', '/', $relativeClass) |
| 123 |
. '.php'; |
| 124 |
|
| 125 |
// if the mapped file exists, require it |
| 126 |
if ($this->requireFile($file)) { |
| 127 |
// yes, we're done |
| 128 |
return $file; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// never found it |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* If a file exists, require it from the file system. |
| 138 |
* |
| 139 |
* @param string $file The file to require. |
| 140 |
* @return bool True if the file exists, false if not. |
| 141 |
*/ |
| 142 |
protected function requireFile($file) |
| 143 |
{ |
| 144 |
if (file_exists($file)) { |
| 145 |
require $file; |
| 146 |
return true; |
| 147 |
} |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|