ClassLoader.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\ClassLoader;
  11. @trigger_error('The '.__NAMESPACE__.'\ClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', \E_USER_DEPRECATED);
  12. /**
  13. * ClassLoader implements an PSR-0 class loader.
  14. *
  15. * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
  16. *
  17. * $loader = new ClassLoader();
  18. *
  19. * // register classes with namespaces
  20. * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
  21. * $loader->addPrefix('Symfony', __DIR__.'/framework');
  22. *
  23. * // activate the autoloader
  24. * $loader->register();
  25. *
  26. * // to enable searching the include path (e.g. for PEAR packages)
  27. * $loader->setUseIncludePath(true);
  28. *
  29. * In this example, if you try to use a class in the Symfony\Component
  30. * namespace or one of its children (Symfony\Component\Console for instance),
  31. * the autoloader will first look for the class under the component/
  32. * directory, and it will then fallback to the framework/ directory if not
  33. * found before giving up.
  34. *
  35. * @author Fabien Potencier <fabien@symfony.com>
  36. * @author Jordi Boggiano <j.boggiano@seld.be>
  37. *
  38. * @deprecated since version 3.3, to be removed in 4.0.
  39. */
  40. class ClassLoader
  41. {
  42. private $prefixes = [];
  43. private $fallbackDirs = [];
  44. private $useIncludePath = false;
  45. /**
  46. * Returns prefixes.
  47. *
  48. * @return array
  49. */
  50. public function getPrefixes()
  51. {
  52. return $this->prefixes;
  53. }
  54. /**
  55. * Returns fallback directories.
  56. *
  57. * @return array
  58. */
  59. public function getFallbackDirs()
  60. {
  61. return $this->fallbackDirs;
  62. }
  63. /**
  64. * Adds prefixes.
  65. *
  66. * @param array $prefixes Prefixes to add
  67. */
  68. public function addPrefixes(array $prefixes)
  69. {
  70. foreach ($prefixes as $prefix => $path) {
  71. $this->addPrefix($prefix, $path);
  72. }
  73. }
  74. /**
  75. * Registers a set of classes.
  76. *
  77. * @param string $prefix The classes prefix
  78. * @param array|string $paths The location(s) of the classes
  79. */
  80. public function addPrefix($prefix, $paths)
  81. {
  82. if (!$prefix) {
  83. foreach ((array) $paths as $path) {
  84. $this->fallbackDirs[] = $path;
  85. }
  86. return;
  87. }
  88. if (isset($this->prefixes[$prefix])) {
  89. if (\is_array($paths)) {
  90. $this->prefixes[$prefix] = array_unique(array_merge(
  91. $this->prefixes[$prefix],
  92. $paths
  93. ));
  94. } elseif (!\in_array($paths, $this->prefixes[$prefix])) {
  95. $this->prefixes[$prefix][] = $paths;
  96. }
  97. } else {
  98. $this->prefixes[$prefix] = array_unique((array) $paths);
  99. }
  100. }
  101. /**
  102. * Turns on searching the include for class files.
  103. *
  104. * @param bool $useIncludePath
  105. */
  106. public function setUseIncludePath($useIncludePath)
  107. {
  108. $this->useIncludePath = (bool) $useIncludePath;
  109. }
  110. /**
  111. * Can be used to check if the autoloader uses the include path to check
  112. * for classes.
  113. *
  114. * @return bool
  115. */
  116. public function getUseIncludePath()
  117. {
  118. return $this->useIncludePath;
  119. }
  120. /**
  121. * Registers this instance as an autoloader.
  122. *
  123. * @param bool $prepend Whether to prepend the autoloader or not
  124. */
  125. public function register($prepend = false)
  126. {
  127. spl_autoload_register([$this, 'loadClass'], true, $prepend);
  128. }
  129. /**
  130. * Unregisters this instance as an autoloader.
  131. */
  132. public function unregister()
  133. {
  134. spl_autoload_unregister([$this, 'loadClass']);
  135. }
  136. /**
  137. * Loads the given class or interface.
  138. *
  139. * @param string $class The name of the class
  140. *
  141. * @return bool|null True, if loaded
  142. */
  143. public function loadClass($class)
  144. {
  145. if ($file = $this->findFile($class)) {
  146. require $file;
  147. return true;
  148. }
  149. return null;
  150. }
  151. /**
  152. * Finds the path to the file where the class is defined.
  153. *
  154. * @param string $class The name of the class
  155. *
  156. * @return string|null The path, if found
  157. */
  158. public function findFile($class)
  159. {
  160. if (false !== $pos = strrpos($class, '\\')) {
  161. // namespaced class name
  162. $classPath = str_replace('\\', \DIRECTORY_SEPARATOR, substr($class, 0, $pos)).\DIRECTORY_SEPARATOR;
  163. $className = substr($class, $pos + 1);
  164. } else {
  165. // PEAR-like class name
  166. $classPath = null;
  167. $className = $class;
  168. }
  169. $classPath .= str_replace('_', \DIRECTORY_SEPARATOR, $className).'.php';
  170. foreach ($this->prefixes as $prefix => $dirs) {
  171. if ($class === strstr($class, $prefix)) {
  172. foreach ($dirs as $dir) {
  173. if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) {
  174. return $dir.\DIRECTORY_SEPARATOR.$classPath;
  175. }
  176. }
  177. }
  178. }
  179. foreach ($this->fallbackDirs as $dir) {
  180. if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) {
  181. return $dir.\DIRECTORY_SEPARATOR.$classPath;
  182. }
  183. }
  184. if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
  185. return $file;
  186. }
  187. return null;
  188. }
  189. }