62 if (!empty($this->prefixesPsr0)) {
63 return call_user_func_array(
'array_merge', $this->prefixesPsr0);
94 if ($this->classMap) {
95 $this->classMap = array_merge($this->classMap, $classMap);
113 $this->fallbackDirsPsr0 = array_merge(
115 $this->fallbackDirsPsr0
118 $this->fallbackDirsPsr0 = array_merge(
119 $this->fallbackDirsPsr0,
128 if (!isset($this->prefixesPsr0[$first][$prefix])) {
129 $this->prefixesPsr0[$first][$prefix] = (array)
$paths;
134 $this->prefixesPsr0[$first][$prefix] = array_merge(
136 $this->prefixesPsr0[$first][$prefix]
139 $this->prefixesPsr0[$first][$prefix] = array_merge(
140 $this->prefixesPsr0[$first][$prefix],
161 $this->fallbackDirsPsr4 = array_merge(
163 $this->fallbackDirsPsr4
166 $this->fallbackDirsPsr4 = array_merge(
167 $this->fallbackDirsPsr4,
171 } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
173 $length = strlen($prefix);
174 if (
'\\' !== $prefix[$length - 1]) {
175 throw new \InvalidArgumentException(
"A non-empty PSR-4 prefix must end with a namespace separator.");
177 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
178 $this->prefixDirsPsr4[$prefix] = (array)
$paths;
179 } elseif ($prepend) {
181 $this->prefixDirsPsr4[$prefix] = array_merge(
183 $this->prefixDirsPsr4[$prefix]
187 $this->prefixDirsPsr4[$prefix] = array_merge(
188 $this->prefixDirsPsr4[$prefix],
204 $this->fallbackDirsPsr0 = (array)
$paths;
206 $this->prefixesPsr0[$prefix[0]][$prefix] = (array)
$paths;
222 $this->fallbackDirsPsr4 = (array)
$paths;
224 $length = strlen($prefix);
225 if (
'\\' !== $prefix[$length - 1]) {
226 throw new \InvalidArgumentException(
"A non-empty PSR-4 prefix must end with a namespace separator.");
228 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
229 $this->prefixDirsPsr4[$prefix] = (array)
$paths;
282 $this->apcuPrefix = function_exists(
'apcu_fetch') && ini_get(
'apc.enabled') ?
$apcuPrefix : null;
300 public function register($prepend =
false)
302 spl_autoload_register(array($this,
'loadClass'),
true, $prepend);
310 spl_autoload_unregister(array($this,
'loadClass'));
321 if ($file = $this->
findFile($class)) {
338 if (isset($this->classMap[$class])) {
339 return $this->classMap[$class];
341 if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
344 if (null !== $this->apcuPrefix) {
345 $file = apcu_fetch($this->apcuPrefix.$class, $hit);
354 if (
false === $file && defined(
'HHVM_VERSION')) {
358 if (null !== $this->apcuPrefix) {
359 apcu_add($this->apcuPrefix.$class, $file);
362 if (
false === $file) {
364 $this->missingClasses[$class] =
true;
373 $logicalPathPsr4 = strtr($class,
'\\', DIRECTORY_SEPARATOR) . $ext;
376 if (isset($this->prefixLengthsPsr4[$first])) {
378 while (
false !== $lastPos = strrpos($subPath,
'\\')) {
379 $subPath = substr($subPath, 0, $lastPos);
380 $search = $subPath.
'\\';
381 if (isset($this->prefixDirsPsr4[$search])) {
382 $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
383 foreach ($this->prefixDirsPsr4[$search] as $dir) {
384 if (file_exists($file = $dir . $pathEnd)) {
393 foreach ($this->fallbackDirsPsr4 as $dir) {
394 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
400 if (
false !== $pos = strrpos($class,
'\\')) {
402 $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
403 . strtr(substr($logicalPathPsr4, $pos + 1),
'_', DIRECTORY_SEPARATOR);
406 $logicalPathPsr0 = strtr($class,
'_', DIRECTORY_SEPARATOR) . $ext;
409 if (isset($this->prefixesPsr0[$first])) {
410 foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
411 if (0 === strpos($class, $prefix)) {
412 foreach ($dirs as $dir) {
413 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
422 foreach ($this->fallbackDirsPsr0 as $dir) {
423 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
429 if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
loadClass($class)
Loads the given class or interface.
getUseIncludePath()
Can be used to check if the autoloader uses the include path to check for classes.
setUseIncludePath($useIncludePath)
Turns on searching the include path for class files.
getApcuPrefix()
The APCu prefix in use, or null if APCu caching is not enabled.
add($prefix, $paths, $prepend=false)
Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones p...
setApcuPrefix($apcuPrefix)
APCu prefix to use to cache found/not-found classes, if the extension is enabled. ...
setClassMapAuthoritative($classMapAuthoritative)
Turns off searching the prefix and fallback directories for classes that have not been registered wit...
addPsr4($prefix, $paths, $prepend=false)
Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the one...
findFileWithExtension($class, $ext)
unregister()
Unregisters this instance as an autoloader.
addClassMap(array $classMap)
setPsr4($prefix, $paths)
Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for t...
includeFile($file)
Scope isolated include.
isClassMapAuthoritative()
Should class lookup fail if not found in the current class map?
ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
findFile($class)
Finds the path to the file where the class is defined.