PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0rc4/vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 261 lines | 108 code | 17 blank | 136 comment | 8 complexity | 06c11219b44ff47a4162ad62f8108a89 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  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. * @api
  53. */
  54. class UniversalClassLoader
  55. {
  56. private $namespaces = array();
  57. private $prefixes = array();
  58. private $namespaceFallbacks = array();
  59. private $prefixFallbacks = array();
  60. /**
  61. * Gets the configured namespaces.
  62. *
  63. * @return array A hash with namespaces as keys and directories as values
  64. */
  65. public function getNamespaces()
  66. {
  67. return $this->namespaces;
  68. }
  69. /**
  70. * Gets the configured class prefixes.
  71. *
  72. * @return array A hash with class prefixes as keys and directories as values
  73. */
  74. public function getPrefixes()
  75. {
  76. return $this->prefixes;
  77. }
  78. /**
  79. * Gets the directory(ies) to use as a fallback for namespaces.
  80. *
  81. * @return array An array of directories
  82. */
  83. public function getNamespaceFallbacks()
  84. {
  85. return $this->namespaceFallbacks;
  86. }
  87. /**
  88. * Gets the directory(ies) to use as a fallback for class prefixes.
  89. *
  90. * @return array An array of directories
  91. */
  92. public function getPrefixFallbacks()
  93. {
  94. return $this->prefixFallbacks;
  95. }
  96. /**
  97. * Registers the directory to use as a fallback for namespaces.
  98. *
  99. * @param array $dirs An array of directories
  100. *
  101. * @api
  102. */
  103. public function registerNamespaceFallbacks(array $dirs)
  104. {
  105. $this->namespaceFallbacks = $dirs;
  106. }
  107. /**
  108. * Registers the directory to use as a fallback for class prefixes.
  109. *
  110. * @param array $dirs An array of directories
  111. *
  112. * @api
  113. */
  114. public function registerPrefixFallbacks(array $dirs)
  115. {
  116. $this->prefixFallbacks = $dirs;
  117. }
  118. /**
  119. * Registers an array of namespaces
  120. *
  121. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  122. *
  123. * @api
  124. */
  125. public function registerNamespaces(array $namespaces)
  126. {
  127. foreach ($namespaces as $namespace => $locations) {
  128. $this->namespaces[$namespace] = (array) $locations;
  129. }
  130. }
  131. /**
  132. * Registers a namespace.
  133. *
  134. * @param string $namespace The namespace
  135. * @param array|string $paths The location(s) of the namespace
  136. *
  137. * @api
  138. */
  139. public function registerNamespace($namespace, $paths)
  140. {
  141. $this->namespaces[$namespace] = (array) $paths;
  142. }
  143. /**
  144. * Registers an array of classes using the PEAR naming convention.
  145. *
  146. * @param array $classes An array of classes (prefixes as keys and locations as values)
  147. *
  148. * @api
  149. */
  150. public function registerPrefixes(array $classes)
  151. {
  152. foreach ($classes as $prefix => $locations) {
  153. $this->prefixes[$prefix] = (array) $locations;
  154. }
  155. }
  156. /**
  157. * Registers a set of classes using the PEAR naming convention.
  158. *
  159. * @param string $prefix The classes prefix
  160. * @param array|string $paths The location(s) of the classes
  161. *
  162. * @api
  163. */
  164. public function registerPrefix($prefix, $paths)
  165. {
  166. $this->prefixes[$prefix] = (array) $paths;
  167. }
  168. /**
  169. * Registers this instance as an autoloader.
  170. *
  171. * @param Boolean $prepend Whether to prepend the autoloader or not
  172. *
  173. * @api
  174. */
  175. public function register($prepend = false)
  176. {
  177. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  178. }
  179. /**
  180. * Loads the given class or interface.
  181. *
  182. * @param string $class The name of the class
  183. */
  184. public function loadClass($class)
  185. {
  186. if ($file = $this->findFile($class)) {
  187. require $file;
  188. }
  189. }
  190. /**
  191. * Finds the path to the file where the class is defined.
  192. *
  193. * @param string $class The name of the class
  194. *
  195. * @return string|null The path, if found
  196. */
  197. public function findFile($class)
  198. {
  199. if ('\\' == $class[0]) {
  200. $class = substr($class, 1);
  201. }
  202. if (false !== $pos = strrpos($class, '\\')) {
  203. // namespaced class name
  204. $namespace = substr($class, 0, $pos);
  205. foreach ($this->namespaces as $ns => $dirs) {
  206. foreach ($dirs as $dir) {
  207. if (0 === strpos($namespace, $ns)) {
  208. $className = substr($class, $pos + 1);
  209. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
  210. if (file_exists($file)) {
  211. return $file;
  212. }
  213. }
  214. }
  215. }
  216. foreach ($this->namespaceFallbacks as $dir) {
  217. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
  218. if (file_exists($file)) {
  219. return $file;
  220. }
  221. }
  222. } else {
  223. // PEAR-like class name
  224. foreach ($this->prefixes as $prefix => $dirs) {
  225. foreach ($dirs as $dir) {
  226. if (0 === strpos($class, $prefix)) {
  227. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  228. if (file_exists($file)) {
  229. return $file;
  230. }
  231. }
  232. }
  233. }
  234. foreach ($this->prefixFallbacks as $dir) {
  235. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  236. if (file_exists($file)) {
  237. return $file;
  238. }
  239. }
  240. }
  241. }
  242. }