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

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. class ClassExistenceResource implements SelfCheckingResourceInterface\Serializable
  20. {
  21.     private $resource;
  22.     private $exists;
  23.     private static $autoloadLevel 0;
  24.     private static $autoloadedClass;
  25.     private static $existsCache = [];
  26.     /**
  27.      * @param string    $resource The fully-qualified class name
  28.      * @param bool|null $exists   Boolean when the existency check has already been done
  29.      */
  30.     public function __construct($resource$exists null)
  31.     {
  32.         $this->resource $resource;
  33.         if (null !== $exists) {
  34.             $this->exists = (bool) $exists;
  35.         }
  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 serialize()
  94.     {
  95.         if (null === $this->exists) {
  96.             $this->isFresh(0);
  97.         }
  98.         return serialize([$this->resource$this->exists]);
  99.     }
  100.     /**
  101.      * @internal
  102.      */
  103.     public function unserialize($serialized)
  104.     {
  105.         list($this->resource$this->exists) = unserialize($serialized);
  106.     }
  107.     /**
  108.      * Throws a reflection exception when the passed class does not exist but is required.
  109.      *
  110.      * A class is considered "not required" when it's loaded as part of a "class_exists" or similar check.
  111.      *
  112.      * This function can be used as an autoload function to throw a reflection
  113.      * exception if the class was not found by previous autoload functions.
  114.      *
  115.      * A previous exception can be passed. In this case, the class is considered as being
  116.      * required totally, so if it doesn't exist, a reflection exception is always thrown.
  117.      * If it exists, the previous exception is rethrown.
  118.      *
  119.      * @throws \ReflectionException
  120.      *
  121.      * @internal
  122.      */
  123.     public static function throwOnRequiredClass($class\Exception $previous null)
  124.     {
  125.         // If the passed class is the resource being checked, we shouldn't throw.
  126.         if (null === $previous && self::$autoloadedClass === $class) {
  127.             return;
  128.         }
  129.         if (class_exists($classfalse) || interface_exists($classfalse) || trait_exists($classfalse)) {
  130.             if (null !== $previous) {
  131.                 throw $previous;
  132.             }
  133.             return;
  134.         }
  135.         if ($previous instanceof \ReflectionException) {
  136.             throw $previous;
  137.         }
  138.         $e = new \ReflectionException(sprintf('Class "%s" not found while loading "%s".'$classself::$autoloadedClass), 0$previous);
  139.         if (null !== $previous) {
  140.             throw $e;
  141.         }
  142.         $trace $e->getTrace();
  143.         $autoloadFrame = [
  144.             'function' => 'spl_autoload_call',
  145.             'args' => [$class],
  146.         ];
  147.         if (false === $i array_search($autoloadFrame$tracetrue)) {
  148.             throw $e;
  149.         }
  150.         if (isset($trace[++$i]['function']) && !isset($trace[$i]['class'])) {
  151.             switch ($trace[$i]['function']) {
  152.                 case 'get_class_methods':
  153.                 case 'get_class_vars':
  154.                 case 'get_parent_class':
  155.                 case 'is_a':
  156.                 case 'is_subclass_of':
  157.                 case 'class_exists':
  158.                 case 'class_implements':
  159.                 case 'class_parents':
  160.                 case 'trait_exists':
  161.                 case 'defined':
  162.                 case 'interface_exists':
  163.                 case 'method_exists':
  164.                 case 'property_exists':
  165.                 case 'is_callable':
  166.                     return;
  167.             }
  168.             $props = [
  169.                 'file' => $trace[$i]['file'],
  170.                 'line' => $trace[$i]['line'],
  171.                 'trace' => \array_slice($trace$i),
  172.             ];
  173.             foreach ($props as $p => $v) {
  174.                 $r = new \ReflectionProperty('Exception'$p);
  175.                 $r->setAccessible(true);
  176.                 $r->setValue($e$v);
  177.             }
  178.         }
  179.         throw $e;
  180.     }
  181. }