UniversalClassLoader.php 4.6 KB

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