ClassLoader.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * This file is based on Composer's autoloader.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * @package SqlParser
  9. * @subpackage Autoload
  10. */
  11. namespace SqlParser\Autoload;
  12. /**
  13. * ClassLoader implements a PSR-4 class loader,
  14. *
  15. * This class is loosely based on the Symfony UniversalClassLoader.
  16. * This class is a stripped version of Composer's ClassLoader.
  17. *
  18. * @package SqlParser
  19. * @subpackage Autoload
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. * @author Jordi Boggiano <j.boggiano@seld.be>
  22. */
  23. class ClassLoader
  24. {
  25. public $prefixLengths = array();
  26. public $prefixDirs = array();
  27. public $fallbackDirs = array();
  28. public $classMap = array();
  29. public $classMapAuthoritative = false;
  30. /**
  31. * @param array $classMap Class to filename map
  32. *
  33. * @return void
  34. */
  35. public function addClassMap(array $classMap)
  36. {
  37. if (!empty($this->classMap)) {
  38. $this->classMap = array_merge($this->classMap, $classMap);
  39. } else {
  40. $this->classMap = $classMap;
  41. }
  42. }
  43. /**
  44. * Registers a set of PSR-4 directories for a given namespace, either
  45. * appending or prepending to the ones previously set for this namespace.
  46. *
  47. * @param string $prefix The prefix/namespace, with trailing '\\'
  48. * @param array|string $paths The PSR-0 base directories
  49. * @param bool $prepend Whether to prepend the directories
  50. *
  51. * @throws \InvalidArgumentException
  52. *
  53. * @return void
  54. */
  55. public function add($prefix, $paths, $prepend = false)
  56. {
  57. if (!$prefix) {
  58. // Register directories for the root namespace.
  59. if ($prepend) {
  60. $this->fallbackDirs = array_merge(
  61. (array) $paths,
  62. $this->fallbackDirs
  63. );
  64. } else {
  65. $this->fallbackDirs = array_merge(
  66. $this->fallbackDirs,
  67. (array) $paths
  68. );
  69. }
  70. } elseif (!isset($this->prefixDirs[$prefix])) {
  71. // Register directories for a new namespace.
  72. $length = strlen($prefix);
  73. if ('\\' !== $prefix[$length - 1]) {
  74. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
  75. }
  76. $this->prefixLengths[$prefix[0]][$prefix] = $length;
  77. $this->prefixDirs[$prefix] = (array) $paths;
  78. } elseif ($prepend) {
  79. // Prepend directories for an already registered namespace.
  80. $this->prefixDirs[$prefix] = array_merge(
  81. (array) $paths,
  82. $this->prefixDirs[$prefix]
  83. );
  84. } else {
  85. // Append directories for an already registered namespace.
  86. $this->prefixDirs[$prefix] = array_merge(
  87. $this->prefixDirs[$prefix],
  88. (array) $paths
  89. );
  90. }
  91. }
  92. /**
  93. * Registers a set of PSR-4 directories for a given namespace,
  94. * replacing any others previously set for this namespace.
  95. *
  96. * @param string $prefix The prefix/namespace, with trailing '\\'
  97. * @param array|string $paths The PSR-4 base directories
  98. *
  99. * @throws \InvalidArgumentException
  100. *
  101. * @return void
  102. */
  103. public function set($prefix, $paths)
  104. {
  105. if (!$prefix) {
  106. $this->fallbackDirs = (array) $paths;
  107. } else {
  108. $length = strlen($prefix);
  109. if ('\\' !== $prefix[$length - 1]) {
  110. throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
  111. }
  112. $this->prefixLengths[$prefix[0]][$prefix] = $length;
  113. $this->prefixDirs[$prefix] = (array) $paths;
  114. }
  115. }
  116. /**
  117. * Registers this instance as an autoloader.
  118. *
  119. * @param bool $prepend Whether to prepend the autoloader or not
  120. *
  121. * @return void
  122. */
  123. public function register($prepend = false)
  124. {
  125. spl_autoload_register(array($this, 'loadClass'), true, $prepend);
  126. }
  127. /**
  128. * Unregisters this instance as an autoloader.
  129. *
  130. * @return void
  131. */
  132. public function unregister()
  133. {
  134. spl_autoload_unregister(array($this, 'loadClass'));
  135. }
  136. /**
  137. * Loads the given class or interface.
  138. *
  139. * @param string $class The name of the class
  140. *
  141. * @return bool|null True if loaded, null otherwise
  142. */
  143. public function loadClass($class)
  144. {
  145. if ($file = $this->findFile($class)) {
  146. includeFile($file);
  147. return true;
  148. }
  149. }
  150. /**
  151. * Finds the path to the file where the class is defined.
  152. *
  153. * @param string $class The name of the class
  154. *
  155. * @return string|false The path if found, false otherwise
  156. */
  157. public function findFile($class)
  158. {
  159. // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
  160. if ('\\' == $class[0]) {
  161. $class = substr($class, 1);
  162. }
  163. // class map lookup
  164. if (isset($this->classMap[$class])) {
  165. return $this->classMap[$class];
  166. }
  167. if ($this->classMapAuthoritative) {
  168. return false;
  169. }
  170. $file = $this->findFileWithExtension($class, '.php');
  171. // Search for Hack files if we are running on HHVM
  172. if ($file === null && defined('HHVM_VERSION')) {
  173. $file = $this->findFileWithExtension($class, '.hh');
  174. }
  175. if ($file === null) {
  176. // Remember that this class does not exist.
  177. return $this->classMap[$class] = false;
  178. }
  179. return $file;
  180. }
  181. /**
  182. * Finds a file that defines the specified class and has the specified
  183. * extension.
  184. *
  185. * @param string $class The name of the class
  186. * @param string $ext The extension of the file
  187. *
  188. * @return string|false The path if found, false otherwise
  189. */
  190. public function findFileWithExtension($class, $ext)
  191. {
  192. $logicalPath = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
  193. $first = $class[0];
  194. if (isset($this->prefixLengths[$first])) {
  195. foreach ($this->prefixLengths[$first] as $prefix => $length) {
  196. if (0 === strpos($class, $prefix)) {
  197. foreach ($this->prefixDirs[$prefix] as $dir) {
  198. if (is_file($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPath, $length))) {
  199. return $file;
  200. }
  201. }
  202. }
  203. }
  204. }
  205. foreach ($this->fallbackDirs as $dir) {
  206. if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPath)) {
  207. return $file;
  208. }
  209. }
  210. return false;
  211. }
  212. }
  213. if (!function_exists('SqlParser\\Autoload\\includeFile')) {
  214. /**
  215. * Scope isolated include.
  216. *
  217. * Prevents access to $this/self from included files.
  218. *
  219. * @param string $file The name of the file
  220. *
  221. * @return void
  222. */
  223. function includeFile($file)
  224. {
  225. include $file;
  226. }
  227. }