123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
-
-
-
- namespace Composer\Autoload;
-
-
- class ClassLoader
- {
- private $prefixes = array();
- private $fallbackDirs = array();
- private $useIncludePath = false;
- private $classMap = array();
-
- public function getPrefixes()
- {
- return $this->prefixes;
- }
-
- public function getFallbackDirs()
- {
- return $this->fallbackDirs;
- }
-
- public function getClassMap()
- {
- return $this->classMap;
- }
-
-
-
- public function addClassMap(array $classMap)
- {
- if ($this->classMap) {
- $this->classMap = array_merge($this->classMap, $classMap);
- } else {
- $this->classMap = $classMap;
- }
- }
-
-
-
- public function add($prefix, $paths, $prepend = false)
- {
- if (!$prefix) {
- if ($prepend) {
- $this->fallbackDirs = array_merge(
- (array) $paths,
- $this->fallbackDirs
- );
- } else {
- $this->fallbackDirs = array_merge(
- $this->fallbackDirs,
- (array) $paths
- );
- }
-
- return;
- }
- if (!isset($this->prefixes[$prefix])) {
- $this->prefixes[$prefix] = (array) $paths;
-
- return;
- }
- if ($prepend) {
- $this->prefixes[$prefix] = array_merge(
- (array) $paths,
- $this->prefixes[$prefix]
- );
- } else {
- $this->prefixes[$prefix] = array_merge(
- $this->prefixes[$prefix],
- (array) $paths
- );
- }
- }
-
-
-
- public function set($prefix, $paths)
- {
- if (!$prefix) {
- $this->fallbackDirs = (array) $paths;
-
- return;
- }
- $this->prefixes[$prefix] = (array) $paths;
- }
-
-
-
- public function setUseIncludePath($useIncludePath)
- {
- $this->useIncludePath = $useIncludePath;
- }
-
-
-
- public function getUseIncludePath()
- {
- return $this->useIncludePath;
- }
-
-
-
- public function register($prepend = false)
- {
- spl_autoload_register(array($this, 'loadClass'), true, $prepend);
- }
-
-
-
- public function unregister()
- {
- spl_autoload_unregister(array($this, 'loadClass'));
- }
-
-
-
- public function loadClass($class)
- {
- if ($file = $this->findFile($class)) {
- include $file;
-
- return true;
- }
- }
-
-
-
- public function findFile($class)
- {
- if ('\\' == $class[0]) {
- $class = substr($class, 1);
- }
-
- if (isset($this->classMap[$class])) {
- return $this->classMap[$class];
- }
-
- if (false !== $pos = strrpos($class, '\\')) {
-
- $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR;
- $className = substr($class, $pos + 1);
- } else {
-
- $classPath = null;
- $className = $class;
- }
-
- $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
-
- foreach ($this->prefixes as $prefix => $dirs) {
- if (0 === strpos($class, $prefix)) {
- foreach ($dirs as $dir) {
- if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
- return $dir . DIRECTORY_SEPARATOR . $classPath;
- }
- }
- }
- }
-
- foreach ($this->fallbackDirs as $dir) {
- if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) {
- return $dir . DIRECTORY_SEPARATOR . $classPath;
- }
- }
-
- if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) {
- return $file;
- }
-
- return $this->classMap[$class] = false;
- }
- }
|