ex
Fork of mbed-os-example-mbed5-blinky by
dumi_doc-master/schema/vendor/composer/ClassLoader.php@44:c1d8923072ba, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:27:22 2017 +0800
- Revision:
- 44:c1d8923072ba
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 44:c1d8923072ba | 1 | <?php |
| TMBOY | 44:c1d8923072ba | 2 | |
| TMBOY | 44:c1d8923072ba | 3 | /* |
| TMBOY | 44:c1d8923072ba | 4 | * This file is part of Composer. |
| TMBOY | 44:c1d8923072ba | 5 | * |
| TMBOY | 44:c1d8923072ba | 6 | * (c) Nils Adermann <naderman@naderman.de> |
| TMBOY | 44:c1d8923072ba | 7 | * Jordi Boggiano <j.boggiano@seld.be> |
| TMBOY | 44:c1d8923072ba | 8 | * |
| TMBOY | 44:c1d8923072ba | 9 | * For the full copyright and license information, please view the LICENSE |
| TMBOY | 44:c1d8923072ba | 10 | * file that was distributed with this source code. |
| TMBOY | 44:c1d8923072ba | 11 | */ |
| TMBOY | 44:c1d8923072ba | 12 | |
| TMBOY | 44:c1d8923072ba | 13 | namespace Composer\Autoload; |
| TMBOY | 44:c1d8923072ba | 14 | |
| TMBOY | 44:c1d8923072ba | 15 | /** |
| TMBOY | 44:c1d8923072ba | 16 | * ClassLoader implements a PSR-0, PSR-4 and classmap class loader. |
| TMBOY | 44:c1d8923072ba | 17 | * |
| TMBOY | 44:c1d8923072ba | 18 | * $loader = new \Composer\Autoload\ClassLoader(); |
| TMBOY | 44:c1d8923072ba | 19 | * |
| TMBOY | 44:c1d8923072ba | 20 | * // register classes with namespaces |
| TMBOY | 44:c1d8923072ba | 21 | * $loader->add('Symfony\Component', __DIR__.'/component'); |
| TMBOY | 44:c1d8923072ba | 22 | * $loader->add('Symfony', __DIR__.'/framework'); |
| TMBOY | 44:c1d8923072ba | 23 | * |
| TMBOY | 44:c1d8923072ba | 24 | * // activate the autoloader |
| TMBOY | 44:c1d8923072ba | 25 | * $loader->register(); |
| TMBOY | 44:c1d8923072ba | 26 | * |
| TMBOY | 44:c1d8923072ba | 27 | * // to enable searching the include path (eg. for PEAR packages) |
| TMBOY | 44:c1d8923072ba | 28 | * $loader->setUseIncludePath(true); |
| TMBOY | 44:c1d8923072ba | 29 | * |
| TMBOY | 44:c1d8923072ba | 30 | * In this example, if you try to use a class in the Symfony\Component |
| TMBOY | 44:c1d8923072ba | 31 | * namespace or one of its children (Symfony\Component\Console for instance), |
| TMBOY | 44:c1d8923072ba | 32 | * the autoloader will first look for the class under the component/ |
| TMBOY | 44:c1d8923072ba | 33 | * directory, and it will then fallback to the framework/ directory if not |
| TMBOY | 44:c1d8923072ba | 34 | * found before giving up. |
| TMBOY | 44:c1d8923072ba | 35 | * |
| TMBOY | 44:c1d8923072ba | 36 | * This class is loosely based on the Symfony UniversalClassLoader. |
| TMBOY | 44:c1d8923072ba | 37 | * |
| TMBOY | 44:c1d8923072ba | 38 | * @author Fabien Potencier <fabien@symfony.com> |
| TMBOY | 44:c1d8923072ba | 39 | * @author Jordi Boggiano <j.boggiano@seld.be> |
| TMBOY | 44:c1d8923072ba | 40 | * @see http://www.php-fig.org/psr/psr-0/ |
| TMBOY | 44:c1d8923072ba | 41 | * @see http://www.php-fig.org/psr/psr-4/ |
| TMBOY | 44:c1d8923072ba | 42 | */ |
| TMBOY | 44:c1d8923072ba | 43 | class ClassLoader |
| TMBOY | 44:c1d8923072ba | 44 | { |
| TMBOY | 44:c1d8923072ba | 45 | // PSR-4 |
| TMBOY | 44:c1d8923072ba | 46 | private $prefixLengthsPsr4 = array(); |
| TMBOY | 44:c1d8923072ba | 47 | private $prefixDirsPsr4 = array(); |
| TMBOY | 44:c1d8923072ba | 48 | private $fallbackDirsPsr4 = array(); |
| TMBOY | 44:c1d8923072ba | 49 | |
| TMBOY | 44:c1d8923072ba | 50 | // PSR-0 |
| TMBOY | 44:c1d8923072ba | 51 | private $prefixesPsr0 = array(); |
| TMBOY | 44:c1d8923072ba | 52 | private $fallbackDirsPsr0 = array(); |
| TMBOY | 44:c1d8923072ba | 53 | |
| TMBOY | 44:c1d8923072ba | 54 | private $useIncludePath = false; |
| TMBOY | 44:c1d8923072ba | 55 | private $classMap = array(); |
| TMBOY | 44:c1d8923072ba | 56 | private $classMapAuthoritative = false; |
| TMBOY | 44:c1d8923072ba | 57 | private $missingClasses = array(); |
| TMBOY | 44:c1d8923072ba | 58 | |
| TMBOY | 44:c1d8923072ba | 59 | public function getPrefixes() |
| TMBOY | 44:c1d8923072ba | 60 | { |
| TMBOY | 44:c1d8923072ba | 61 | if (!empty($this->prefixesPsr0)) { |
| TMBOY | 44:c1d8923072ba | 62 | return call_user_func_array('array_merge', $this->prefixesPsr0); |
| TMBOY | 44:c1d8923072ba | 63 | } |
| TMBOY | 44:c1d8923072ba | 64 | |
| TMBOY | 44:c1d8923072ba | 65 | return array(); |
| TMBOY | 44:c1d8923072ba | 66 | } |
| TMBOY | 44:c1d8923072ba | 67 | |
| TMBOY | 44:c1d8923072ba | 68 | public function getPrefixesPsr4() |
| TMBOY | 44:c1d8923072ba | 69 | { |
| TMBOY | 44:c1d8923072ba | 70 | return $this->prefixDirsPsr4; |
| TMBOY | 44:c1d8923072ba | 71 | } |
| TMBOY | 44:c1d8923072ba | 72 | |
| TMBOY | 44:c1d8923072ba | 73 | public function getFallbackDirs() |
| TMBOY | 44:c1d8923072ba | 74 | { |
| TMBOY | 44:c1d8923072ba | 75 | return $this->fallbackDirsPsr0; |
| TMBOY | 44:c1d8923072ba | 76 | } |
| TMBOY | 44:c1d8923072ba | 77 | |
| TMBOY | 44:c1d8923072ba | 78 | public function getFallbackDirsPsr4() |
| TMBOY | 44:c1d8923072ba | 79 | { |
| TMBOY | 44:c1d8923072ba | 80 | return $this->fallbackDirsPsr4; |
| TMBOY | 44:c1d8923072ba | 81 | } |
| TMBOY | 44:c1d8923072ba | 82 | |
| TMBOY | 44:c1d8923072ba | 83 | public function getClassMap() |
| TMBOY | 44:c1d8923072ba | 84 | { |
| TMBOY | 44:c1d8923072ba | 85 | return $this->classMap; |
| TMBOY | 44:c1d8923072ba | 86 | } |
| TMBOY | 44:c1d8923072ba | 87 | |
| TMBOY | 44:c1d8923072ba | 88 | /** |
| TMBOY | 44:c1d8923072ba | 89 | * @param array $classMap Class to filename map |
| TMBOY | 44:c1d8923072ba | 90 | */ |
| TMBOY | 44:c1d8923072ba | 91 | public function addClassMap(array $classMap) |
| TMBOY | 44:c1d8923072ba | 92 | { |
| TMBOY | 44:c1d8923072ba | 93 | if ($this->classMap) { |
| TMBOY | 44:c1d8923072ba | 94 | $this->classMap = array_merge($this->classMap, $classMap); |
| TMBOY | 44:c1d8923072ba | 95 | } else { |
| TMBOY | 44:c1d8923072ba | 96 | $this->classMap = $classMap; |
| TMBOY | 44:c1d8923072ba | 97 | } |
| TMBOY | 44:c1d8923072ba | 98 | } |
| TMBOY | 44:c1d8923072ba | 99 | |
| TMBOY | 44:c1d8923072ba | 100 | /** |
| TMBOY | 44:c1d8923072ba | 101 | * Registers a set of PSR-0 directories for a given prefix, either |
| TMBOY | 44:c1d8923072ba | 102 | * appending or prepending to the ones previously set for this prefix. |
| TMBOY | 44:c1d8923072ba | 103 | * |
| TMBOY | 44:c1d8923072ba | 104 | * @param string $prefix The prefix |
| TMBOY | 44:c1d8923072ba | 105 | * @param array|string $paths The PSR-0 root directories |
| TMBOY | 44:c1d8923072ba | 106 | * @param bool $prepend Whether to prepend the directories |
| TMBOY | 44:c1d8923072ba | 107 | */ |
| TMBOY | 44:c1d8923072ba | 108 | public function add($prefix, $paths, $prepend = false) |
| TMBOY | 44:c1d8923072ba | 109 | { |
| TMBOY | 44:c1d8923072ba | 110 | if (!$prefix) { |
| TMBOY | 44:c1d8923072ba | 111 | if ($prepend) { |
| TMBOY | 44:c1d8923072ba | 112 | $this->fallbackDirsPsr0 = array_merge( |
| TMBOY | 44:c1d8923072ba | 113 | (array) $paths, |
| TMBOY | 44:c1d8923072ba | 114 | $this->fallbackDirsPsr0 |
| TMBOY | 44:c1d8923072ba | 115 | ); |
| TMBOY | 44:c1d8923072ba | 116 | } else { |
| TMBOY | 44:c1d8923072ba | 117 | $this->fallbackDirsPsr0 = array_merge( |
| TMBOY | 44:c1d8923072ba | 118 | $this->fallbackDirsPsr0, |
| TMBOY | 44:c1d8923072ba | 119 | (array) $paths |
| TMBOY | 44:c1d8923072ba | 120 | ); |
| TMBOY | 44:c1d8923072ba | 121 | } |
| TMBOY | 44:c1d8923072ba | 122 | |
| TMBOY | 44:c1d8923072ba | 123 | return; |
| TMBOY | 44:c1d8923072ba | 124 | } |
| TMBOY | 44:c1d8923072ba | 125 | |
| TMBOY | 44:c1d8923072ba | 126 | $first = $prefix[0]; |
| TMBOY | 44:c1d8923072ba | 127 | if (!isset($this->prefixesPsr0[$first][$prefix])) { |
| TMBOY | 44:c1d8923072ba | 128 | $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 129 | |
| TMBOY | 44:c1d8923072ba | 130 | return; |
| TMBOY | 44:c1d8923072ba | 131 | } |
| TMBOY | 44:c1d8923072ba | 132 | if ($prepend) { |
| TMBOY | 44:c1d8923072ba | 133 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| TMBOY | 44:c1d8923072ba | 134 | (array) $paths, |
| TMBOY | 44:c1d8923072ba | 135 | $this->prefixesPsr0[$first][$prefix] |
| TMBOY | 44:c1d8923072ba | 136 | ); |
| TMBOY | 44:c1d8923072ba | 137 | } else { |
| TMBOY | 44:c1d8923072ba | 138 | $this->prefixesPsr0[$first][$prefix] = array_merge( |
| TMBOY | 44:c1d8923072ba | 139 | $this->prefixesPsr0[$first][$prefix], |
| TMBOY | 44:c1d8923072ba | 140 | (array) $paths |
| TMBOY | 44:c1d8923072ba | 141 | ); |
| TMBOY | 44:c1d8923072ba | 142 | } |
| TMBOY | 44:c1d8923072ba | 143 | } |
| TMBOY | 44:c1d8923072ba | 144 | |
| TMBOY | 44:c1d8923072ba | 145 | /** |
| TMBOY | 44:c1d8923072ba | 146 | * Registers a set of PSR-4 directories for a given namespace, either |
| TMBOY | 44:c1d8923072ba | 147 | * appending or prepending to the ones previously set for this namespace. |
| TMBOY | 44:c1d8923072ba | 148 | * |
| TMBOY | 44:c1d8923072ba | 149 | * @param string $prefix The prefix/namespace, with trailing '\\' |
| TMBOY | 44:c1d8923072ba | 150 | * @param array|string $paths The PSR-4 base directories |
| TMBOY | 44:c1d8923072ba | 151 | * @param bool $prepend Whether to prepend the directories |
| TMBOY | 44:c1d8923072ba | 152 | * |
| TMBOY | 44:c1d8923072ba | 153 | * @throws \InvalidArgumentException |
| TMBOY | 44:c1d8923072ba | 154 | */ |
| TMBOY | 44:c1d8923072ba | 155 | public function addPsr4($prefix, $paths, $prepend = false) |
| TMBOY | 44:c1d8923072ba | 156 | { |
| TMBOY | 44:c1d8923072ba | 157 | if (!$prefix) { |
| TMBOY | 44:c1d8923072ba | 158 | // Register directories for the root namespace. |
| TMBOY | 44:c1d8923072ba | 159 | if ($prepend) { |
| TMBOY | 44:c1d8923072ba | 160 | $this->fallbackDirsPsr4 = array_merge( |
| TMBOY | 44:c1d8923072ba | 161 | (array) $paths, |
| TMBOY | 44:c1d8923072ba | 162 | $this->fallbackDirsPsr4 |
| TMBOY | 44:c1d8923072ba | 163 | ); |
| TMBOY | 44:c1d8923072ba | 164 | } else { |
| TMBOY | 44:c1d8923072ba | 165 | $this->fallbackDirsPsr4 = array_merge( |
| TMBOY | 44:c1d8923072ba | 166 | $this->fallbackDirsPsr4, |
| TMBOY | 44:c1d8923072ba | 167 | (array) $paths |
| TMBOY | 44:c1d8923072ba | 168 | ); |
| TMBOY | 44:c1d8923072ba | 169 | } |
| TMBOY | 44:c1d8923072ba | 170 | } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
| TMBOY | 44:c1d8923072ba | 171 | // Register directories for a new namespace. |
| TMBOY | 44:c1d8923072ba | 172 | $length = strlen($prefix); |
| TMBOY | 44:c1d8923072ba | 173 | if ('\\' !== $prefix[$length - 1]) { |
| TMBOY | 44:c1d8923072ba | 174 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| TMBOY | 44:c1d8923072ba | 175 | } |
| TMBOY | 44:c1d8923072ba | 176 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| TMBOY | 44:c1d8923072ba | 177 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 178 | } elseif ($prepend) { |
| TMBOY | 44:c1d8923072ba | 179 | // Prepend directories for an already registered namespace. |
| TMBOY | 44:c1d8923072ba | 180 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| TMBOY | 44:c1d8923072ba | 181 | (array) $paths, |
| TMBOY | 44:c1d8923072ba | 182 | $this->prefixDirsPsr4[$prefix] |
| TMBOY | 44:c1d8923072ba | 183 | ); |
| TMBOY | 44:c1d8923072ba | 184 | } else { |
| TMBOY | 44:c1d8923072ba | 185 | // Append directories for an already registered namespace. |
| TMBOY | 44:c1d8923072ba | 186 | $this->prefixDirsPsr4[$prefix] = array_merge( |
| TMBOY | 44:c1d8923072ba | 187 | $this->prefixDirsPsr4[$prefix], |
| TMBOY | 44:c1d8923072ba | 188 | (array) $paths |
| TMBOY | 44:c1d8923072ba | 189 | ); |
| TMBOY | 44:c1d8923072ba | 190 | } |
| TMBOY | 44:c1d8923072ba | 191 | } |
| TMBOY | 44:c1d8923072ba | 192 | |
| TMBOY | 44:c1d8923072ba | 193 | /** |
| TMBOY | 44:c1d8923072ba | 194 | * Registers a set of PSR-0 directories for a given prefix, |
| TMBOY | 44:c1d8923072ba | 195 | * replacing any others previously set for this prefix. |
| TMBOY | 44:c1d8923072ba | 196 | * |
| TMBOY | 44:c1d8923072ba | 197 | * @param string $prefix The prefix |
| TMBOY | 44:c1d8923072ba | 198 | * @param array|string $paths The PSR-0 base directories |
| TMBOY | 44:c1d8923072ba | 199 | */ |
| TMBOY | 44:c1d8923072ba | 200 | public function set($prefix, $paths) |
| TMBOY | 44:c1d8923072ba | 201 | { |
| TMBOY | 44:c1d8923072ba | 202 | if (!$prefix) { |
| TMBOY | 44:c1d8923072ba | 203 | $this->fallbackDirsPsr0 = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 204 | } else { |
| TMBOY | 44:c1d8923072ba | 205 | $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 206 | } |
| TMBOY | 44:c1d8923072ba | 207 | } |
| TMBOY | 44:c1d8923072ba | 208 | |
| TMBOY | 44:c1d8923072ba | 209 | /** |
| TMBOY | 44:c1d8923072ba | 210 | * Registers a set of PSR-4 directories for a given namespace, |
| TMBOY | 44:c1d8923072ba | 211 | * replacing any others previously set for this namespace. |
| TMBOY | 44:c1d8923072ba | 212 | * |
| TMBOY | 44:c1d8923072ba | 213 | * @param string $prefix The prefix/namespace, with trailing '\\' |
| TMBOY | 44:c1d8923072ba | 214 | * @param array|string $paths The PSR-4 base directories |
| TMBOY | 44:c1d8923072ba | 215 | * |
| TMBOY | 44:c1d8923072ba | 216 | * @throws \InvalidArgumentException |
| TMBOY | 44:c1d8923072ba | 217 | */ |
| TMBOY | 44:c1d8923072ba | 218 | public function setPsr4($prefix, $paths) |
| TMBOY | 44:c1d8923072ba | 219 | { |
| TMBOY | 44:c1d8923072ba | 220 | if (!$prefix) { |
| TMBOY | 44:c1d8923072ba | 221 | $this->fallbackDirsPsr4 = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 222 | } else { |
| TMBOY | 44:c1d8923072ba | 223 | $length = strlen($prefix); |
| TMBOY | 44:c1d8923072ba | 224 | if ('\\' !== $prefix[$length - 1]) { |
| TMBOY | 44:c1d8923072ba | 225 | throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
| TMBOY | 44:c1d8923072ba | 226 | } |
| TMBOY | 44:c1d8923072ba | 227 | $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
| TMBOY | 44:c1d8923072ba | 228 | $this->prefixDirsPsr4[$prefix] = (array) $paths; |
| TMBOY | 44:c1d8923072ba | 229 | } |
| TMBOY | 44:c1d8923072ba | 230 | } |
| TMBOY | 44:c1d8923072ba | 231 | |
| TMBOY | 44:c1d8923072ba | 232 | /** |
| TMBOY | 44:c1d8923072ba | 233 | * Turns on searching the include path for class files. |
| TMBOY | 44:c1d8923072ba | 234 | * |
| TMBOY | 44:c1d8923072ba | 235 | * @param bool $useIncludePath |
| TMBOY | 44:c1d8923072ba | 236 | */ |
| TMBOY | 44:c1d8923072ba | 237 | public function setUseIncludePath($useIncludePath) |
| TMBOY | 44:c1d8923072ba | 238 | { |
| TMBOY | 44:c1d8923072ba | 239 | $this->useIncludePath = $useIncludePath; |
| TMBOY | 44:c1d8923072ba | 240 | } |
| TMBOY | 44:c1d8923072ba | 241 | |
| TMBOY | 44:c1d8923072ba | 242 | /** |
| TMBOY | 44:c1d8923072ba | 243 | * Can be used to check if the autoloader uses the include path to check |
| TMBOY | 44:c1d8923072ba | 244 | * for classes. |
| TMBOY | 44:c1d8923072ba | 245 | * |
| TMBOY | 44:c1d8923072ba | 246 | * @return bool |
| TMBOY | 44:c1d8923072ba | 247 | */ |
| TMBOY | 44:c1d8923072ba | 248 | public function getUseIncludePath() |
| TMBOY | 44:c1d8923072ba | 249 | { |
| TMBOY | 44:c1d8923072ba | 250 | return $this->useIncludePath; |
| TMBOY | 44:c1d8923072ba | 251 | } |
| TMBOY | 44:c1d8923072ba | 252 | |
| TMBOY | 44:c1d8923072ba | 253 | /** |
| TMBOY | 44:c1d8923072ba | 254 | * Turns off searching the prefix and fallback directories for classes |
| TMBOY | 44:c1d8923072ba | 255 | * that have not been registered with the class map. |
| TMBOY | 44:c1d8923072ba | 256 | * |
| TMBOY | 44:c1d8923072ba | 257 | * @param bool $classMapAuthoritative |
| TMBOY | 44:c1d8923072ba | 258 | */ |
| TMBOY | 44:c1d8923072ba | 259 | public function setClassMapAuthoritative($classMapAuthoritative) |
| TMBOY | 44:c1d8923072ba | 260 | { |
| TMBOY | 44:c1d8923072ba | 261 | $this->classMapAuthoritative = $classMapAuthoritative; |
| TMBOY | 44:c1d8923072ba | 262 | } |
| TMBOY | 44:c1d8923072ba | 263 | |
| TMBOY | 44:c1d8923072ba | 264 | /** |
| TMBOY | 44:c1d8923072ba | 265 | * Should class lookup fail if not found in the current class map? |
| TMBOY | 44:c1d8923072ba | 266 | * |
| TMBOY | 44:c1d8923072ba | 267 | * @return bool |
| TMBOY | 44:c1d8923072ba | 268 | */ |
| TMBOY | 44:c1d8923072ba | 269 | public function isClassMapAuthoritative() |
| TMBOY | 44:c1d8923072ba | 270 | { |
| TMBOY | 44:c1d8923072ba | 271 | return $this->classMapAuthoritative; |
| TMBOY | 44:c1d8923072ba | 272 | } |
| TMBOY | 44:c1d8923072ba | 273 | |
| TMBOY | 44:c1d8923072ba | 274 | /** |
| TMBOY | 44:c1d8923072ba | 275 | * Registers this instance as an autoloader. |
| TMBOY | 44:c1d8923072ba | 276 | * |
| TMBOY | 44:c1d8923072ba | 277 | * @param bool $prepend Whether to prepend the autoloader or not |
| TMBOY | 44:c1d8923072ba | 278 | */ |
| TMBOY | 44:c1d8923072ba | 279 | public function register($prepend = false) |
| TMBOY | 44:c1d8923072ba | 280 | { |
| TMBOY | 44:c1d8923072ba | 281 | spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
| TMBOY | 44:c1d8923072ba | 282 | } |
| TMBOY | 44:c1d8923072ba | 283 | |
| TMBOY | 44:c1d8923072ba | 284 | /** |
| TMBOY | 44:c1d8923072ba | 285 | * Unregisters this instance as an autoloader. |
| TMBOY | 44:c1d8923072ba | 286 | */ |
| TMBOY | 44:c1d8923072ba | 287 | public function unregister() |
| TMBOY | 44:c1d8923072ba | 288 | { |
| TMBOY | 44:c1d8923072ba | 289 | spl_autoload_unregister(array($this, 'loadClass')); |
| TMBOY | 44:c1d8923072ba | 290 | } |
| TMBOY | 44:c1d8923072ba | 291 | |
| TMBOY | 44:c1d8923072ba | 292 | /** |
| TMBOY | 44:c1d8923072ba | 293 | * Loads the given class or interface. |
| TMBOY | 44:c1d8923072ba | 294 | * |
| TMBOY | 44:c1d8923072ba | 295 | * @param string $class The name of the class |
| TMBOY | 44:c1d8923072ba | 296 | * @return bool|null True if loaded, null otherwise |
| TMBOY | 44:c1d8923072ba | 297 | */ |
| TMBOY | 44:c1d8923072ba | 298 | public function loadClass($class) |
| TMBOY | 44:c1d8923072ba | 299 | { |
| TMBOY | 44:c1d8923072ba | 300 | if ($file = $this->findFile($class)) { |
| TMBOY | 44:c1d8923072ba | 301 | includeFile($file); |
| TMBOY | 44:c1d8923072ba | 302 | |
| TMBOY | 44:c1d8923072ba | 303 | return true; |
| TMBOY | 44:c1d8923072ba | 304 | } |
| TMBOY | 44:c1d8923072ba | 305 | } |
| TMBOY | 44:c1d8923072ba | 306 | |
| TMBOY | 44:c1d8923072ba | 307 | /** |
| TMBOY | 44:c1d8923072ba | 308 | * Finds the path to the file where the class is defined. |
| TMBOY | 44:c1d8923072ba | 309 | * |
| TMBOY | 44:c1d8923072ba | 310 | * @param string $class The name of the class |
| TMBOY | 44:c1d8923072ba | 311 | * |
| TMBOY | 44:c1d8923072ba | 312 | * @return string|false The path if found, false otherwise |
| TMBOY | 44:c1d8923072ba | 313 | */ |
| TMBOY | 44:c1d8923072ba | 314 | public function findFile($class) |
| TMBOY | 44:c1d8923072ba | 315 | { |
| TMBOY | 44:c1d8923072ba | 316 | // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 |
| TMBOY | 44:c1d8923072ba | 317 | if ('\\' == $class[0]) { |
| TMBOY | 44:c1d8923072ba | 318 | $class = substr($class, 1); |
| TMBOY | 44:c1d8923072ba | 319 | } |
| TMBOY | 44:c1d8923072ba | 320 | |
| TMBOY | 44:c1d8923072ba | 321 | // class map lookup |
| TMBOY | 44:c1d8923072ba | 322 | if (isset($this->classMap[$class])) { |
| TMBOY | 44:c1d8923072ba | 323 | return $this->classMap[$class]; |
| TMBOY | 44:c1d8923072ba | 324 | } |
| TMBOY | 44:c1d8923072ba | 325 | if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
| TMBOY | 44:c1d8923072ba | 326 | return false; |
| TMBOY | 44:c1d8923072ba | 327 | } |
| TMBOY | 44:c1d8923072ba | 328 | |
| TMBOY | 44:c1d8923072ba | 329 | $file = $this->findFileWithExtension($class, '.php'); |
| TMBOY | 44:c1d8923072ba | 330 | |
| TMBOY | 44:c1d8923072ba | 331 | // Search for Hack files if we are running on HHVM |
| TMBOY | 44:c1d8923072ba | 332 | if (false === $file && defined('HHVM_VERSION')) { |
| TMBOY | 44:c1d8923072ba | 333 | $file = $this->findFileWithExtension($class, '.hh'); |
| TMBOY | 44:c1d8923072ba | 334 | } |
| TMBOY | 44:c1d8923072ba | 335 | |
| TMBOY | 44:c1d8923072ba | 336 | if (false === $file) { |
| TMBOY | 44:c1d8923072ba | 337 | // Remember that this class does not exist. |
| TMBOY | 44:c1d8923072ba | 338 | $this->missingClasses[$class] = true; |
| TMBOY | 44:c1d8923072ba | 339 | } |
| TMBOY | 44:c1d8923072ba | 340 | |
| TMBOY | 44:c1d8923072ba | 341 | return $file; |
| TMBOY | 44:c1d8923072ba | 342 | } |
| TMBOY | 44:c1d8923072ba | 343 | |
| TMBOY | 44:c1d8923072ba | 344 | private function findFileWithExtension($class, $ext) |
| TMBOY | 44:c1d8923072ba | 345 | { |
| TMBOY | 44:c1d8923072ba | 346 | // PSR-4 lookup |
| TMBOY | 44:c1d8923072ba | 347 | $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
| TMBOY | 44:c1d8923072ba | 348 | |
| TMBOY | 44:c1d8923072ba | 349 | $first = $class[0]; |
| TMBOY | 44:c1d8923072ba | 350 | if (isset($this->prefixLengthsPsr4[$first])) { |
| TMBOY | 44:c1d8923072ba | 351 | foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { |
| TMBOY | 44:c1d8923072ba | 352 | if (0 === strpos($class, $prefix)) { |
| TMBOY | 44:c1d8923072ba | 353 | foreach ($this->prefixDirsPsr4[$prefix] as $dir) { |
| TMBOY | 44:c1d8923072ba | 354 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) { |
| TMBOY | 44:c1d8923072ba | 355 | return $file; |
| TMBOY | 44:c1d8923072ba | 356 | } |
| TMBOY | 44:c1d8923072ba | 357 | } |
| TMBOY | 44:c1d8923072ba | 358 | } |
| TMBOY | 44:c1d8923072ba | 359 | } |
| TMBOY | 44:c1d8923072ba | 360 | } |
| TMBOY | 44:c1d8923072ba | 361 | |
| TMBOY | 44:c1d8923072ba | 362 | // PSR-4 fallback dirs |
| TMBOY | 44:c1d8923072ba | 363 | foreach ($this->fallbackDirsPsr4 as $dir) { |
| TMBOY | 44:c1d8923072ba | 364 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
| TMBOY | 44:c1d8923072ba | 365 | return $file; |
| TMBOY | 44:c1d8923072ba | 366 | } |
| TMBOY | 44:c1d8923072ba | 367 | } |
| TMBOY | 44:c1d8923072ba | 368 | |
| TMBOY | 44:c1d8923072ba | 369 | // PSR-0 lookup |
| TMBOY | 44:c1d8923072ba | 370 | if (false !== $pos = strrpos($class, '\\')) { |
| TMBOY | 44:c1d8923072ba | 371 | // namespaced class name |
| TMBOY | 44:c1d8923072ba | 372 | $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
| TMBOY | 44:c1d8923072ba | 373 | . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
| TMBOY | 44:c1d8923072ba | 374 | } else { |
| TMBOY | 44:c1d8923072ba | 375 | // PEAR-like class name |
| TMBOY | 44:c1d8923072ba | 376 | $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
| TMBOY | 44:c1d8923072ba | 377 | } |
| TMBOY | 44:c1d8923072ba | 378 | |
| TMBOY | 44:c1d8923072ba | 379 | if (isset($this->prefixesPsr0[$first])) { |
| TMBOY | 44:c1d8923072ba | 380 | foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
| TMBOY | 44:c1d8923072ba | 381 | if (0 === strpos($class, $prefix)) { |
| TMBOY | 44:c1d8923072ba | 382 | foreach ($dirs as $dir) { |
| TMBOY | 44:c1d8923072ba | 383 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| TMBOY | 44:c1d8923072ba | 384 | return $file; |
| TMBOY | 44:c1d8923072ba | 385 | } |
| TMBOY | 44:c1d8923072ba | 386 | } |
| TMBOY | 44:c1d8923072ba | 387 | } |
| TMBOY | 44:c1d8923072ba | 388 | } |
| TMBOY | 44:c1d8923072ba | 389 | } |
| TMBOY | 44:c1d8923072ba | 390 | |
| TMBOY | 44:c1d8923072ba | 391 | // PSR-0 fallback dirs |
| TMBOY | 44:c1d8923072ba | 392 | foreach ($this->fallbackDirsPsr0 as $dir) { |
| TMBOY | 44:c1d8923072ba | 393 | if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
| TMBOY | 44:c1d8923072ba | 394 | return $file; |
| TMBOY | 44:c1d8923072ba | 395 | } |
| TMBOY | 44:c1d8923072ba | 396 | } |
| TMBOY | 44:c1d8923072ba | 397 | |
| TMBOY | 44:c1d8923072ba | 398 | // PSR-0 include paths. |
| TMBOY | 44:c1d8923072ba | 399 | if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
| TMBOY | 44:c1d8923072ba | 400 | return $file; |
| TMBOY | 44:c1d8923072ba | 401 | } |
| TMBOY | 44:c1d8923072ba | 402 | |
| TMBOY | 44:c1d8923072ba | 403 | return false; |
| TMBOY | 44:c1d8923072ba | 404 | } |
| TMBOY | 44:c1d8923072ba | 405 | } |
| TMBOY | 44:c1d8923072ba | 406 | |
| TMBOY | 44:c1d8923072ba | 407 | /** |
| TMBOY | 44:c1d8923072ba | 408 | * Scope isolated include. |
| TMBOY | 44:c1d8923072ba | 409 | * |
| TMBOY | 44:c1d8923072ba | 410 | * Prevents access to $this/self from included files. |
| TMBOY | 44:c1d8923072ba | 411 | */ |
| TMBOY | 44:c1d8923072ba | 412 | function includeFile($file) |
| TMBOY | 44:c1d8923072ba | 413 | { |
| TMBOY | 44:c1d8923072ba | 414 | include $file; |
| TMBOY | 44:c1d8923072ba | 415 | } |
