ClassExistenceResource.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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(string $resource, bool $exists = null)
  31. {
  32. $this->resource = $resource;
  33. $this->exists = $exists;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function __toString()
  39. {
  40. return $this->resource;
  41. }
  42. /**
  43. * @return string The file path to the resource
  44. */
  45. public function getResource()
  46. {
  47. return $this->resource;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. *
  52. * @throws \ReflectionException when a parent class/interface/trait is not found
  53. */
  54. public function isFresh($timestamp)
  55. {
  56. $loaded = class_exists($this->resource, false) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
  57. if (null !== $exists = &self::$existsCache[(int) (0 >= $timestamp)][$this->resource]) {
  58. $exists = $exists || $loaded;
  59. } elseif (!$exists = $loaded) {
  60. if (!self::$autoloadLevel++) {
  61. spl_autoload_register(__CLASS__.'::throwOnRequiredClass');
  62. }
  63. $autoloadedClass = self::$autoloadedClass;
  64. self::$autoloadedClass = $this->resource;
  65. try {
  66. $exists = class_exists($this->resource) || interface_exists($this->resource, false) || trait_exists($this->resource, false);
  67. } catch (\ReflectionException $e) {
  68. if (0 >= $timestamp) {
  69. unset(self::$existsCache[1][$this->resource]);
  70. throw $e;
  71. }
  72. } finally {
  73. self::$autoloadedClass = $autoloadedClass;
  74. if (!--self::$autoloadLevel) {
  75. spl_autoload_unregister(__CLASS__.'::throwOnRequiredClass');
  76. }
  77. }
  78. }
  79. if (null === $this->exists) {
  80. $this->exists = $exists;
  81. }
  82. return $this->exists xor !$exists;
  83. }
  84. /**
  85. * @internal
  86. */
  87. public function serialize()
  88. {
  89. if (null === $this->exists) {
  90. $this->isFresh(0);
  91. }
  92. return serialize([$this->resource, $this->exists]);
  93. }
  94. /**
  95. * @internal
  96. */
  97. public function unserialize($serialized)
  98. {
  99. list($this->resource, $this->exists) = unserialize($serialized);
  100. }
  101. /**
  102. * @throws \ReflectionException When $class is not found and is required
  103. *
  104. * @internal
  105. */
  106. public static function throwOnRequiredClass($class)
  107. {
  108. if (self::$autoloadedClass === $class) {
  109. return;
  110. }
  111. $e = new \ReflectionException("Class $class not found");
  112. $trace = $e->getTrace();
  113. $autoloadFrame = [
  114. 'function' => 'spl_autoload_call',
  115. 'args' => [$class],
  116. ];
  117. $i = 1 + array_search($autoloadFrame, $trace, true);
  118. if (isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
  119. switch ($trace[$i]['function']) {
  120. case 'get_class_methods':
  121. case 'get_class_vars':
  122. case 'get_parent_class':
  123. case 'is_a':
  124. case 'is_subclass_of':
  125. case 'class_exists':
  126. case 'class_implements':
  127. case 'class_parents':
  128. case 'trait_exists':
  129. case 'defined':
  130. case 'interface_exists':
  131. case 'method_exists':
  132. case 'property_exists':
  133. case 'is_callable':
  134. return;
  135. }
  136. $props = [
  137. 'file' => $trace[$i]['file'],
  138. 'line' => $trace[$i]['line'],
  139. 'trace' => \array_slice($trace, 1 + $i),
  140. ];
  141. foreach ($props as $p => $v) {
  142. $r = new \ReflectionProperty('Exception', $p);
  143. $r->setAccessible(true);
  144. $r->setValue($e, $v);
  145. }
  146. }
  147. throw $e;
  148. }
  149. }