Drupal investigation

UniversalClassLoader.php 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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__.'\UniversalClassLoader class is deprecated since version 2.7 and will be removed in 3.0. Use the Symfony\Component\ClassLoader\ClassLoader class instead.', E_USER_DEPRECATED);
  12. /**
  13. * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
  14. *
  15. * It is able to load classes that use either:
  16. *
  17. * * The technical interoperability standards for PHP 5.3 namespaces and
  18. * class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
  19. *
  20. * * The PEAR naming convention for classes (http://pear.php.net/).
  21. *
  22. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  23. * looked for in a list of locations to ease the vendoring of a sub-set of
  24. * classes for large projects.
  25. *
  26. * Example usage:
  27. *
  28. * $loader = new UniversalClassLoader();
  29. *
  30. * // register classes with namespaces
  31. * $loader->registerNamespaces(array(
  32. * 'Symfony\Component' => __DIR__.'/component',
  33. * 'Symfony' => __DIR__.'/framework',
  34. * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
  35. * ));
  36. *
  37. * // register a library using the PEAR naming convention
  38. * $loader->registerPrefixes(array(
  39. * 'Swift_' => __DIR__.'/Swift',
  40. * ));
  41. *
  42. *
  43. * // to enable searching the include path (e.g. for PEAR packages)
  44. * $loader->useIncludePath(true);
  45. *
  46. * // activate the autoloader
  47. * $loader->register();
  48. *
  49. * In this example, if you try to use a class in the Symfony\Component
  50. * namespace or one of its children (Symfony\Component\Console for instance),
  51. * the autoloader will first look for the class under the component/
  52. * directory, and it will then fallback to the framework/ directory if not
  53. * found before giving up.
  54. *
  55. * @author Fabien Potencier <fabien@symfony.com>
  56. *
  57. * @deprecated since version 2.4, to be removed in 3.0.
  58. * Use the {@link ClassLoader} class instead.
  59. */
  60. class UniversalClassLoader
  61. {
  62. private $namespaces = array();
  63. private $prefixes = array();
  64. private $namespaceFallbacks = array();
  65. private $prefixFallbacks = array();
  66. private $useIncludePath = false;
  67. /**
  68. * Turns on searching the include for class files. Allows easy loading
  69. * of installed PEAR packages.
  70. *
  71. * @param bool $useIncludePath
  72. */
  73. public function useIncludePath($useIncludePath)
  74. {
  75. $this->useIncludePath = (bool) $useIncludePath;
  76. }
  77. /**
  78. * Can be used to check if the autoloader uses the include path to check
  79. * for classes.
  80. *
  81. * @return bool
  82. */
  83. public function getUseIncludePath()
  84. {
  85. return $this->useIncludePath;
  86. }
  87. /**
  88. * Gets the configured namespaces.
  89. *
  90. * @return array A hash with namespaces as keys and directories as values
  91. */
  92. public function getNamespaces()
  93. {
  94. return $this->namespaces;
  95. }
  96. /**
  97. * Gets the configured class prefixes.
  98. *
  99. * @return array A hash with class prefixes as keys and directories as values
  100. */
  101. public function getPrefixes()
  102. {
  103. return $this->prefixes;
  104. }
  105. /**
  106. * Gets the directory(ies) to use as a fallback for namespaces.
  107. *
  108. * @return array An array of directories
  109. */
  110. public function getNamespaceFallbacks()
  111. {
  112. return $this->namespaceFallbacks;
  113. }
  114. /**
  115. * Gets the directory(ies) to use as a fallback for class prefixes.
  116. *
  117. * @return array An array of directories
  118. */
  119. public function getPrefixFallbacks()
  120. {
  121. return $this->prefixFallbacks;
  122. }
  123. /**
  124. * Registers the directory to use as a fallback for namespaces.
  125. *
  126. * @param array $dirs An array of directories
  127. */
  128. public function registerNamespaceFallbacks(array $dirs)
  129. {
  130. $this->namespaceFallbacks = $dirs;
  131. }
  132. /**
  133. * Registers a directory to use as a fallback for namespaces.
  134. *
  135. * @param string $dir A directory
  136. */
  137. public function registerNamespaceFallback($dir)
  138. {
  139. $this->namespaceFallbacks[] = $dir;
  140. }
  141. /**
  142. * Registers directories to use as a fallback for class prefixes.
  143. *
  144. * @param array $dirs An array of directories
  145. */
  146. public function registerPrefixFallbacks(array $dirs)
  147. {
  148. $this->prefixFallbacks = $dirs;
  149. }
  150. /**
  151. * Registers a directory to use as a fallback for class prefixes.
  152. *
  153. * @param string $dir A directory
  154. */
  155. public function registerPrefixFallback($dir)
  156. {
  157. $this->prefixFallbacks[] = $dir;
  158. }
  159. /**
  160. * Registers an array of namespaces.
  161. *
  162. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  163. */
  164. public function registerNamespaces(array $namespaces)
  165. {
  166. foreach ($namespaces as $namespace => $locations) {
  167. $this->namespaces[$namespace] = (array) $locations;
  168. }
  169. }
  170. /**
  171. * Registers a namespace.
  172. *
  173. * @param string $namespace The namespace
  174. * @param array|string $paths The location(s) of the namespace
  175. */
  176. public function registerNamespace($namespace, $paths)
  177. {
  178. $this->namespaces[$namespace] = (array) $paths;
  179. }
  180. /**
  181. * Registers an array of classes using the PEAR naming convention.
  182. *
  183. * @param array $classes An array of classes (prefixes as keys and locations as values)
  184. */
  185. public function registerPrefixes(array $classes)
  186. {
  187. foreach ($classes as $prefix => $locations) {
  188. $this->prefixes[$prefix] = (array) $locations;
  189. }
  190. }
  191. /**
  192. * Registers a set of classes using the PEAR naming convention.
  193. *
  194. * @param string $prefix The classes prefix
  195. * @param array|string $paths The location(s) of the classes
  196. */
  197. public function registerPrefix($prefix, $paths)
  198. {
  199. $this->prefixes[$prefix] = (array) $paths;
  200. }
  201. /**
  202. * Registers this instance as an autoloader.
  203. *
  204. * @param bool $prepend Whether to prepend the autoloader or not
  205. */
  206. public function register($prepend = false)
  207. {
  208. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  209. }
  210. /**
  211. * Loads the given class or interface.
  212. *
  213. * @param string $class The name of the class
  214. *
  215. * @return bool|null True, if loaded
  216. */
  217. public function loadClass($class)
  218. {
  219. if ($file = $this->findFile($class)) {
  220. require $file;
  221. return true;
  222. }
  223. }
  224. /**
  225. * Finds the path to the file where the class is defined.
  226. *
  227. * @param string $class The name of the class
  228. *
  229. * @return string|null The path, if found
  230. */
  231. public function findFile($class)
  232. {
  233. if (false !== $pos = strrpos($class, '\\')) {
  234. // namespaced class name
  235. $namespace = substr($class, 0, $pos);
  236. $className = substr($class, $pos + 1);
  237. $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  238. foreach ($this->namespaces as $ns => $dirs) {
  239. if (0 !== strpos($namespace, $ns)) {
  240. continue;
  241. }
  242. foreach ($dirs as $dir) {
  243. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  244. if (is_file($file)) {
  245. return $file;
  246. }
  247. }
  248. }
  249. foreach ($this->namespaceFallbacks as $dir) {
  250. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  251. if (is_file($file)) {
  252. return $file;
  253. }
  254. }
  255. } else {
  256. // PEAR-like class name
  257. $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  258. foreach ($this->prefixes as $prefix => $dirs) {
  259. if (0 !== strpos($class, $prefix)) {
  260. continue;
  261. }
  262. foreach ($dirs as $dir) {
  263. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  264. if (is_file($file)) {
  265. return $file;
  266. }
  267. }
  268. }
  269. foreach ($this->prefixFallbacks as $dir) {
  270. $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
  271. if (is_file($file)) {
  272. return $file;
  273. }
  274. }
  275. }
  276. if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
  277. return $file;
  278. }
  279. }
  280. }