|
13 | 13 |
|
14 | 14 | use Psr\Cache\CacheItemInterface; |
15 | 15 | use Psr\Cache\CacheItemPoolInterface; |
| 16 | +use Symfony\Component\Cache\CacheInterface; |
16 | 17 | use Symfony\Component\Cache\CacheItem; |
17 | 18 | use Symfony\Component\Cache\Exception\InvalidArgumentException; |
18 | 19 | use Symfony\Component\Cache\PruneableInterface; |
19 | 20 | use Symfony\Component\Cache\ResettableInterface; |
| 21 | +use Symfony\Component\Cache\Traits\GetTrait; |
20 | 22 | use Symfony\Component\Cache\Traits\PhpArrayTrait; |
21 | 23 |
|
22 | 24 | /** |
|
26 | 28 | * @author Titouan Galopin <galopintitouan@gmail.com> |
27 | 29 | * @author Nicolas Grekas <p@tchwork.com> |
28 | 30 | */ |
29 | | -class PhpArrayAdapter implements AdapterInterface, PruneableInterface, ResettableInterface |
| 31 | +class PhpArrayAdapter implements AdapterInterface, CacheInterface, PruneableInterface, ResettableInterface |
30 | 32 | { |
31 | 33 | use PhpArrayTrait; |
| 34 | + use GetTrait; |
32 | 35 |
|
33 | 36 | private $createCacheItem; |
34 | 37 |
|
@@ -77,6 +80,34 @@ public static function create($file, CacheItemPoolInterface $fallbackPool) |
77 | 80 | return $fallbackPool; |
78 | 81 | } |
79 | 82 |
|
| 83 | + /** |
| 84 | + * {@inheritdoc} |
| 85 | + */ |
| 86 | + public function get(string $key, callable $callback) |
| 87 | + { |
| 88 | + if (!\is_string($key)) { |
| 89 | + throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given.', is_object($key) ? get_class($key) : gettype($key))); |
| 90 | + } |
| 91 | + if (null === $this->values) { |
| 92 | + $this->initialize(); |
| 93 | + } |
| 94 | + if (null === $value = $this->values[$key] ?? null) { |
| 95 | + if ($this->pool instanceof CacheInterface) { |
| 96 | + return $this->pool->get($key, $callback); |
| 97 | + } |
| 98 | + |
| 99 | + return $this->doGet($this->pool, $key, $callback); |
| 100 | + } |
| 101 | + if ('N;' === $value) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + if (\is_string($value) && isset($value[2]) && ':' === $value[1]) { |
| 105 | + return unserialize($value); |
| 106 | + } |
| 107 | + |
| 108 | + return $value; |
| 109 | + } |
| 110 | + |
80 | 111 | /** |
81 | 112 | * {@inheritdoc} |
82 | 113 | */ |
|
0 commit comments