xrstf_Composer52_ClassLoader

Copyright (c) 2013, Christoph Mewes, http://www.xrstf.de.

Defined (2)

The class is defined in the following location(s).

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