vendor/symfony/symfony/src/Symfony/Component/Debug/DebugClassLoader.php line 143

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  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\Debug;
  11. /**
  12.  * Autoloader checking if the class is really defined in the file found.
  13.  *
  14.  * The ClassLoader will wrap all registered autoloaders
  15.  * and will throw an exception if a file is found but does
  16.  * not declare the class.
  17.  *
  18.  * @author Fabien Potencier <[email protected]>
  19.  * @author Christophe Coevoet <[email protected]>
  20.  * @author Nicolas Grekas <[email protected]>
  21.  */
  22. class DebugClassLoader
  23. {
  24.     private $classLoader;
  25.     private $isFinder;
  26.     private $loaded = array();
  27.     private static $caseCheck;
  28.     private static $final = array();
  29.     private static $finalMethods = array();
  30.     private static $deprecated = array();
  31.     private static $internal = array();
  32.     private static $internalMethods = array();
  33.     private static $php7Reserved = array('int''float''bool''string''true''false''null');
  34.     private static $darwinCache = array('/' => array('/', array()));
  35.     public function __construct(callable $classLoader)
  36.     {
  37.         $this->classLoader $classLoader;
  38.         $this->isFinder is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  39.         if (!isset(self::$caseCheck)) {
  40.             $file file_exists(__FILE__) ? __FILE__ rtrim(realpath('.'), DIRECTORY_SEPARATOR);
  41.             $i strrpos($fileDIRECTORY_SEPARATOR);
  42.             $dir substr($file0$i);
  43.             $file substr($file$i);
  44.             $test strtoupper($file) === $file strtolower($file) : strtoupper($file);
  45.             $test realpath($dir.$test);
  46.             if (false === $test || false === $i) {
  47.                 // filesystem is case sensitive
  48.                 self::$caseCheck 0;
  49.             } elseif (substr($test, -strlen($file)) === $file) {
  50.                 // filesystem is case insensitive and realpath() normalizes the case of characters
  51.                 self::$caseCheck 1;
  52.             } elseif (false !== stripos(PHP_OS'darwin')) {
  53.                 // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
  54.                 self::$caseCheck 2;
  55.             } else {
  56.                 // filesystem case checks failed, fallback to disabling them
  57.                 self::$caseCheck 0;
  58.             }
  59.         }
  60.     }
  61.     /**
  62.      * Gets the wrapped class loader.
  63.      *
  64.      * @return callable The wrapped class loader
  65.      */
  66.     public function getClassLoader()
  67.     {
  68.         return $this->classLoader;
  69.     }
  70.     /**
  71.      * Wraps all autoloaders.
  72.      */
  73.     public static function enable()
  74.     {
  75.         // Ensures we don't hit https://bugs.php.net/42098
  76.         class_exists('Symfony\Component\Debug\ErrorHandler');
  77.         class_exists('Psr\Log\LogLevel');
  78.         if (!is_array($functions spl_autoload_functions())) {
  79.             return;
  80.         }
  81.         foreach ($functions as $function) {
  82.             spl_autoload_unregister($function);
  83.         }
  84.         foreach ($functions as $function) {
  85.             if (!is_array($function) || !$function[0] instanceof self) {
  86.                 $function = array(new static($function), 'loadClass');
  87.             }
  88.             spl_autoload_register($function);
  89.         }
  90.     }
  91.     /**
  92.      * Disables the wrapping.
  93.      */
  94.     public static function disable()
  95.     {
  96.         if (!is_array($functions spl_autoload_functions())) {
  97.             return;
  98.         }
  99.         foreach ($functions as $function) {
  100.             spl_autoload_unregister($function);
  101.         }
  102.         foreach ($functions as $function) {
  103.             if (is_array($function) && $function[0] instanceof self) {
  104.                 $function $function[0]->getClassLoader();
  105.             }
  106.             spl_autoload_register($function);
  107.         }
  108.     }
  109.     /**
  110.      * Loads the given class or interface.
  111.      *
  112.      * @param string $class The name of the class
  113.      *
  114.      * @return bool|null True, if loaded
  115.      *
  116.      * @throws \RuntimeException
  117.      */
  118.     public function loadClass($class)
  119.     {
  120.         $e error_reporting(error_reporting() | E_PARSE E_ERROR E_CORE_ERROR E_COMPILE_ERROR);
  121.         try {
  122.             if ($this->isFinder && !isset($this->loaded[$class])) {
  123.                 $this->loaded[$class] = true;
  124.                 if ($file $this->classLoader[0]->findFile($class)) {
  125.                     require $file;
  126.                 }
  127.             } else {
  128.                 call_user_func($this->classLoader$class);
  129.                 $file false;
  130.             }
  131.         } finally {
  132.             error_reporting($e);
  133.         }
  134.         $exists class_exists($classfalse) || interface_exists($classfalse) || trait_exists($classfalse);
  135.         if ($class && '\\' === $class[0]) {
  136.             $class substr($class1);
  137.         }
  138.         if ($exists) {
  139.             $refl = new \ReflectionClass($class);
  140.             $name $refl->getName();
  141.             if ($name !== $class && === strcasecmp($name$class)) {
  142.                 throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: "%s" vs "%s".'$class$name));
  143.             }
  144.             // Don't trigger deprecations for classes in the same vendor
  145.             if ($len + (strpos($name'\\') ?: strpos($name'_'))) {
  146.                 $len 0;
  147.                 $ns '';
  148.             } else {
  149.                 $ns substr($name0$len);
  150.             }
  151.             // Detect annotations on the class
  152.             if (false !== $doc $refl->getDocComment()) {
  153.                 foreach (array('final''deprecated''internal') as $annotation) {
  154.                     if (false !== strpos($doc'@'.$annotation) && preg_match('#\n \* @'.$annotation.'(?:( .+?)\.?)?\r?\n \*(?: @|/$)#s'$doc$notice)) {
  155.                         self::${$annotation}[$name] = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#'' '$notice[1]) : '';
  156.                     }
  157.                 }
  158.             }
  159.             $parentAndTraits class_uses($namefalse);
  160.             if ($parent get_parent_class($class)) {
  161.                 $parentAndTraits[] = $parent;
  162.                 if (isset(self::$final[$parent])) {
  163.                     @trigger_error(sprintf('The "%s" class is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".'$parentself::$final[$parent], $name), E_USER_DEPRECATED);
  164.                 }
  165.             }
  166.             // Detect if the parent is annotated
  167.             foreach ($parentAndTraits $this->getOwnInterfaces($name$parent) as $use) {
  168.                 if (isset(self::$deprecated[$use]) && strncmp($ns$use$len)) {
  169.                     $type class_exists($namefalse) ? 'class' : (interface_exists($namefalse) ? 'interface' 'trait');
  170.                     $verb class_exists($usefalse) || interface_exists($namefalse) ? 'extends' : (interface_exists($usefalse) ? 'implements' 'uses');
  171.                     @trigger_error(sprintf('The "%s" %s %s "%s" that is deprecated%s.'$name$type$verb$useself::$deprecated[$use]), E_USER_DEPRECATED);
  172.                 }
  173.                 if (isset(self::$internal[$use]) && strncmp($ns$use$len)) {
  174.                     @trigger_error(sprintf('The "%s" %s is considered internal%s. It may change without further notice. You should not use it from "%s".'$useclass_exists($usefalse) ? 'class' : (interface_exists($usefalse) ? 'interface' 'trait'), self::$internal[$use], $name), E_USER_DEPRECATED);
  175.                 }
  176.             }
  177.             // Inherit @final and @internal annotations for methods
  178.             self::$finalMethods[$name] = array();
  179.             self::$internalMethods[$name] = array();
  180.             foreach ($parentAndTraits as $use) {
  181.                 foreach (array('finalMethods''internalMethods') as $property) {
  182.                     if (isset(self::${$property}[$use])) {
  183.                         self::${$property}[$name] = array_merge(self::${$property}[$name], self::${$property}[$use]);
  184.                     }
  185.                 }
  186.             }
  187.             $isClass class_exists($namefalse);
  188.             foreach ($refl->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED) as $method) {
  189.                 if ($method->class !== $name) {
  190.                     continue;
  191.                 }
  192.                 // Method from a trait
  193.                 if ($method->getFilename() !== $refl->getFileName()) {
  194.                     continue;
  195.                 }
  196.                 if ($isClass && $parent && isset(self::$finalMethods[$parent][$method->name])) {
  197.                     list($declaringClass$message) = self::$finalMethods[$parent][$method->name];
  198.                     @trigger_error(sprintf('The "%s::%s()" method is considered final%s. It may change without further notice as of its next major version. You should not extend it from "%s".'$declaringClass$method->name$message$name), E_USER_DEPRECATED);
  199.                 }
  200.                 foreach ($parentAndTraits as $use) {
  201.                     if (isset(self::$internalMethods[$use][$method->name])) {
  202.                         list($declaringClass$message) = self::$internalMethods[$use][$method->name];
  203.                         if (strncmp($ns$declaringClass$len)) {
  204.                             @trigger_error(sprintf('The "%s::%s()" method is considered internal%s. It may change without further notice. You should not extend it from "%s".'$declaringClass$method->name$message$name), E_USER_DEPRECATED);
  205.                         }
  206.                     }
  207.                 }
  208.                 // Detect method annotations
  209.                 if (false === $doc $method->getDocComment()) {
  210.                     continue;
  211.                 }
  212.                 foreach (array('final''internal') as $annotation) {
  213.                     if (false !== strpos($doc'@'.$annotation) && preg_match('#\n\s+\* @'.$annotation.'(?:( .+?)\.?)?\r?\n\s+\*(?: @|/$)#s'$doc$notice)) {
  214.                         $message = isset($notice[1]) ? preg_replace('#\s*\r?\n \* +#'' '$notice[1]) : '';
  215.                         self::${$annotation.'Methods'}[$name][$method->name] = array($name$message);
  216.                     }
  217.                 }
  218.             }
  219.             if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
  220.                 @trigger_error(sprintf('The "%s" class uses the reserved name "%s", it will break on PHP 7 and higher'$name$refl->getShortName()), E_USER_DEPRECATED);
  221.             }
  222.         }
  223.         if ($file) {
  224.             if (!$exists) {
  225.                 if (false !== strpos($class'/')) {
  226.                     throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".'$class));
  227.                 }
  228.                 throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.'$class$file));
  229.             }
  230.             if (self::$caseCheck) {
  231.                 $real explode('\\'$class.strrchr($file'.'));
  232.                 $tail explode(DIRECTORY_SEPARATORstr_replace('/'DIRECTORY_SEPARATOR$file));
  233.                 $i count($tail) - 1;
  234.                 $j count($real) - 1;
  235.                 while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
  236.                     --$i;
  237.                     --$j;
  238.                 }
  239.                 array_splice($tail0$i 1);
  240.             }
  241.             if (self::$caseCheck && $tail) {
  242.                 $tail DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR$tail);
  243.                 $tailLen strlen($tail);
  244.                 $real $refl->getFileName();
  245.                 if (=== self::$caseCheck) {
  246.                     // realpath() on MacOSX doesn't normalize the case of characters
  247.                     $i strrpos($real'/');
  248.                     $file substr($real$i);
  249.                     $real substr($real0$i);
  250.                     if (isset(self::$darwinCache[$real])) {
  251.                         $kDir $real;
  252.                     } else {
  253.                         $kDir strtolower($real);
  254.                         if (isset(self::$darwinCache[$kDir])) {
  255.                             $real self::$darwinCache[$kDir][0];
  256.                         } else {
  257.                             $dir getcwd();
  258.                             chdir($real);
  259.                             $real getcwd().'/';
  260.                             chdir($dir);
  261.                             $dir $real;
  262.                             $k $kDir;
  263.                             $i strlen($dir) - 1;
  264.                             while (!isset(self::$darwinCache[$k])) {
  265.                                 self::$darwinCache[$k] = array($dir, array());
  266.                                 self::$darwinCache[$dir] = &self::$darwinCache[$k];
  267.                                 while ('/' !== $dir[--$i]) {
  268.                                 }
  269.                                 $k substr($k0, ++$i);
  270.                                 $dir substr($dir0$i--);
  271.                             }
  272.                         }
  273.                     }
  274.                     $dirFiles self::$darwinCache[$kDir][1];
  275.                     if (isset($dirFiles[$file])) {
  276.                         $kFile $file;
  277.                     } else {
  278.                         $kFile strtolower($file);
  279.                         if (!isset($dirFiles[$kFile])) {
  280.                             foreach (scandir($real2) as $f) {
  281.                                 if ('.' !== $f[0]) {
  282.                                     $dirFiles[$f] = $f;
  283.                                     if ($f === $file) {
  284.                                         $kFile $k $file;
  285.                                     } elseif ($f !== $k strtolower($f)) {
  286.                                         $dirFiles[$k] = $f;
  287.                                     }
  288.                                 }
  289.                             }
  290.                             self::$darwinCache[$kDir][1] = $dirFiles;
  291.                         }
  292.                     }
  293.                     $real .= $dirFiles[$kFile];
  294.                 }
  295.                 if (=== substr_compare($real$tail, -$tailLen$tailLentrue)
  296.                   && !== substr_compare($real$tail, -$tailLen$tailLenfalse)
  297.                 ) {
  298.                     throw new \RuntimeException(sprintf('Case mismatch between class and real file names: "%s" vs "%s" in "%s".'substr($tail, -$tailLen 1), substr($real, -$tailLen 1), substr($real0, -$tailLen 1)));
  299.                 }
  300.             }
  301.             return true;
  302.         }
  303.     }
  304.     /**
  305.      * `class_implements` includes interfaces from the parents so we have to manually exclude them.
  306.      *
  307.      * @param string       $class
  308.      * @param string|false $parent
  309.      *
  310.      * @return string[]
  311.      */
  312.     private function getOwnInterfaces($class$parent)
  313.     {
  314.         $ownInterfaces class_implements($classfalse);
  315.         if ($parent) {
  316.             foreach (class_implements($parentfalse) as $interface) {
  317.                 unset($ownInterfaces[$interface]);
  318.             }
  319.         }
  320.         foreach ($ownInterfaces as $interface) {
  321.             foreach (class_implements($interface) as $interface) {
  322.                 unset($ownInterfaces[$interface]);
  323.             }
  324.         }
  325.         return $ownInterfaces;
  326.     }
  327. }