Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\ClassLoader; Chris@0: Chris@14: @trigger_error('The '.__NAMESPACE__.'\ClassLoader class is deprecated since Symfony 3.3 and will be removed in 4.0. Use Composer instead.', E_USER_DEPRECATED); Chris@14: Chris@0: /** Chris@0: * ClassLoader implements an PSR-0 class loader. Chris@0: * Chris@0: * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md Chris@0: * Chris@0: * $loader = new ClassLoader(); Chris@0: * Chris@0: * // register classes with namespaces Chris@0: * $loader->addPrefix('Symfony\Component', __DIR__.'/component'); Chris@0: * $loader->addPrefix('Symfony', __DIR__.'/framework'); Chris@0: * Chris@0: * // activate the autoloader Chris@0: * $loader->register(); Chris@0: * Chris@0: * // to enable searching the include path (e.g. for PEAR packages) Chris@0: * $loader->setUseIncludePath(true); Chris@0: * Chris@0: * In this example, if you try to use a class in the Symfony\Component Chris@0: * namespace or one of its children (Symfony\Component\Console for instance), Chris@0: * the autoloader will first look for the class under the component/ Chris@0: * directory, and it will then fallback to the framework/ directory if not Chris@0: * found before giving up. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: * @author Jordi Boggiano Chris@14: * Chris@14: * @deprecated since version 3.3, to be removed in 4.0. Chris@0: */ Chris@0: class ClassLoader Chris@0: { Chris@17: private $prefixes = []; Chris@17: private $fallbackDirs = []; Chris@0: private $useIncludePath = false; Chris@0: Chris@0: /** Chris@0: * Returns prefixes. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getPrefixes() Chris@0: { Chris@0: return $this->prefixes; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns fallback directories. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function getFallbackDirs() Chris@0: { Chris@0: return $this->fallbackDirs; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds prefixes. Chris@0: * Chris@0: * @param array $prefixes Prefixes to add Chris@0: */ Chris@0: public function addPrefixes(array $prefixes) Chris@0: { Chris@0: foreach ($prefixes as $prefix => $path) { Chris@0: $this->addPrefix($prefix, $path); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers a set of classes. Chris@0: * Chris@0: * @param string $prefix The classes prefix Chris@0: * @param array|string $paths The location(s) of the classes Chris@0: */ Chris@0: public function addPrefix($prefix, $paths) Chris@0: { Chris@0: if (!$prefix) { Chris@0: foreach ((array) $paths as $path) { Chris@0: $this->fallbackDirs[] = $path; Chris@0: } Chris@0: Chris@0: return; Chris@0: } Chris@0: if (isset($this->prefixes[$prefix])) { Chris@17: if (\is_array($paths)) { Chris@0: $this->prefixes[$prefix] = array_unique(array_merge( Chris@0: $this->prefixes[$prefix], Chris@0: $paths Chris@0: )); Chris@17: } elseif (!\in_array($paths, $this->prefixes[$prefix])) { Chris@0: $this->prefixes[$prefix][] = $paths; Chris@0: } Chris@0: } else { Chris@0: $this->prefixes[$prefix] = array_unique((array) $paths); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Turns on searching the include for class files. Chris@0: * Chris@0: * @param bool $useIncludePath Chris@0: */ Chris@0: public function setUseIncludePath($useIncludePath) Chris@0: { Chris@0: $this->useIncludePath = (bool) $useIncludePath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Can be used to check if the autoloader uses the include path to check Chris@0: * for classes. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function getUseIncludePath() Chris@0: { Chris@0: return $this->useIncludePath; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Registers this instance as an autoloader. Chris@0: * Chris@0: * @param bool $prepend Whether to prepend the autoloader or not Chris@0: */ Chris@0: public function register($prepend = false) Chris@0: { Chris@17: spl_autoload_register([$this, 'loadClass'], true, $prepend); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Unregisters this instance as an autoloader. Chris@0: */ Chris@0: public function unregister() Chris@0: { Chris@17: spl_autoload_unregister([$this, 'loadClass']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loads the given class or interface. Chris@0: * Chris@0: * @param string $class The name of the class Chris@0: * Chris@0: * @return bool|null True, if loaded Chris@0: */ Chris@0: public function loadClass($class) Chris@0: { Chris@0: if ($file = $this->findFile($class)) { Chris@0: require $file; Chris@0: Chris@0: return true; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Finds the path to the file where the class is defined. Chris@0: * Chris@0: * @param string $class The name of the class Chris@0: * Chris@0: * @return string|null The path, if found Chris@0: */ Chris@0: public function findFile($class) Chris@0: { Chris@0: if (false !== $pos = strrpos($class, '\\')) { Chris@0: // namespaced class name Chris@17: $classPath = str_replace('\\', \DIRECTORY_SEPARATOR, substr($class, 0, $pos)).\DIRECTORY_SEPARATOR; Chris@0: $className = substr($class, $pos + 1); Chris@0: } else { Chris@0: // PEAR-like class name Chris@0: $classPath = null; Chris@0: $className = $class; Chris@0: } Chris@0: Chris@17: $classPath .= str_replace('_', \DIRECTORY_SEPARATOR, $className).'.php'; Chris@0: Chris@0: foreach ($this->prefixes as $prefix => $dirs) { Chris@0: if ($class === strstr($class, $prefix)) { Chris@0: foreach ($dirs as $dir) { Chris@17: if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) { Chris@17: return $dir.\DIRECTORY_SEPARATOR.$classPath; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: foreach ($this->fallbackDirs as $dir) { Chris@17: if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) { Chris@17: return $dir.\DIRECTORY_SEPARATOR.$classPath; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { Chris@0: return $file; Chris@0: } Chris@0: } Chris@0: }