61 if (!empty($this->prefixesPsr0)) {
62 return call_user_func_array(
'array_merge', $this->prefixesPsr0);
93 if ($this->classMap) {
94 $this->classMap = array_merge($this->classMap,
$classMap);
108 public function add($prefix, $paths, $prepend =
false)
112 $this->fallbackDirsPsr0 = array_merge(
114 $this->fallbackDirsPsr0
117 $this->fallbackDirsPsr0 = array_merge(
118 $this->fallbackDirsPsr0,
127 if (!isset($this->prefixesPsr0[$first][$prefix])) {
128 $this->prefixesPsr0[$first][$prefix] = (array) $paths;
133 $this->prefixesPsr0[$first][$prefix] = array_merge(
135 $this->prefixesPsr0[$first][$prefix]
138 $this->prefixesPsr0[$first][$prefix] = array_merge(
139 $this->prefixesPsr0[$first][$prefix],
155 public function addPsr4($prefix, $paths, $prepend =
false)
160 $this->fallbackDirsPsr4 = array_merge(
162 $this->fallbackDirsPsr4
165 $this->fallbackDirsPsr4 = array_merge(
166 $this->fallbackDirsPsr4,
170 } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
172 $length = strlen($prefix);
173 if (
'\\' !== $prefix[$length - 1]) {
174 throw new \InvalidArgumentException(
"A non-empty PSR-4 prefix must end with a namespace separator.");
176 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
177 $this->prefixDirsPsr4[$prefix] = (array) $paths;
178 } elseif ($prepend) {
180 $this->prefixDirsPsr4[$prefix] = array_merge(
182 $this->prefixDirsPsr4[$prefix]
186 $this->prefixDirsPsr4[$prefix] = array_merge(
187 $this->prefixDirsPsr4[$prefix],
200 public function set($prefix, $paths)
203 $this->fallbackDirsPsr0 = (array) $paths;
205 $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
221 $this->fallbackDirsPsr4 = (array) $paths;
223 $length = strlen($prefix);
224 if (
'\\' !== $prefix[$length - 1]) {
225 throw new \InvalidArgumentException(
"A non-empty PSR-4 prefix must end with a namespace separator.");
227 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
228 $this->prefixDirsPsr4[$prefix] = (array) $paths;
279 public function register($prepend =
false)
281 spl_autoload_register(array($this,
'loadClass'),
true, $prepend);
289 spl_autoload_unregister(array($this,
'loadClass'));
317 if (
'\\' == $class[0]) {
318 $class = substr($class, 1);
322 if (isset($this->classMap[$class])) {
323 return $this->classMap[$class];
325 if ($this->classMapAuthoritative) {
332 if (
$file ===
null && defined(
'HHVM_VERSION')) {
336 if (
$file ===
null) {
338 return $this->classMap[$class] =
false;
347 $logicalPathPsr4 = strtr($class,
'\\', DIRECTORY_SEPARATOR) . $ext;
350 if (isset($this->prefixLengthsPsr4[$first])) {
351 foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
352 if (0 === strpos($class, $prefix)) {
353 foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
354 if (is_file(
$file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
363 foreach ($this->fallbackDirsPsr4 as $dir) {
364 if (is_file(
$file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
370 if (
false !== $pos = strrpos($class,
'\\')) {
372 $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
373 . strtr(substr($logicalPathPsr4, $pos + 1),
'_', DIRECTORY_SEPARATOR);
376 $logicalPathPsr0 = strtr($class,
'_', DIRECTORY_SEPARATOR) . $ext;
379 if (isset($this->prefixesPsr0[$first])) {
380 foreach ($this->prefixesPsr0[$first] as $prefix =>
$dirs) {
381 if (0 === strpos($class, $prefix)) {
382 foreach (
$dirs as $dir) {
383 if (is_file(
$file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
392 foreach ($this->fallbackDirsPsr0 as $dir) {
393 if (is_file(
$file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
399 if ($this->useIncludePath &&
$file = stream_resolve_include_path($logicalPathPsr0)) {
ClassLoader implements a PSR-0 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...
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?
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.