ClassLoader.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * A class/interface/trait autoloader for PHP
  5. *
  6. * It is able to load classes that use either:
  7. *
  8. * * The technical interoperability standards for PHP 5.3 namespaces and
  9. * class names (https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md);
  10. *
  11. * * The PEAR naming convention for classes (http://pear.php.net/).
  12. *
  13. * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
  14. * looked for in a list of locations to ease the vendoring of a sub-set of
  15. * classes for large projects.
  16. *
  17. * All discovered files are stored in the internal class map and the map is
  18. * queried before attempting to find a file.
  19. *
  20. * Contains code from Symfony2's UniversalClassLoader.
  21. *
  22. * Copyright (c) 2004-2013 Fabien Potencier
  23. *
  24. * Permission is hereby granted, free of charge, to any person obtaining a copy
  25. * of this software and associated documentation files (the "Software"), to deal
  26. * in the Software without restriction, including without limitation the rights
  27. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  28. * copies of the Software, and to permit persons to whom the Software is furnished
  29. * to do so, subject to the following conditions:
  30. *
  31. * The above copyright notice and this permission notice shall be included in all
  32. * copies or substantial portions of the Software.
  33. *
  34. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  35. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  36. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  37. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  38. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  39. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  40. * THE SOFTWARE.
  41. *
  42. * @access private
  43. *
  44. * @package Elgg.Core
  45. * @subpackage Autoloader
  46. * @author Fabien Potencier <fabien@symfony.com>
  47. */
  48. class ClassLoader {
  49. protected $namespaces = array();
  50. protected $prefixes = array();
  51. protected $fallbacks = array();
  52. /**
  53. * @var \Elgg\ClassMap Map of classes to files
  54. */
  55. protected $map;
  56. /**
  57. * Constructor
  58. *
  59. * @param \Elgg\ClassMap $map Class map
  60. */
  61. public function __construct(\Elgg\ClassMap $map) {
  62. $this->map = $map;
  63. }
  64. /**
  65. * Get the class map
  66. *
  67. * @return \Elgg\ClassMap
  68. */
  69. public function getClassMap() {
  70. return $this->map;
  71. }
  72. /**
  73. * Gets the configured namespaces.
  74. *
  75. * @return array A hash with namespaces as keys and directories as values
  76. */
  77. public function getNamespaces() {
  78. return $this->namespaces;
  79. }
  80. /**
  81. * Gets the configured class prefixes.
  82. *
  83. * @return array A hash with class prefixes as keys and directories as values
  84. */
  85. public function getPrefixes() {
  86. return $this->prefixes;
  87. }
  88. /**
  89. * Registers an array of namespaces
  90. *
  91. * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
  92. * @return void
  93. */
  94. public function registerNamespaces(array $namespaces) {
  95. foreach ($namespaces as $namespace => $locations) {
  96. $this->namespaces[$namespace] = (array)$locations;
  97. }
  98. }
  99. /**
  100. * Registers a namespace.
  101. *
  102. * @param string $namespace The namespace
  103. * @param array|string $paths The location(s) of the namespace
  104. * @return void
  105. */
  106. public function registerNamespace($namespace, $paths) {
  107. $this->namespaces[$namespace] = (array)$paths;
  108. }
  109. /**
  110. * Registers an array of classes using the PEAR naming convention.
  111. *
  112. * @param array $classes An array of classes (prefixes as keys and locations as values)
  113. * @return void
  114. */
  115. public function registerPrefixes(array $classes) {
  116. foreach ($classes as $prefix => $locations) {
  117. $this->prefixes[$prefix] = (array)$locations;
  118. }
  119. }
  120. /**
  121. * Registers a set of classes using the PEAR naming convention.
  122. *
  123. * @param string $prefix The classes prefix
  124. * @param array|string $paths The location(s) of the classes
  125. * @return void
  126. */
  127. public function registerPrefix($prefix, $paths) {
  128. $this->prefixes[$prefix] = (array)$paths;
  129. }
  130. /**
  131. * Add a directory to search if no registered directory is found.
  132. *
  133. * @param string $path The directory
  134. * @return void
  135. */
  136. public function addFallback($path) {
  137. $this->fallbacks[] = rtrim($path, '/\\');
  138. }
  139. /**
  140. * Registers this instance as an autoloader.
  141. *
  142. * @return void
  143. */
  144. public function register() {
  145. spl_autoload_register(array($this, 'loadClass'));
  146. }
  147. /**
  148. * Loads the given class or interface, possibly updating the class map.
  149. *
  150. * @param string $class The name of the class
  151. * @return void
  152. */
  153. public function loadClass($class) {
  154. $file = $this->map->getPath($class);
  155. if ($file && is_readable($file)) {
  156. require $file;
  157. return;
  158. }
  159. $file = $this->findFile($class);
  160. if ($file && is_readable($file)) {
  161. $this->map->setPath($class, $file);
  162. $this->map->setAltered(true);
  163. require $file;
  164. }
  165. }
  166. /**
  167. * Finds the path to the file where the class is defined.
  168. *
  169. * @param string $class The name of the class
  170. *
  171. * @return string|null The path, if found
  172. */
  173. public function findFile($class) {
  174. if ('\\' == $class[0]) {
  175. $class = substr($class, 1);
  176. }
  177. $pos = strrpos($class, '\\');
  178. if (false !== $pos) {
  179. // namespaced class name
  180. $namespace = substr($class, 0, $pos);
  181. $className = substr($class, $pos + 1);
  182. $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace)
  183. . DIRECTORY_SEPARATOR
  184. . str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  185. foreach ($this->namespaces as $ns => $dirs) {
  186. if (0 !== strpos($namespace, $ns)) {
  187. continue;
  188. }
  189. foreach ($dirs as $dir) {
  190. $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
  191. if (is_file($file)) {
  192. return $file;
  193. }
  194. }
  195. }
  196. } else {
  197. // PEAR-like class name
  198. $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php';
  199. foreach ($this->prefixes as $prefix => $dirs) {
  200. if (0 !== strpos($class, $prefix)) {
  201. continue;
  202. }
  203. foreach ($dirs as $dir) {
  204. $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
  205. if (is_file($file)) {
  206. return $file;
  207. }
  208. }
  209. }
  210. }
  211. foreach ($this->fallbacks as $dir) {
  212. $file = $dir . DIRECTORY_SEPARATOR . $normalizedClass;
  213. if (is_file($file)) {
  214. return $file;
  215. }
  216. }
  217. }
  218. }