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);
109 public function add($prefix, $paths, $prepend =
false)
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],
156 public function addPsr4($prefix, $paths, $prepend =
false)
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],
201 public function set($prefix, $paths)
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'));
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);
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 foreach ($this->prefixDirsPsr4[$search] as $dir) {
383 $length = $this->prefixLengthsPsr4[$first][$search];
384 if (file_exists(
$file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
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)) {
An exception for terminatinating execution or to throw for unit testing.
ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
setPsr4($prefix, $paths)
Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for t...
addClassMap(array $classMap)
unregister()
Unregisters this instance as an autoloader.
findFileWithExtension($class, $ext)
setUseIncludePath($useIncludePath)
Turns on searching the include path for class files.
findFile($class)
Finds the path to the file where the class is defined.
addPsr4($prefix, $paths, $prepend=false)
Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the one...
setApcuPrefix($apcuPrefix)
APCu prefix to use to cache found/not-found classes, if the extension is 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...
setClassMapAuthoritative($classMapAuthoritative)
Turns off searching the prefix and fallback directories for classes that have not been registered wit...
isClassMapAuthoritative()
Should class lookup fail if not found in the current class map?
getApcuPrefix()
The APCu prefix in use, or null if APCu caching is not enabled.
getUseIncludePath()
Can be used to check if the autoloader uses the include path to check for classes.
loadClass($class)
Loads the given class or interface.
includeFile($file)
Scope isolated include.
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
if(!file_exists("$old.txt")) if( $old===$new) if(file_exists("$new.txt")) $file