vendor/symfony/config/Resource/ClassExistenceResource.php line 81

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\Config\Resource;
  11. /**
  12.  * ClassExistenceResource represents a class existence.
  13.  * Freshness is only evaluated against resource existence.
  14.  *
  15.  * The resource must be a fully-qualified class name.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @final since Symfony 4.3
  20.  */
  21. class ClassExistenceResource implements SelfCheckingResourceInterface
  22. {
  23.     private $resource;
  24.     private $exists;
  25.     private static $autoloadLevel 0;
  26.     private static $autoloadedClass;
  27.     private static $existsCache = [];
  28.     /**
  29.      * @param string    $resource The fully-qualified class name
  30.      * @param bool|null $exists   Boolean when the existency check has already been done
  31.      */
  32.     public function __construct(string $resourcebool $exists null)
  33.     {
  34.         $this->resource $resource;
  35.         if (null !== $exists) {
  36.             $this->exists = [$existsnull];
  37.         }
  38.     }
  39.     public function __toString()
  40.     {
  41.         return $this->resource;
  42.     }
  43.     /**
  44.      * @return string The file path to the resource
  45.      */
  46.     public function getResource()
  47.     {
  48.         return $this->resource;
  49.     }
  50.     /**
  51.      * {@inheritdoc}
  52.      *
  53.      * @throws \ReflectionException when a parent class/interface/trait is not found
  54.      */
  55.     public function isFresh($timestamp)
  56.     {
  57.         $loaded class_exists($this->resourcefalse) || interface_exists($this->resourcefalse) || trait_exists($this->resourcefalse);
  58.         if (null !== $exists = &self::$existsCache[$this->resource]) {
  59.             if ($loaded) {
  60.                 $exists = [truenull];
  61.             } elseif (>= $timestamp && !$exists[0] && null !== $exists[1]) {
  62.                 throw new \ReflectionException($exists[1]);
  63.             }
  64.         } elseif ([falsenull] === $exists = [$loadednull]) {
  65.             if (!self::$autoloadLevel++) {
  66.                 spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
  67.             }
  68.             $autoloadedClass self::$autoloadedClass;
  69.             self::$autoloadedClass ltrim($this->resource'\\');
  70.             try {
  71.                 $exists[0] = class_exists($this->resource) || interface_exists($this->resourcefalse) || trait_exists($this->resourcefalse);
  72.             } catch (\Exception $e) {
  73.                 $exists[1] = $e->getMessage();
  74.                 try {
  75.                     self::throwOnRequiredClass($this->resource$e);
  76.                 } catch (\ReflectionException $e) {
  77.                     if (>= $timestamp) {
  78.                         throw $e;
  79.                     }
  80.                 }
  81.             } catch (\Throwable $e) {
  82.                 $exists[1] = $e->getMessage();
  83.                 throw $e;
  84.             } finally {
  85.                 self::$autoloadedClass $autoloadedClass;
  86.                 if (!--self::$autoloadLevel) {
  87.                     spl_autoload_unregister(__CLASS__.'::throwOnRequiredClass');
  88.                 }
  89.             }
  90.         }
  91.         if (null === $this->exists) {
  92.             $this->exists $exists;
  93.         }
  94.         return $this->exists[0] xor !$exists[0];
  95.     }
  96.     /**
  97.      * @internal
  98.      */
  99.     public function __sleep(): array
  100.     {
  101.         if (null === $this->exists) {
  102.             $this->isFresh(0);
  103.         }
  104.         return ['resource''exists'];
  105.     }
  106.     /**
  107.      * @internal
  108.      */
  109.     public function __wakeup()
  110.     {
  111.         if (\is_bool($this->exists)) {
  112.             $this->exists = [$this->existsnull];
  113.         }
  114.     }
  115.     /**
  116.      * Throws a reflection exception when the passed class does not exist but is required.
  117.      *
  118.      * A class is considered "not required" when it's loaded as part of a "class_exists" or similar check.
  119.      *
  120.      * This function can be used as an autoload function to throw a reflection
  121.      * exception if the class was not found by previous autoload functions.
  122.      *
  123.      * A previous exception can be passed. In this case, the class is considered as being
  124.      * required totally, so if it doesn't exist, a reflection exception is always thrown.
  125.      * If it exists, the previous exception is rethrown.
  126.      *
  127.      * @throws \ReflectionException
  128.      *
  129.      * @internal
  130.      */
  131.     public static function throwOnRequiredClass($class\Exception $previous null)
  132.     {
  133.         // If the passed class is the resource being checked, we shouldn't throw.
  134.         if (null === $previous && self::$autoloadedClass === $class) {
  135.             return;
  136.         }
  137.         if (class_exists($classfalse) || interface_exists($classfalse) || trait_exists($classfalse)) {
  138.             if (null !== $previous) {
  139.                 throw $previous;
  140.             }
  141.             return;
  142.         }
  143.         if ($previous instanceof \ReflectionException) {
  144.             throw $previous;
  145.         }
  146.         $message sprintf('Class "%s" not found.'$class);
  147.         if (self::$autoloadedClass !== $class) {
  148.             $message substr_replace($messagesprintf(' while loading "%s"'self::$autoloadedClass), -10);
  149.         }
  150.         if (null !== $previous) {
  151.             $message $previous->getMessage();
  152.         }
  153.         $e = new \ReflectionException($message0$previous);
  154.         if (null !== $previous) {
  155.             throw $e;
  156.         }
  157.         $trace debug_backtrace();
  158.         $autoloadFrame = [
  159.             'function' => 'spl_autoload_call',
  160.             'args' => [$class],
  161.         ];
  162.         if (\PHP_VERSION_ID >= 80000 && isset($trace[1])) {
  163.             $callerFrame $trace[1];
  164.             $i 2;
  165.         } elseif (false !== $i array_search($autoloadFrame$tracetrue)) {
  166.             $callerFrame $trace[++$i];
  167.         } else {
  168.             throw $e;
  169.         }
  170.         if (isset($callerFrame['function']) && !isset($callerFrame['class'])) {
  171.             switch ($callerFrame['function']) {
  172.                 case 'get_class_methods':
  173.                 case 'get_class_vars':
  174.                 case 'get_parent_class':
  175.                 case 'is_a':
  176.                 case 'is_subclass_of':
  177.                 case 'class_exists':
  178.                 case 'class_implements':
  179.                 case 'class_parents':
  180.                 case 'trait_exists':
  181.                 case 'defined':
  182.                 case 'interface_exists':
  183.                 case 'method_exists':
  184.                 case 'property_exists':
  185.                 case 'is_callable':
  186.                     return;
  187.             }
  188.             $props = [
  189.                 'file' => $callerFrame['file'] ?? null,
  190.                 'line' => $callerFrame['line'] ?? null,
  191.                 'trace' => \array_slice($trace$i),
  192.             ];
  193.             foreach ($props as $p => $v) {
  194.                 if (null !== $v) {
  195.                     $r = new \ReflectionProperty(\Exception::class, $p);
  196.                     $r->setAccessible(true);
  197.                     $r->setValue($e$v);
  198.                 }
  199.             }
  200.         }
  201.         throw $e;
  202.     }
  203. }