vendor/symfony/symfony/src/Symfony/Component/ClassLoader/ClassLoader.php line 14

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\ClassLoader;
  11. @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);
  12. /**
  13.  * ClassLoader implements an PSR-0 class loader.
  14.  *
  15.  * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
  16.  *
  17.  *     $loader = new ClassLoader();
  18.  *
  19.  *     // register classes with namespaces
  20.  *     $loader->addPrefix('Symfony\Component', __DIR__.'/component');
  21.  *     $loader->addPrefix('Symfony',           __DIR__.'/framework');
  22.  *
  23.  *     // activate the autoloader
  24.  *     $loader->register();
  25.  *
  26.  *     // to enable searching the include path (e.g. for PEAR packages)
  27.  *     $loader->setUseIncludePath(true);
  28.  *
  29.  * In this example, if you try to use a class in the Symfony\Component
  30.  * namespace or one of its children (Symfony\Component\Console for instance),
  31.  * the autoloader will first look for the class under the component/
  32.  * directory, and it will then fallback to the framework/ directory if not
  33.  * found before giving up.
  34.  *
  35.  * @author Fabien Potencier <fabien@symfony.com>
  36.  * @author Jordi Boggiano <j.boggiano@seld.be>
  37.  *
  38.  * @deprecated since version 3.3, to be removed in 4.0.
  39.  */
  40. class ClassLoader
  41. {
  42.     private $prefixes = [];
  43.     private $fallbackDirs = [];
  44.     private $useIncludePath false;
  45.     /**
  46.      * Returns prefixes.
  47.      *
  48.      * @return array
  49.      */
  50.     public function getPrefixes()
  51.     {
  52.         return $this->prefixes;
  53.     }
  54.     /**
  55.      * Returns fallback directories.
  56.      *
  57.      * @return array
  58.      */
  59.     public function getFallbackDirs()
  60.     {
  61.         return $this->fallbackDirs;
  62.     }
  63.     /**
  64.      * Adds prefixes.
  65.      *
  66.      * @param array $prefixes Prefixes to add
  67.      */
  68.     public function addPrefixes(array $prefixes)
  69.     {
  70.         foreach ($prefixes as $prefix => $path) {
  71.             $this->addPrefix($prefix$path);
  72.         }
  73.     }
  74.     /**
  75.      * Registers a set of classes.
  76.      *
  77.      * @param string       $prefix The classes prefix
  78.      * @param array|string $paths  The location(s) of the classes
  79.      */
  80.     public function addPrefix($prefix$paths)
  81.     {
  82.         if (!$prefix) {
  83.             foreach ((array) $paths as $path) {
  84.                 $this->fallbackDirs[] = $path;
  85.             }
  86.             return;
  87.         }
  88.         if (isset($this->prefixes[$prefix])) {
  89.             if (\is_array($paths)) {
  90.                 $this->prefixes[$prefix] = array_unique(array_merge(
  91.                     $this->prefixes[$prefix],
  92.                     $paths
  93.                 ));
  94.             } elseif (!\in_array($paths$this->prefixes[$prefix])) {
  95.                 $this->prefixes[$prefix][] = $paths;
  96.             }
  97.         } else {
  98.             $this->prefixes[$prefix] = array_unique((array) $paths);
  99.         }
  100.     }
  101.     /**
  102.      * Turns on searching the include for class files.
  103.      *
  104.      * @param bool $useIncludePath
  105.      */
  106.     public function setUseIncludePath($useIncludePath)
  107.     {
  108.         $this->useIncludePath = (bool) $useIncludePath;
  109.     }
  110.     /**
  111.      * Can be used to check if the autoloader uses the include path to check
  112.      * for classes.
  113.      *
  114.      * @return bool
  115.      */
  116.     public function getUseIncludePath()
  117.     {
  118.         return $this->useIncludePath;
  119.     }
  120.     /**
  121.      * Registers this instance as an autoloader.
  122.      *
  123.      * @param bool $prepend Whether to prepend the autoloader or not
  124.      */
  125.     public function register($prepend false)
  126.     {
  127.         spl_autoload_register([$this'loadClass'], true$prepend);
  128.     }
  129.     /**
  130.      * Unregisters this instance as an autoloader.
  131.      */
  132.     public function unregister()
  133.     {
  134.         spl_autoload_unregister([$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
  142.      */
  143.     public function loadClass($class)
  144.     {
  145.         if ($file $this->findFile($class)) {
  146.             require $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|null The path, if found
  156.      */
  157.     public function findFile($class)
  158.     {
  159.         if (false !== $pos strrpos($class'\\')) {
  160.             // namespaced class name
  161.             $classPath str_replace('\\', \DIRECTORY_SEPARATORsubstr($class0$pos)).\DIRECTORY_SEPARATOR;
  162.             $className substr($class$pos 1);
  163.         } else {
  164.             // PEAR-like class name
  165.             $classPath null;
  166.             $className $class;
  167.         }
  168.         $classPath .= str_replace('_', \DIRECTORY_SEPARATOR$className).'.php';
  169.         foreach ($this->prefixes as $prefix => $dirs) {
  170.             if ($class === strstr($class$prefix)) {
  171.                 foreach ($dirs as $dir) {
  172.                     if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) {
  173.                         return $dir.\DIRECTORY_SEPARATOR.$classPath;
  174.                     }
  175.                 }
  176.             }
  177.         }
  178.         foreach ($this->fallbackDirs as $dir) {
  179.             if (file_exists($dir.\DIRECTORY_SEPARATOR.$classPath)) {
  180.                 return $dir.\DIRECTORY_SEPARATOR.$classPath;
  181.             }
  182.         }
  183.         if ($this->useIncludePath && $file stream_resolve_include_path($classPath)) {
  184.             return $file;
  185.         }
  186.     }
  187. }