UniversalClassLoader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Symfony\Component\HttpFoundation;
  3. /*
  4. * This file is part of the Symfony package.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  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. * ));
  34. *
  35. * // register a library using the PEAR naming convention
  36. * $loader->registerPrefixes(array(
  37. * 'Swift_' => __DIR__.'/Swift',
  38. * ));
  39. *
  40. * // activate the autoloader
  41. * $loader->register();
  42. *
  43. * In this example, if you try to use a class in the Symfony\Component
  44. * namespace or one of its children (Symfony\Component\Console for instance),
  45. * the autoloader will first look for the class under the component/
  46. * directory, and it will then fallback to the framework/ directory if not
  47. * found before giving up.
  48. *
  49. * @author Fabien Potencier <fabien.potencier@symfony-project.org>
  50. */
  51. class UniversalClassLoader
  52. {
  53. protected $namespaces = array();
  54. protected $prefixes = array();
  55. public function getNamespaces()
  56. {
  57. return $this->namespaces;
  58. }
  59. public function getPrefixes()
  60. {
  61. return $this->prefixes;
  62. }
  63. /**
  64. * Registers an array of namespaces
  65. *
  66. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  67. */
  68. public function registerNamespaces(array $namespaces)
  69. {
  70. $this->namespaces = array_merge($this->namespaces, $namespaces);
  71. }
  72. /**
  73. * Registers a namespace.
  74. *
  75. * @param string $namespace The namespace
  76. * @param string $path The location of the namespace
  77. */
  78. public function registerNamespace($namespace, $path)
  79. {
  80. $this->namespaces[$namespace] = $path;
  81. }
  82. /**
  83. * Registers an array of classes using the PEAR naming convention.
  84. *
  85. * @param array $classes An array of classes (prefixes as keys and locations as values)
  86. */
  87. public function registerPrefixes(array $classes)
  88. {
  89. $this->prefixes = array_merge($this->prefixes, $classes);
  90. }
  91. /**
  92. * Registers a set of classes using the PEAR naming convention.
  93. *
  94. * @param string $prefix The classes prefix
  95. * @param string $path The location of the classes
  96. */
  97. public function registerPrefix($prefix, $path)
  98. {
  99. $this->prefixes[$prefix] = $path;
  100. }
  101. /**
  102. * Registers this instance as an autoloader.
  103. */
  104. public function register()
  105. {
  106. spl_autoload_register(array($this, 'loadClass'));
  107. }
  108. /**
  109. * Loads the given class or interface.
  110. *
  111. * @param string $class The name of the class
  112. */
  113. public function loadClass($class)
  114. {
  115. if ('\\' === $class[0]) {
  116. $class = substr($class, 1);
  117. }
  118. if (false !== ($pos = strripos($class, '\\'))) {
  119. // namespaced class name
  120. $namespace = substr($class, 0, $pos);
  121. foreach ($this->namespaces as $ns => $dir) {
  122. if (0 === strpos($namespace, $ns)) {
  123. $class = substr($class, $pos + 1);
  124. $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  125. if (file_exists($file)) {
  126. require $file;
  127. }
  128. return;
  129. }
  130. }
  131. } else {
  132. // PEAR-like class name
  133. foreach ($this->prefixes as $prefix => $dir) {
  134. if (0 === strpos($class, $prefix)) {
  135. $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
  136. if (file_exists($file)) {
  137. require $file;
  138. }
  139. return;
  140. }
  141. }
  142. }
  143. }
  144. }