UniversalClassLoader.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. /**
  12. * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
  13. *
  14. * It is able to load classes that use either:
  15. *
  16. * * The technical interoperability standards for PHP 5.3 namespaces and
  17. * class names (http://groups.google.com/group/php-standards/web/psr-0-final-proposal);
  18. *
  19. * * The PEAR naming convention for classes (http://pear.php.net/).
  20. *
  21. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  22. * looked for in a list of locations to ease the vendoring of a sub-set of
  23. * classes for large projects.
  24. *
  25. * Example usage:
  26. *
  27. * $loader = new UniversalClassLoader();
  28. *
  29. * // register classes with namespaces
  30. * $loader->registerNamespaces(array(
  31. * 'Symfony\Component' => __DIR__.'/component',
  32. * 'Symfony' => __DIR__.'/framework',
  33. * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
  34. * ));
  35. *
  36. * // register a library using the PEAR naming convention
  37. * $loader->registerPrefixes(array(
  38. * 'Swift_' => __DIR__.'/Swift',
  39. * ));
  40. *
  41. * // activate the autoloader
  42. * $loader->register();
  43. *
  44. * In this example, if you try to use a class in the Symfony\Component
  45. * namespace or one of its children (Symfony\Component\Console for instance),
  46. * the autoloader will first look for the class under the component/
  47. * directory, and it will then fallback to the framework/ directory if not
  48. * found before giving up.
  49. *
  50. * @author Fabien Potencier <fabien@symfony.com>
  51. */
  52. class UniversalClassLoader
  53. {
  54. private $namespaces = array();
  55. private $prefixes = array();
  56. private $namespaceFallback = array();
  57. private $prefixFallback = array();
  58. /**
  59. * Gets the configured namespaces.
  60. *
  61. * @return array A hash with namespaces as keys and directories as values
  62. */
  63. public function getNamespaces()
  64. {
  65. return $this->namespaces;
  66. }
  67. /**
  68. * Gets the configured class prefixes.
  69. *
  70. * @return array A hash with class prefixes as keys and directories as values
  71. */
  72. public function getPrefixes()
  73. {
  74. return $this->prefixes;
  75. }
  76. /**
  77. * Gets the directory(ies) to use as a fallback for namespaces.
  78. *
  79. * @return array An array of directories
  80. */
  81. public function getNamespaceFallback()
  82. {
  83. return $this->namespaceFallback;
  84. }
  85. /**
  86. * Gets the directory(ies) to use as a fallback for class prefixes.
  87. *
  88. * @return array An array of directories
  89. */
  90. public function getPrefixFallback()
  91. {
  92. return $this->prefixFallback;
  93. }
  94. /**
  95. * Registers the directory to use as a fallback for namespaces.
  96. *
  97. * @param string|array $dirs A directory path or an array of directories
  98. */
  99. public function registerNamespaceFallback($dirs)
  100. {
  101. $this->namespaceFallback = (array) $dirs;
  102. }
  103. /**
  104. * Registers the directory to use as a fallback for class prefixes.
  105. *
  106. * @param string|array $dirs A directory path or an array of directories
  107. */
  108. public function registerPrefixFallback($dirs)
  109. {
  110. $this->prefixFallback = (array) $dirs;
  111. }
  112. /**
  113. * Registers an array of namespaces
  114. *
  115. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  116. */
  117. public function registerNamespaces(array $namespaces)
  118. {
  119. foreach ($namespaces as $namespace => $locations) {
  120. $this->namespaces[$namespace] = (array) $locations;
  121. }
  122. }
  123. /**
  124. * Registers a namespace.
  125. *
  126. * @param string $namespace The namespace
  127. * @param array|string $paths The location(s) of the namespace
  128. */
  129. public function registerNamespace($namespace, $paths)
  130. {
  131. $this->namespaces[$namespace] = (array) $paths;
  132. }
  133. /**
  134. * Registers an array of classes using the PEAR naming convention.
  135. *
  136. * @param array $classes An array of classes (prefixes as keys and locations as values)
  137. */
  138. public function registerPrefixes(array $classes)
  139. {
  140. foreach ($classes as $prefix => $locations) {
  141. $this->prefixes[$prefix] = (array) $locations;
  142. }
  143. }
  144. /**
  145. * Registers a set of classes using the PEAR naming convention.
  146. *
  147. * @param string $prefix The classes prefix
  148. * @param array|string $paths The location(s) of the classes
  149. */
  150. public function registerPrefix($prefix, $paths)
  151. {
  152. $this->prefixes[$prefix] = (array) $paths;
  153. }
  154. /**
  155. * Registers this instance as an autoloader.
  156. *
  157. * @param Boolean $prepend Whether to prepend the autoloader or not
  158. */
  159. public function register($prepend = false)
  160. {
  161. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  162. }
  163. /**
  164. * Loads the given class or interface.
  165. *
  166. * @param string $class The name of the class
  167. */
  168. public function loadClass($class)
  169. {
  170. if ($file = $this->findFile($class)) {
  171. require $file;
  172. }
  173. }
  174. /**
  175. * Finds the path to the file where the class is defined.
  176. *
  177. * @param string $class The name of the class
  178. *
  179. * @return string|null The path, if found
  180. */
  181. protected function findFile($class)
  182. {
  183. if ('\\' == $class[0]) {
  184. $class = substr($class, 1);
  185. }
  186. if (false !== $pos = strrpos($class, '\\')) {
  187. // namespaced class name
  188. $namespace = substr($class, 0, $pos);
  189. foreach ($this->namespaces as $ns => $dirs) {
  190. foreach ($dirs as $dir) {
  191. if (0 === strpos($namespace, $ns)) {
  192. $className = substr($class, $pos + 1);
  193. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  194. if (file_exists($file)) {
  195. return $file;
  196. }
  197. }
  198. }
  199. }
  200. foreach ($this->namespaceFallback as $dir) {
  201. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
  202. if (file_exists($file)) {
  203. return $file;
  204. }
  205. }
  206. } else {
  207. // PEAR-like class name
  208. foreach ($this->prefixes as $prefix => $dirs) {
  209. foreach ($dirs as $dir) {
  210. if (0 === strpos($class, $prefix)) {
  211. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  212. if (file_exists($file)) {
  213. return $file;
  214. }
  215. }
  216. }
  217. }
  218. foreach ($this->prefixFallback as $dir) {
  219. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  220. if (file_exists($file)) {
  221. return $file;
  222. }
  223. }
  224. }
  225. }
  226. }