4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config\Resource;
15 * ClassExistenceResource represents a class existence.
16 * Freshness is only evaluated against resource existence.
18 * The resource must be a fully-qualified class name.
20 * @author Fabien Potencier <fabien@symfony.com>
22 class ClassExistenceResource implements SelfCheckingResourceInterface, \Serializable
27 private static $autoloadLevel = 0;
28 private static $autoloadedClass;
29 private static $existsCache = array();
32 * @param string $resource The fully-qualified class name
33 * @param bool|null $exists Boolean when the existency check has already been done
35 public function __construct($resource, $exists = null)
37 $this->resource = $resource;
38 if (null !== $exists) {
39 $this->exists = (bool) $exists;
46 public function __toString()
48 return $this->resource;
52 * @return string The file path to the resource
54 public function getResource()
56 return $this->resource;
62 * @throws \ReflectionException when a parent class/interface/trait is not found
64 public function isFresh($timestamp)
66 $loaded = class_exists($this->resource, false) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
68 if (null !== $exists = &self::$existsCache[(int) (0 >= $timestamp)][$this->resource]) {
69 $exists = $exists || $loaded;
70 } elseif (!$exists = $loaded) {
71 if (!self::$autoloadLevel++) {
72 spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
74 $autoloadedClass = self::$autoloadedClass;
75 self::$autoloadedClass = $this->resource;
78 $exists = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
79 } catch (\ReflectionException $e) {
80 if (0 >= $timestamp) {
81 unset(self::$existsCache[1][$this->resource]);
85 self::$autoloadedClass = $autoloadedClass;
86 if (!--self::$autoloadLevel) {
87 spl_autoload_unregister(__CLASS__.'::throwOnRequiredClass');
92 if (null === $this->exists) {
93 $this->exists = $exists;
96 return $this->exists xor !$exists;
102 public function serialize()
104 if (null === $this->exists) {
108 return serialize(array($this->resource, $this->exists));
114 public function unserialize($serialized)
116 list($this->resource, $this->exists) = unserialize($serialized);
120 * @throws \ReflectionException When $class is not found and is required
122 private static function throwOnRequiredClass($class)
124 if (self::$autoloadedClass === $class) {
127 $e = new \ReflectionException("Class $class not found");
128 $trace = $e->getTrace();
129 $autoloadFrame = array(
130 'function' => 'spl_autoload_call',
131 'args' => array($class),
133 $i = 1 + array_search($autoloadFrame, $trace, true);
135 if (isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
136 switch ($trace[$i]['function']) {
137 case 'get_class_methods':
138 case 'get_class_vars':
139 case 'get_parent_class':
141 case 'is_subclass_of':
143 case 'class_implements':
144 case 'class_parents':
147 case 'interface_exists':
148 case 'method_exists':
149 case 'property_exists':
155 'file' => $trace[$i]['file'],
156 'line' => $trace[$i]['line'],
157 'trace' => \array_slice($trace, 1 + $i),
160 foreach ($props as $p => $v) {
161 $r = new \ReflectionProperty('Exception', $p);
162 $r->setAccessible(true);
163 $r->setValue($e, $v);