/wp-content/plugins/wordpress-seo/vendor/composer/ClassLoader52.php

https://bitbucket.org/carloskikea/helpet · PHP · 271 lines · 149 code · 35 blank · 87 comment · 22 complexity · c8b909e2a65a5ecec42bc17d69f47f98 MD5 · raw file

  1. <?php
  2. /*
  3. * Copyright (c) 2013, Christoph Mewes, http://www.xrstf.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. *
  10. * --------------------------------------------------------------------------
  11. *
  12. * 99% of this is copied as-is from the original Composer source code and is
  13. * released under MIT license as well. Copyright goes to:
  14. *
  15. * - Fabien Potencier <fabien@symfony.com>
  16. * - Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class xrstf_Composer52_ClassLoader {
  19. private $prefixes = array();
  20. private $fallbackDirs = array();
  21. private $useIncludePath = false;
  22. private $classMap = array();
  23. private $classMapAuthoratative = false;
  24. private $allowUnderscore = false;
  25. /**
  26. * @param boolean $flag true to allow class names with a leading underscore, false to disable
  27. */
  28. public function setAllowUnderscore($flag) {
  29. $this->allowUnderscore = (boolean) $flag;
  30. }
  31. /**
  32. * @return array
  33. */
  34. public function getPrefixes() {
  35. return $this->prefixes;
  36. }
  37. /**
  38. * Turns off searching the prefix and fallback directories for classes
  39. * that have not been registered with the class map.
  40. *
  41. * @param bool $classMapAuthoratative
  42. */
  43. public function setClassMapAuthoritative($classMapAuthoratative) {
  44. $this->classMapAuthoratative = $classMapAuthoratative;
  45. }
  46. /**
  47. * Should class lookup fail if not found in the current class map?
  48. *
  49. * @return bool
  50. */
  51. public function getClassMapAuthoratative() {
  52. return $this->classMapAuthoratative;
  53. }
  54. /**
  55. * @return array
  56. */
  57. public function getFallbackDirs() {
  58. return $this->fallbackDirs;
  59. }
  60. /**
  61. * @return array
  62. */
  63. public function getClassMap() {
  64. return $this->classMap;
  65. }
  66. /**
  67. * @param array $classMap class to filename map
  68. */
  69. public function addClassMap(array $classMap) {
  70. if ($this->classMap) {
  71. $this->classMap = array_merge($this->classMap, $classMap);
  72. }
  73. else {
  74. $this->classMap = $classMap;
  75. }
  76. }
  77. /**
  78. * Registers a set of classes, merging with any others previously set.
  79. *
  80. * @param string $prefix the classes prefix
  81. * @param array|string $paths the location(s) of the classes
  82. * @param bool $prepend prepend the location(s)
  83. */
  84. public function add($prefix, $paths, $prepend = false) {
  85. if (!$prefix) {
  86. if ($prepend) {
  87. $this->fallbackDirs = array_merge(
  88. (array) $paths,
  89. $this->fallbackDirs
  90. );
  91. }
  92. else {
  93. $this->fallbackDirs = array_merge(
  94. $this->fallbackDirs,
  95. (array) $paths
  96. );
  97. }
  98. return;
  99. }
  100. if (!isset($this->prefixes[$prefix])) {
  101. $this->prefixes[$prefix] = (array) $paths;
  102. return;
  103. }
  104. if ($prepend) {
  105. $this->prefixes[$prefix] = array_merge(
  106. (array) $paths,
  107. $this->prefixes[$prefix]
  108. );
  109. }
  110. else {
  111. $this->prefixes[$prefix] = array_merge(
  112. $this->prefixes[$prefix],
  113. (array) $paths
  114. );
  115. }
  116. }
  117. /**
  118. * Registers a set of classes, replacing any others previously set.
  119. *
  120. * @param string $prefix the classes prefix
  121. * @param array|string $paths the location(s) of the classes
  122. */
  123. public function set($prefix, $paths) {
  124. if (!$prefix) {
  125. $this->fallbackDirs = (array) $paths;
  126. return;
  127. }
  128. $this->prefixes[$prefix] = (array) $paths;
  129. }
  130. /**
  131. * Turns on searching the include path for class files.
  132. *
  133. * @param bool $useIncludePath
  134. */
  135. public function setUseIncludePath($useIncludePath) {
  136. $this->useIncludePath = $useIncludePath;
  137. }
  138. /**
  139. * Can be used to check if the autoloader uses the include path to check
  140. * for classes.
  141. *
  142. * @return bool
  143. */
  144. public function getUseIncludePath() {
  145. return $this->useIncludePath;
  146. }
  147. /**
  148. * Registers this instance as an autoloader.
  149. */
  150. public function register() {
  151. spl_autoload_register(array($this, 'loadClass'), true);
  152. }
  153. /**
  154. * Unregisters this instance as an autoloader.
  155. */
  156. public function unregister() {
  157. spl_autoload_unregister(array($this, 'loadClass'));
  158. }
  159. /**
  160. * Loads the given class or interface.
  161. *
  162. * @param string $class the name of the class
  163. * @return bool|null true, if loaded
  164. */
  165. public function loadClass($class) {
  166. if ($file = $this->findFile($class)) {
  167. include $file;
  168. return true;
  169. }
  170. }
  171. /**
  172. * Finds the path to the file where the class is defined.
  173. *
  174. * @param string $class the name of the class
  175. * @return string|null the path, if found
  176. */
  177. public function findFile($class) {
  178. if ('\\' === $class[0]) {
  179. $class = substr($class, 1);
  180. }
  181. if (isset($this->classMap[$class])) {
  182. return $this->classMap[$class];
  183. }
  184. elseif ($this->classMapAuthoratative) {
  185. return false;
  186. }
  187. $classPath = $this->getClassPath($class);
  188. foreach ($this->prefixes as $prefix => $dirs) {
  189. if (0 === strpos($class, $prefix)) {
  190. foreach ($dirs as $dir) {
  191. if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
  192. return $dir.DIRECTORY_SEPARATOR.$classPath;
  193. }
  194. }
  195. }
  196. }
  197. foreach ($this->fallbackDirs as $dir) {
  198. if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) {
  199. return $dir.DIRECTORY_SEPARATOR.$classPath;
  200. }
  201. }
  202. if ($this->useIncludePath && $file = self::resolveIncludePath($classPath)) {
  203. return $file;
  204. }
  205. return $this->classMap[$class] = false;
  206. }
  207. private function getClassPath($class) {
  208. if (false !== $pos = strrpos($class, '\\')) {
  209. // namespaced class name
  210. $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)).DIRECTORY_SEPARATOR;
  211. $className = substr($class, $pos + 1);
  212. }
  213. else {
  214. // PEAR-like class name
  215. $classPath = null;
  216. $className = $class;
  217. }
  218. $className = str_replace('_', DIRECTORY_SEPARATOR, $className);
  219. // restore the prefix
  220. if ($this->allowUnderscore && DIRECTORY_SEPARATOR === $className[0]) {
  221. $className[0] = '_';
  222. }
  223. $classPath .= $className.'.php';
  224. return $classPath;
  225. }
  226. public static function resolveIncludePath($classPath) {
  227. $paths = explode(PATH_SEPARATOR, get_include_path());
  228. foreach ($paths as $path) {
  229. $path = rtrim($path, '/\\');
  230. if ($file = file_exists($path.DIRECTORY_SEPARATOR.$file)) {
  231. return $file;
  232. }
  233. }
  234. return false;
  235. }
  236. }