PhpArrayAdapter.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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\Cache\Adapter;
  11. use Psr\Cache\CacheItemInterface;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\CacheItem;
  14. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  15. use Symfony\Component\Cache\PruneableInterface;
  16. use Symfony\Component\Cache\ResettableInterface;
  17. use Symfony\Component\Cache\Traits\PhpArrayTrait;
  18. /**
  19. * Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0.
  20. * Warmed up items are read-only and run-time discovered items are cached using a fallback adapter.
  21. *
  22. * @author Titouan Galopin <galopintitouan@gmail.com>
  23. * @author Nicolas Grekas <p@tchwork.com>
  24. */
  25. class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
  26. {
  27. use PhpArrayTrait;
  28. private $createCacheItem;
  29. /**
  30. * @param string $file The PHP file were values are cached
  31. * @param AdapterInterface $fallbackPool A pool to fallback on when an item is not hit
  32. */
  33. public function __construct(string $file, AdapterInterface $fallbackPool)
  34. {
  35. $this->file = $file;
  36. $this->pool = $fallbackPool;
  37. $this->zendDetectUnicode = filter_var(ini_get('zend.detect_unicode'), FILTER_VALIDATE_BOOLEAN);
  38. $this->createCacheItem = \Closure::bind(
  39. function ($key, $value, $isHit) {
  40. $item = new CacheItem();
  41. $item->key = $key;
  42. $item->value = $value;
  43. $item->isHit = $isHit;
  44. return $item;
  45. },
  46. null,
  47. CacheItem::class
  48. );
  49. }
  50. /**
  51. * This adapter takes advantage of how PHP stores arrays in its latest versions.
  52. *
  53. * @param string $file The PHP file were values are cached
  54. * @param CacheItemPoolInterface $fallbackPool Fallback when opcache is disabled
  55. *
  56. * @return CacheItemPoolInterface
  57. */
  58. public static function create($file, CacheItemPoolInterface $fallbackPool)
  59. {
  60. // Shared memory is available in PHP 7.0+ with OPCache enabled
  61. if (filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
  62. if (!$fallbackPool instanceof AdapterInterface) {
  63. $fallbackPool = new ProxyAdapter($fallbackPool);
  64. }
  65. return new static($file, $fallbackPool);
  66. }
  67. return $fallbackPool;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getItem($key)
  73. {
  74. if (!\is_string($key)) {
  75. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  76. }
  77. if (null === $this->values) {
  78. $this->initialize();
  79. }
  80. if (!isset($this->values[$key])) {
  81. return $this->pool->getItem($key);
  82. }
  83. $value = $this->values[$key];
  84. $isHit = true;
  85. if ('N;' === $value) {
  86. $value = null;
  87. } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  88. try {
  89. $value = unserialize($value);
  90. } catch (\Throwable $e) {
  91. $value = null;
  92. $isHit = false;
  93. }
  94. }
  95. $f = $this->createCacheItem;
  96. return $f($key, $value, $isHit);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function getItems(array $keys = [])
  102. {
  103. foreach ($keys as $key) {
  104. if (!\is_string($key)) {
  105. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  106. }
  107. }
  108. if (null === $this->values) {
  109. $this->initialize();
  110. }
  111. return $this->generateItems($keys);
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function hasItem($key)
  117. {
  118. if (!\is_string($key)) {
  119. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  120. }
  121. if (null === $this->values) {
  122. $this->initialize();
  123. }
  124. return isset($this->values[$key]) || $this->pool->hasItem($key);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function deleteItem($key)
  130. {
  131. if (!\is_string($key)) {
  132. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  133. }
  134. if (null === $this->values) {
  135. $this->initialize();
  136. }
  137. return !isset($this->values[$key]) && $this->pool->deleteItem($key);
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function deleteItems(array $keys)
  143. {
  144. $deleted = true;
  145. $fallbackKeys = [];
  146. foreach ($keys as $key) {
  147. if (!\is_string($key)) {
  148. throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
  149. }
  150. if (isset($this->values[$key])) {
  151. $deleted = false;
  152. } else {
  153. $fallbackKeys[] = $key;
  154. }
  155. }
  156. if (null === $this->values) {
  157. $this->initialize();
  158. }
  159. if ($fallbackKeys) {
  160. $deleted = $this->pool->deleteItems($fallbackKeys) && $deleted;
  161. }
  162. return $deleted;
  163. }
  164. /**
  165. * {@inheritdoc}
  166. */
  167. public function save(CacheItemInterface $item)
  168. {
  169. if (null === $this->values) {
  170. $this->initialize();
  171. }
  172. return !isset($this->values[$item->getKey()]) && $this->pool->save($item);
  173. }
  174. /**
  175. * {@inheritdoc}
  176. */
  177. public function saveDeferred(CacheItemInterface $item)
  178. {
  179. if (null === $this->values) {
  180. $this->initialize();
  181. }
  182. return !isset($this->values[$item->getKey()]) && $this->pool->saveDeferred($item);
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function commit()
  188. {
  189. return $this->pool->commit();
  190. }
  191. private function generateItems(array $keys): \Generator
  192. {
  193. $f = $this->createCacheItem;
  194. $fallbackKeys = [];
  195. foreach ($keys as $key) {
  196. if (isset($this->values[$key])) {
  197. $value = $this->values[$key];
  198. if ('N;' === $value) {
  199. yield $key => $f($key, null, true);
  200. } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
  201. try {
  202. yield $key => $f($key, unserialize($value), true);
  203. } catch (\Throwable $e) {
  204. yield $key => $f($key, null, false);
  205. }
  206. } else {
  207. yield $key => $f($key, $value, true);
  208. }
  209. } else {
  210. $fallbackKeys[] = $key;
  211. }
  212. }
  213. if ($fallbackKeys) {
  214. foreach ($this->pool->getItems($fallbackKeys) as $key => $item) {
  215. yield $key => $item;
  216. }
  217. }
  218. }
  219. /**
  220. * @throws \ReflectionException When $class is not found and is required
  221. *
  222. * @internal
  223. */
  224. public static function throwOnRequiredClass($class)
  225. {
  226. $e = new \ReflectionException("Class $class does not exist");
  227. $trace = $e->getTrace();
  228. $autoloadFrame = [
  229. 'function' => 'spl_autoload_call',
  230. 'args' => [$class],
  231. ];
  232. $i = 1 + array_search($autoloadFrame, $trace, true);
  233. if (isset($trace[$i]['function']) && !isset($trace[$i]['class'])) {
  234. switch ($trace[$i]['function']) {
  235. case 'get_class_methods':
  236. case 'get_class_vars':
  237. case 'get_parent_class':
  238. case 'is_a':
  239. case 'is_subclass_of':
  240. case 'class_exists':
  241. case 'class_implements':
  242. case 'class_parents':
  243. case 'trait_exists':
  244. case 'defined':
  245. case 'interface_exists':
  246. case 'method_exists':
  247. case 'property_exists':
  248. case 'is_callable':
  249. return;
  250. }
  251. }
  252. throw $e;
  253. }
  254. }