summaryrefslogtreecommitdiff
path: root/vendor/symfony/cache/Adapter/PhpArrayAdapter.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/cache/Adapter/PhpArrayAdapter.php')
-rw-r--r--vendor/symfony/cache/Adapter/PhpArrayAdapter.php96
1 files changed, 59 insertions, 37 deletions
diff --git a/vendor/symfony/cache/Adapter/PhpArrayAdapter.php b/vendor/symfony/cache/Adapter/PhpArrayAdapter.php
index 87c8090b91..a145a361d1 100644
--- a/vendor/symfony/cache/Adapter/PhpArrayAdapter.php
+++ b/vendor/symfony/cache/Adapter/PhpArrayAdapter.php
@@ -17,7 +17,9 @@ use Symfony\Component\Cache\CacheItem;
use Symfony\Component\Cache\Exception\InvalidArgumentException;
use Symfony\Component\Cache\PruneableInterface;
use Symfony\Component\Cache\ResettableInterface;
+use Symfony\Component\Cache\Traits\ContractsTrait;
use Symfony\Component\Cache\Traits\PhpArrayTrait;
+use Symfony\Contracts\Cache\CacheInterface;
/**
* Caches items at warm up time using a PHP array that is stored in shared memory by OPCache since PHP 7.0.
@@ -26,9 +28,10 @@ use Symfony\Component\Cache\Traits\PhpArrayTrait;
* @author Titouan Galopin <galopintitouan@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
-class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface
+class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface
{
use PhpArrayTrait;
+ use ContractsTrait;
private $createCacheItem;
@@ -36,11 +39,10 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
* @param string $file The PHP file were values are cached
* @param AdapterInterface $fallbackPool A pool to fallback on when an item is not hit
*/
- public function __construct($file, AdapterInterface $fallbackPool)
+ public function __construct(string $file, AdapterInterface $fallbackPool)
{
$this->file = $file;
$this->pool = $fallbackPool;
- $this->zendDetectUnicode = ini_get('zend.detect_unicode');
$this->createCacheItem = \Closure::bind(
function ($key, $value, $isHit) {
$item = new CacheItem();
@@ -56,19 +58,17 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
}
/**
- * This adapter should only be used on PHP 7.0+ to take advantage of how PHP
- * stores arrays in its latest versions. This factory method decorates the given
- * fallback pool with this adapter only if the current PHP version is supported.
+ * This adapter takes advantage of how PHP stores arrays in its latest versions.
*
* @param string $file The PHP file were values are cached
- * @param CacheItemPoolInterface $fallbackPool Fallback for old PHP versions or opcache disabled
+ * @param CacheItemPoolInterface $fallbackPool Fallback when opcache is disabled
*
* @return CacheItemPoolInterface
*/
public static function create($file, CacheItemPoolInterface $fallbackPool)
{
- // Shared memory is available in PHP 7.0+ with OPCache enabled and in HHVM
- if ((\PHP_VERSION_ID >= 70000 && ini_get('opcache.enable')) || \defined('HHVM_VERSION')) {
+ // Shared memory is available in PHP 7.0+ with OPCache enabled
+ if (filter_var(ini_get('opcache.enable'), FILTER_VALIDATE_BOOLEAN)) {
if (!$fallbackPool instanceof AdapterInterface) {
$fallbackPool = new ProxyAdapter($fallbackPool);
}
@@ -82,6 +82,39 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
/**
* {@inheritdoc}
*/
+ public function get(string $key, callable $callback, float $beta = null, array &$metadata = null)
+ {
+ if (null === $this->values) {
+ $this->initialize();
+ }
+ if (!isset($this->keys[$key])) {
+ get_from_pool:
+ if ($this->pool instanceof CacheInterface) {
+ return $this->pool->get($key, $callback, $beta, $metadata);
+ }
+
+ return $this->doGet($this->pool, $key, $callback, $beta, $metadata);
+ }
+ $value = $this->values[$this->keys[$key]];
+
+ if ('N;' === $value) {
+ return null;
+ }
+ try {
+ if ($value instanceof \Closure) {
+ return $value();
+ }
+ } catch (\Throwable $e) {
+ unset($this->keys[$key]);
+ goto get_from_pool;
+ }
+
+ return $value;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
public function getItem($key)
{
if (!\is_string($key)) {
@@ -90,23 +123,19 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
if (null === $this->values) {
$this->initialize();
}
- if (!isset($this->values[$key])) {
+ if (!isset($this->keys[$key])) {
return $this->pool->getItem($key);
}
- $value = $this->values[$key];
+ $value = $this->values[$this->keys[$key]];
$isHit = true;
if ('N;' === $value) {
$value = null;
- } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ } elseif ($value instanceof \Closure) {
try {
- $e = null;
- $value = unserialize($value);
- } catch (\Error $e) {
- } catch (\Exception $e) {
- }
- if (null !== $e) {
+ $value = $value();
+ } catch (\Throwable $e) {
$value = null;
$isHit = false;
}
@@ -146,7 +175,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
$this->initialize();
}
- return isset($this->values[$key]) || $this->pool->hasItem($key);
+ return isset($this->keys[$key]) || $this->pool->hasItem($key);
}
/**
@@ -161,7 +190,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
$this->initialize();
}
- return !isset($this->values[$key]) && $this->pool->deleteItem($key);
+ return !isset($this->keys[$key]) && $this->pool->deleteItem($key);
}
/**
@@ -177,7 +206,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', \is_object($key) ? \get_class($key) : \gettype($key)));
}
- if (isset($this->values[$key])) {
+ if (isset($this->keys[$key])) {
$deleted = false;
} else {
$fallbackKeys[] = $key;
@@ -203,7 +232,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
$this->initialize();
}
- return !isset($this->values[$item->getKey()]) && $this->pool->save($item);
+ return !isset($this->keys[$item->getKey()]) && $this->pool->save($item);
}
/**
@@ -215,7 +244,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
$this->initialize();
}
- return !isset($this->values[$item->getKey()]) && $this->pool->saveDeferred($item);
+ return !isset($this->keys[$item->getKey()]) && $this->pool->saveDeferred($item);
}
/**
@@ -226,26 +255,21 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
return $this->pool->commit();
}
- /**
- * @return \Generator
- */
- private function generateItems(array $keys)
+ private function generateItems(array $keys): \Generator
{
$f = $this->createCacheItem;
$fallbackKeys = array();
foreach ($keys as $key) {
- if (isset($this->values[$key])) {
- $value = $this->values[$key];
+ if (isset($this->keys[$key])) {
+ $value = $this->values[$this->keys[$key]];
if ('N;' === $value) {
yield $key => $f($key, null, true);
- } elseif (\is_string($value) && isset($value[2]) && ':' === $value[1]) {
+ } elseif ($value instanceof \Closure) {
try {
- yield $key => $f($key, unserialize($value), true);
- } catch (\Error $e) {
- yield $key => $f($key, null, false);
- } catch (\Exception $e) {
+ yield $key => $f($key, $value(), true);
+ } catch (\Throwable $e) {
yield $key => $f($key, null, false);
}
} else {
@@ -257,9 +281,7 @@ class PhpArrayAdapter implements AdapterInterface, PruneableInterface, Resettabl
}
if ($fallbackKeys) {
- foreach ($this->pool->getItems($fallbackKeys) as $key => $item) {
- yield $key => $item;
- }
+ yield from $this->pool->getItems($fallbackKeys);
}
}