ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
Composer\Autoload\ClassLoader Class Reference

ClassLoader implements a PSR-0 class loader. More...

+ Collaboration diagram for Composer\Autoload\ClassLoader:

Public Member Functions

 getPrefixes ()
 
 getPrefixesPsr4 ()
 
 getFallbackDirs ()
 
 getFallbackDirsPsr4 ()
 
 getClassMap ()
 
 addClassMap (array $classMap)
 
 add ($prefix, $paths, $prepend=false)
 Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix. More...
 
 addPsr4 ($prefix, $paths, $prepend=false)
 Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace. More...
 
 set ($prefix, $paths)
 Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix. More...
 
 setPsr4 ($prefix, $paths)
 Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace. More...
 
 setUseIncludePath ($useIncludePath)
 Turns on searching the include path for class files. More...
 
 getUseIncludePath ()
 Can be used to check if the autoloader uses the include path to check for classes. More...
 
 setClassMapAuthoritative ($classMapAuthoritative)
 Turns off searching the prefix and fallback directories for classes that have not been registered with the class map. More...
 
 isClassMapAuthoritative ()
 Should class lookup fail if not found in the current class map? More...
 
 register ($prepend=false)
 Registers this instance as an autoloader. More...
 
 unregister ()
 Unregisters this instance as an autoloader. More...
 
 loadClass ($class)
 Loads the given class or interface. More...
 
 findFile ($class)
 Finds the path to the file where the class is defined. More...
 

Private Member Functions

 findFileWithExtension ($class, $ext)
 

Private Attributes

 $prefixLengthsPsr4 = array()
 
 $prefixDirsPsr4 = array()
 
 $fallbackDirsPsr4 = array()
 
 $prefixesPsr0 = array()
 
 $fallbackDirsPsr0 = array()
 
 $useIncludePath = false
 
 $classMap = array()
 
 $classMapAuthoritative = false
 

Detailed Description

ClassLoader implements a PSR-0 class loader.

See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

$loader = new \Composer\Autoload\ClassLoader();

// register classes with namespaces
$loader->add('Symfony\Component', __DIR__.'/component');
$loader->add('Symfony',           __DIR__.'/framework');

// activate the autoloader
$loader->register();

// to enable searching the include path (eg. for PEAR packages)
$loader->setUseIncludePath(true);

In this example, if you try to use a class in the Symfony namespace or one of its children (Symfony for instance), the autoloader will first look for the class under the component/ directory, and it will then fallback to the framework/ directory if not found before giving up.

This class is loosely based on the Symfony UniversalClassLoader.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com
Jordi Boggiano j.bog.nosp@m.gian.nosp@m.o@sel.nosp@m.d.be

Definition at line 43 of file ClassLoader.php.

Member Function Documentation

◆ add()

Composer\Autoload\ClassLoader::add (   $prefix,
  $paths,
  $prepend = false 
)

Registers a set of PSR-0 directories for a given prefix, either appending or prepending to the ones previously set for this prefix.

Parameters
string$prefixThe prefix
array | string$pathsThe PSR-0 root directories
bool$prependWhether to prepend the directories

Definition at line 108 of file ClassLoader.php.

109  {
110  if (!$prefix) {
111  if ($prepend) {
112  $this->fallbackDirsPsr0 = array_merge(
113  (array) $paths,
114  $this->fallbackDirsPsr0
115  );
116  } else {
117  $this->fallbackDirsPsr0 = array_merge(
118  $this->fallbackDirsPsr0,
119  (array) $paths
120  );
121  }
122 
123  return;
124  }
125 
126  $first = $prefix[0];
127  if (!isset($this->prefixesPsr0[$first][$prefix])) {
128  $this->prefixesPsr0[$first][$prefix] = (array) $paths;
129 
130  return;
131  }
132  if ($prepend) {
133  $this->prefixesPsr0[$first][$prefix] = array_merge(
134  (array) $paths,
135  $this->prefixesPsr0[$first][$prefix]
136  );
137  } else {
138  $this->prefixesPsr0[$first][$prefix] = array_merge(
139  $this->prefixesPsr0[$first][$prefix],
140  (array) $paths
141  );
142  }
143  }

◆ addClassMap()

Composer\Autoload\ClassLoader::addClassMap ( array  $classMap)
Parameters
array$classMapClass to filename map

Definition at line 91 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$classMap.

92  {
93  if ($this->classMap) {
94  $this->classMap = array_merge($this->classMap, $classMap);
95  } else {
96  $this->classMap = $classMap;
97  }
98  }

◆ addPsr4()

Composer\Autoload\ClassLoader::addPsr4 (   $prefix,
  $paths,
  $prepend = false 
)

Registers a set of PSR-4 directories for a given namespace, either appending or prepending to the ones previously set for this namespace.

Parameters
string$prefixThe prefix/namespace, with trailing '\'
array | string$pathsThe PSR-0 base directories
bool$prependWhether to prepend the directories
Exceptions

Definition at line 155 of file ClassLoader.php.

156  {
157  if (!$prefix) {
158  // Register directories for the root namespace.
159  if ($prepend) {
160  $this->fallbackDirsPsr4 = array_merge(
161  (array) $paths,
162  $this->fallbackDirsPsr4
163  );
164  } else {
165  $this->fallbackDirsPsr4 = array_merge(
166  $this->fallbackDirsPsr4,
167  (array) $paths
168  );
169  }
170  } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
171  // Register directories for a new namespace.
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.");
175  }
176  $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
177  $this->prefixDirsPsr4[$prefix] = (array) $paths;
178  } elseif ($prepend) {
179  // Prepend directories for an already registered namespace.
180  $this->prefixDirsPsr4[$prefix] = array_merge(
181  (array) $paths,
182  $this->prefixDirsPsr4[$prefix]
183  );
184  } else {
185  // Append directories for an already registered namespace.
186  $this->prefixDirsPsr4[$prefix] = array_merge(
187  $this->prefixDirsPsr4[$prefix],
188  (array) $paths
189  );
190  }
191  }

◆ findFile()

Composer\Autoload\ClassLoader::findFile (   $class)

Finds the path to the file where the class is defined.

Parameters
string$classThe name of the class
Returns
string|false The path if found, false otherwise

Definition at line 314 of file ClassLoader.php.

References $file, and Composer\Autoload\ClassLoader\findFileWithExtension().

Referenced by Composer\Autoload\ClassLoader\loadClass().

315  {
316  // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
317  if ('\\' == $class[0]) {
318  $class = substr($class, 1);
319  }
320 
321  // class map lookup
322  if (isset($this->classMap[$class])) {
323  return $this->classMap[$class];
324  }
325  if ($this->classMapAuthoritative) {
326  return false;
327  }
328 
329  $file = $this->findFileWithExtension($class, '.php');
330 
331  // Search for Hack files if we are running on HHVM
332  if ($file === null && defined('HHVM_VERSION')) {
333  $file = $this->findFileWithExtension($class, '.hh');
334  }
335 
336  if ($file === null) {
337  // Remember that this class does not exist.
338  return $this->classMap[$class] = false;
339  }
340 
341  return $file;
342  }
print $file
findFileWithExtension($class, $ext)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ findFileWithExtension()

Composer\Autoload\ClassLoader::findFileWithExtension (   $class,
  $ext 
)
private

Definition at line 344 of file ClassLoader.php.

References $dirs, and $file.

Referenced by Composer\Autoload\ClassLoader\findFile().

345  {
346  // PSR-4 lookup
347  $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
348 
349  $first = $class[0];
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))) {
355  return $file;
356  }
357  }
358  }
359  }
360  }
361 
362  // PSR-4 fallback dirs
363  foreach ($this->fallbackDirsPsr4 as $dir) {
364  if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
365  return $file;
366  }
367  }
368 
369  // PSR-0 lookup
370  if (false !== $pos = strrpos($class, '\\')) {
371  // namespaced class name
372  $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
373  . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
374  } else {
375  // PEAR-like class name
376  $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
377  }
378 
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)) {
384  return $file;
385  }
386  }
387  }
388  }
389  }
390 
391  // PSR-0 fallback dirs
392  foreach ($this->fallbackDirsPsr0 as $dir) {
393  if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
394  return $file;
395  }
396  }
397 
398  // PSR-0 include paths.
399  if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
400  return $file;
401  }
402  }
print $file
$dirs
+ Here is the caller graph for this function:

◆ getClassMap()

Composer\Autoload\ClassLoader::getClassMap ( )

Definition at line 83 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$classMap.

84  {
85  return $this->classMap;
86  }

◆ getFallbackDirs()

Composer\Autoload\ClassLoader::getFallbackDirs ( )

◆ getFallbackDirsPsr4()

Composer\Autoload\ClassLoader::getFallbackDirsPsr4 ( )

◆ getPrefixes()

Composer\Autoload\ClassLoader::getPrefixes ( )

Definition at line 59 of file ClassLoader.php.

60  {
61  if (!empty($this->prefixesPsr0)) {
62  return call_user_func_array('array_merge', $this->prefixesPsr0);
63  }
64 
65  return array();
66  }

◆ getPrefixesPsr4()

Composer\Autoload\ClassLoader::getPrefixesPsr4 ( )

Definition at line 68 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$prefixDirsPsr4.

69  {
70  return $this->prefixDirsPsr4;
71  }

◆ getUseIncludePath()

Composer\Autoload\ClassLoader::getUseIncludePath ( )

Can be used to check if the autoloader uses the include path to check for classes.

Returns
bool

Definition at line 248 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$useIncludePath.

249  {
250  return $this->useIncludePath;
251  }

◆ isClassMapAuthoritative()

Composer\Autoload\ClassLoader::isClassMapAuthoritative ( )

Should class lookup fail if not found in the current class map?

Returns
bool

Definition at line 269 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$classMapAuthoritative.

270  {
272  }

◆ loadClass()

Composer\Autoload\ClassLoader::loadClass (   $class)

Loads the given class or interface.

Parameters
string$classThe name of the class
Returns
bool|null True if loaded, null otherwise

Definition at line 298 of file ClassLoader.php.

References $file, Composer\Autoload\ClassLoader\findFile(), and Composer\Autoload\includeFile().

299  {
300  if ($file = $this->findFile($class)) {
302 
303  return true;
304  }
305  }
print $file
includeFile($file)
Scope isolated include.
findFile($class)
Finds the path to the file where the class is defined.
+ Here is the call graph for this function:

◆ register()

Composer\Autoload\ClassLoader::register (   $prepend = false)

Registers this instance as an autoloader.

Parameters
bool$prependWhether to prepend the autoloader or not

Definition at line 279 of file ClassLoader.php.

280  {
281  spl_autoload_register(array($this, 'loadClass'), true, $prepend);
282  }

◆ set()

Composer\Autoload\ClassLoader::set (   $prefix,
  $paths 
)

Registers a set of PSR-0 directories for a given prefix, replacing any others previously set for this prefix.

Parameters
string$prefixThe prefix
array | string$pathsThe PSR-0 base directories

Definition at line 200 of file ClassLoader.php.

201  {
202  if (!$prefix) {
203  $this->fallbackDirsPsr0 = (array) $paths;
204  } else {
205  $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
206  }
207  }

◆ setClassMapAuthoritative()

Composer\Autoload\ClassLoader::setClassMapAuthoritative (   $classMapAuthoritative)

Turns off searching the prefix and fallback directories for classes that have not been registered with the class map.

Parameters
bool$classMapAuthoritative

Definition at line 259 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$classMapAuthoritative.

260  {
261  $this->classMapAuthoritative = $classMapAuthoritative;
262  }

◆ setPsr4()

Composer\Autoload\ClassLoader::setPsr4 (   $prefix,
  $paths 
)

Registers a set of PSR-4 directories for a given namespace, replacing any others previously set for this namespace.

Parameters
string$prefixThe prefix/namespace, with trailing '\'
array | string$pathsThe PSR-4 base directories
Exceptions

Definition at line 218 of file ClassLoader.php.

219  {
220  if (!$prefix) {
221  $this->fallbackDirsPsr4 = (array) $paths;
222  } else {
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.");
226  }
227  $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
228  $this->prefixDirsPsr4[$prefix] = (array) $paths;
229  }
230  }

◆ setUseIncludePath()

Composer\Autoload\ClassLoader::setUseIncludePath (   $useIncludePath)

Turns on searching the include path for class files.

Parameters
bool$useIncludePath

Definition at line 237 of file ClassLoader.php.

References Composer\Autoload\ClassLoader\$useIncludePath.

238  {
239  $this->useIncludePath = $useIncludePath;
240  }

◆ unregister()

Composer\Autoload\ClassLoader::unregister ( )

Unregisters this instance as an autoloader.

Definition at line 287 of file ClassLoader.php.

288  {
289  spl_autoload_unregister(array($this, 'loadClass'));
290  }

Field Documentation

◆ $classMap

Composer\Autoload\ClassLoader::$classMap = array()
private

◆ $classMapAuthoritative

Composer\Autoload\ClassLoader::$classMapAuthoritative = false
private

◆ $fallbackDirsPsr0

Composer\Autoload\ClassLoader::$fallbackDirsPsr0 = array()
private

Definition at line 52 of file ClassLoader.php.

Referenced by Composer\Autoload\ClassLoader\getFallbackDirs().

◆ $fallbackDirsPsr4

Composer\Autoload\ClassLoader::$fallbackDirsPsr4 = array()
private

Definition at line 48 of file ClassLoader.php.

Referenced by Composer\Autoload\ClassLoader\getFallbackDirsPsr4().

◆ $prefixDirsPsr4

Composer\Autoload\ClassLoader::$prefixDirsPsr4 = array()
private

Definition at line 47 of file ClassLoader.php.

Referenced by Composer\Autoload\ClassLoader\getPrefixesPsr4().

◆ $prefixesPsr0

Composer\Autoload\ClassLoader::$prefixesPsr0 = array()
private

Definition at line 51 of file ClassLoader.php.

◆ $prefixLengthsPsr4

Composer\Autoload\ClassLoader::$prefixLengthsPsr4 = array()
private

Definition at line 46 of file ClassLoader.php.

◆ $useIncludePath

Composer\Autoload\ClassLoader::$useIncludePath = false
private

The documentation for this class was generated from the following file: