|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of Composer. |
| 5 | + * |
| 6 | + * (c) Nils Adermann <[email protected]> |
| 7 | + * Jordi Boggiano <[email protected]> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Composer\Autoload; |
| 14 | + |
| 15 | +/** |
| 16 | + * ClassLoader implements a PSR-0 class loader |
| 17 | + * |
| 18 | + * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md |
| 19 | + * |
| 20 | + * $loader = new \Composer\Autoload\ClassLoader(); |
| 21 | + * |
| 22 | + * // register classes with namespaces |
| 23 | + * $loader->add('Symfony\Component', __DIR__.'/component'); |
| 24 | + * $loader->add('Symfony', __DIR__.'/framework'); |
| 25 | + * |
| 26 | + * // activate the autoloader |
| 27 | + * $loader->register(); |
| 28 | + * |
| 29 | + * // to enable searching the include path (eg. for PEAR packages) |
| 30 | + * $loader->setUseIncludePath(true); |
| 31 | + * |
| 32 | + * In this example, if you try to use a class in the Symfony\Component |
| 33 | + * namespace or one of its children (Symfony\Component\Console for instance), |
| 34 | + * the autoloader will first look for the class under the component/ |
| 35 | + * directory, and it will then fallback to the framework/ directory if not |
| 36 | + * found before giving up. |
| 37 | + * |
| 38 | + * This class is loosely based on the Symfony UniversalClassLoader. |
| 39 | + * |
| 40 | + * @author Fabien Potencier <[email protected]> |
| 41 | + * @author Jordi Boggiano <[email protected]> |
| 42 | + */ |
| 43 | +class ClassLoader |
| 44 | +{ |
| 45 | + private $prefixes = array(); |
| 46 | + private $fallbackDirs = array(); |
| 47 | + private $useIncludePath = false; |
| 48 | + private $classMap = array(); |
| 49 | + |
| 50 | + public function getPrefixes() |
| 51 | + { |
| 52 | + return $this->prefixes; |
| 53 | + } |
| 54 | + |
| 55 | + public function getFallbackDirs() |
| 56 | + { |
| 57 | + return $this->fallbackDirs; |
| 58 | + } |
| 59 | + |
| 60 | + public function getClassMap() |
| 61 | + { |
| 62 | + return $this->classMap; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param array $classMap Class to filename map |
| 67 | + */ |
| 68 | + public function addClassMap(array $classMap) |
| 69 | + { |
| 70 | + if ($this->classMap) { |
| 71 | + $this->classMap = array_merge($this->classMap, $classMap); |
| 72 | + } else { |
| 73 | + $this->classMap = $classMap; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Registers a set of classes |
| 79 | + * |
| 80 | + * @param string $prefix The classes prefix |
| 81 | + * @param array|string $paths The location(s) of the classes |
| 82 | + */ |
| 83 | + public function add($prefix, $paths) |
| 84 | + { |
| 85 | + if (!$prefix) { |
| 86 | + foreach ((array) $paths as $path) { |
| 87 | + $this->fallbackDirs[] = $path; |
| 88 | + } |
| 89 | + |
| 90 | + return; |
| 91 | + } |
| 92 | + if (isset($this->prefixes[$prefix])) { |
| 93 | + $this->prefixes[$prefix] = array_merge( |
| 94 | + $this->prefixes[$prefix], |
| 95 | + (array) $paths |
| 96 | + ); |
| 97 | + } else { |
| 98 | + $this->prefixes[$prefix] = (array) $paths; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Turns on searching the include path for class files. |
| 104 | + * |
| 105 | + * @param bool $useIncludePath |
| 106 | + */ |
| 107 | + public function setUseIncludePath($useIncludePath) |
| 108 | + { |
| 109 | + $this->useIncludePath = $useIncludePath; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Can be used to check if the autoloader uses the include path to check |
| 114 | + * for classes. |
| 115 | + * |
| 116 | + * @return bool |
| 117 | + */ |
| 118 | + public function getUseIncludePath() |
| 119 | + { |
| 120 | + return $this->useIncludePath; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Registers this instance as an autoloader. |
| 125 | + * |
| 126 | + * @param bool $prepend Whether to prepend the autoloader or not |
| 127 | + */ |
| 128 | + public function register($prepend = false) |
| 129 | + { |
| 130 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Unregisters this instance as an autoloader. |
| 135 | + */ |
| 136 | + public function unregister() |
| 137 | + { |
| 138 | + spl_autoload_unregister(array($this, 'loadClass')); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Loads the given class or interface. |
| 143 | + * |
| 144 | + * @param string $class The name of the class |
| 145 | + * @return bool|null True, if loaded |
| 146 | + */ |
| 147 | + public function loadClass($class) |
| 148 | + { |
| 149 | + if ($file = $this->findFile($class)) { |
| 150 | + include $file; |
| 151 | + |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Finds the path to the file where the class is defined. |
| 158 | + * |
| 159 | + * @param string $class The name of the class |
| 160 | + * |
| 161 | + * @return string|null The path, if found |
| 162 | + */ |
| 163 | + public function findFile($class) |
| 164 | + { |
| 165 | + if (isset($this->classMap[$class])) { |
| 166 | + return $this->classMap[$class]; |
| 167 | + } |
| 168 | + |
| 169 | + if ('\\' == $class[0]) { |
| 170 | + $class = substr($class, 1); |
| 171 | + } |
| 172 | + |
| 173 | + if (false !== $pos = strrpos($class, '\\')) { |
| 174 | + // namespaced class name |
| 175 | + $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; |
| 176 | + $className = substr($class, $pos + 1); |
| 177 | + } else { |
| 178 | + // PEAR-like class name |
| 179 | + $classPath = null; |
| 180 | + $className = $class; |
| 181 | + } |
| 182 | + |
| 183 | + $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; |
| 184 | + |
| 185 | + foreach ($this->prefixes as $prefix => $dirs) { |
| 186 | + if (0 === strpos($class, $prefix)) { |
| 187 | + foreach ($dirs as $dir) { |
| 188 | + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { |
| 189 | + return $dir . DIRECTORY_SEPARATOR . $classPath; |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + foreach ($this->fallbackDirs as $dir) { |
| 196 | + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { |
| 197 | + return $dir . DIRECTORY_SEPARATOR . $classPath; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { |
| 202 | + return $file; |
| 203 | + } |
| 204 | + |
| 205 | + $this->classMap[$class] = false; |
| 206 | + } |
| 207 | +} |
0 commit comments