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

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.         $this->exists $exists;
  36.     }
  37.     /**
  38.      * {@inheritdoc}
  39.      */
  40.     public function __toString()
  41.     {
  42.         return $this->resource;
  43.     }
  44.     /**
  45.      * @return string The file path to the resource
  46.      */
  47.     public function getResource()
  48.     {
  49.         return $this->resource;
  50.     }
  51.     /**
  52.      * {@inheritdoc}
  53.      *
  54.      * @throws \ReflectionException when a parent class/interface/trait is not found
  55.      */
  56.     public function isFresh($timestamp)
  57.     {
  58.         $loaded class_exists($this->resourcefalse) || interface_exists($this->resourcefalse) || trait_exists($this->resourcefalse);
  59.         if (null !== $exists = &self::$existsCache[(int) (>= $timestamp)][$this->resource]) {
  60.             $exists $exists || $loaded;
  61.         } elseif (!$exists $loaded) {
  62.             if (!self::$autoloadLevel++) {
  63.                 spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
  64.             }
  65.             $autoloadedClass self::$autoloadedClass;
  66.             self::$autoloadedClass $this->resource;
  67.             try {
  68.                 $exists class_exists($this->resource) || interface_exists($this->resourcefalse) || trait_exists($this->resourcefalse);
  69.             } catch (\Exception $e) {
  70.                 try {
  71.                     self::throwOnRequiredClass($this->resource$e);
  72.                 } catch (\ReflectionException $e) {
  73.                     if (>= $timestamp) {
  74.                         unset(self::$existsCache[1][$this->resource]);
  75.                         throw $e;
  76.                     }
  77.                 }
  78.             } finally {
  79.                 self::$autoloadedClass $autoloadedClass;
  80.                 if (!--self::$autoloadLevel) {
  81.                     spl_autoload_unregister(__CLASS__.'::throwOnRequiredClass');
  82.                 }
  83.             }
  84.         }
  85.         if (null === $this->exists) {
  86.             $this->exists $exists;
  87.         }
  88.         return $this->exists xor !$exists;
  89.     }
  90.     /**
  91.      * @internal
  92.      */
  93.     public function __sleep(): array
  94.     {
  95.         if (null === $this->exists) {
  96.             $this->isFresh(0);
  97.         }
  98.         return ['resource''exists'];
  99.     }
  100.     /**
  101.      * Throws a reflection exception when the passed class does not exist but is required.
  102.      *
  103.      * A class is considered "not required" when it's loaded as part of a "class_exists" or similar check.
  104.      *
  105.      * This function can be used as an autoload function to throw a reflection
  106.      * exception if the class was not found by previous autoload functions.
  107.      *
  108.      * A previous exception can be passed. In this case, the class is considered as being
  109.      * required totally, so if it doesn't exist, a reflection exception is always thrown.
  110.      * If it exists, the previous exception is rethrown.
  111.      *
  112.      * @throws \ReflectionException
  113.      *
  114.      * @internal
  115.      */
  116.     public static function throwOnRequiredClass($class, \Exception $previous null)
  117.     {
  118.         // If the passed class is the resource being checked, we shouldn't throw.
  119.         if (null === $previous && self::$autoloadedClass === $class) {
  120.             return;
  121.         }
  122.         if (class_exists($classfalse) || interface_exists($classfalse) || trait_exists($classfalse)) {
  123.             if (null !== $previous) {
  124.                 throw $previous;
  125.             }
  126.             return;
  127.         }
  128.         if ($previous instanceof \ReflectionException) {
  129.             throw $previous;
  130.         }
  131.         $e = new \ReflectionException("Class $class not found"0$previous);
  132.         if (null !== $previous) {
  133.             throw $e;
  134.         }
  135.         $trace $e->getTrace();
  136.         $autoloadFrame = [
  137.             'function' => 'spl_autoload_call',
  138.             'args' => [$class],
  139.         ];
  140.         if (false === $i array_search($autoloadFrame$tracetrue)) {
  141.             throw $e;
  142.         }
  143.         if (isset($trace[++$i]['function']) && !isset($trace[$i]['class'])) {
  144.             switch ($trace[$i]['function']) {
  145.                 case 'get_class_methods':
  146.                 case 'get_class_vars':
  147.                 case 'get_parent_class':
  148.                 case 'is_a':
  149.                 case 'is_subclass_of':
  150.                 case 'class_exists':
  151.                 case 'class_implements':
  152.                 case 'class_parents':
  153.                 case 'trait_exists':
  154.                 case 'defined':
  155.                 case 'interface_exists':
  156.                 case 'method_exists':
  157.                 case 'property_exists':
  158.                 case 'is_callable':
  159.                     return;
  160.             }
  161.             $props = [
  162.                 'file' => $trace[$i]['file'],
  163.                 'line' => $trace[$i]['line'],
  164.                 'trace' => \array_slice($trace$i),
  165.             ];
  166.             foreach ($props as $p => $v) {
  167.                 $r = new \ReflectionProperty('Exception'$p);
  168.                 $r->setAccessible(true);
  169.                 $r->setValue($e$v);
  170.             }
  171.         }
  172.         throw $e;
  173.     }
  174. }