| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
namespace WindPressDeps\Symfony\Component\PropertyInfo; |
| 12 |
|
| 13 |
use WindPressDeps\Psr\Cache\CacheItemPoolInterface; |
| 14 |
/** |
| 15 |
* Adds a PSR-6 cache layer on top of an extractor. |
| 16 |
* |
| 17 |
* @author Kévin Dunglas <dunglas@gmail.com> |
| 18 |
* |
| 19 |
* @final |
| 20 |
*/ |
| 21 |
class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, PropertyInitializableExtractorInterface |
| 22 |
{ |
| 23 |
private $propertyInfoExtractor; |
| 24 |
private $cacheItemPool; |
| 25 |
private $arrayCache = []; |
| 26 |
public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, CacheItemPoolInterface $cacheItemPool) |
| 27 |
{ |
| 28 |
$this->propertyInfoExtractor = $propertyInfoExtractor; |
| 29 |
$this->cacheItemPool = $cacheItemPool; |
| 30 |
} |
| 31 |
/** |
| 32 |
* {@inheritdoc} |
| 33 |
*/ |
| 34 |
public function isReadable(string $class, string $property, array $context = []): ?bool |
| 35 |
{ |
| 36 |
return $this->extract('isReadable', [$class, $property, $context]); |
| 37 |
} |
| 38 |
/** |
| 39 |
* {@inheritdoc} |
| 40 |
*/ |
| 41 |
public function isWritable(string $class, string $property, array $context = []): ?bool |
| 42 |
{ |
| 43 |
return $this->extract('isWritable', [$class, $property, $context]); |
| 44 |
} |
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
*/ |
| 48 |
public function getShortDescription(string $class, string $property, array $context = []): ?string |
| 49 |
{ |
| 50 |
return $this->extract('getShortDescription', [$class, $property, $context]); |
| 51 |
} |
| 52 |
/** |
| 53 |
* {@inheritdoc} |
| 54 |
*/ |
| 55 |
public function getLongDescription(string $class, string $property, array $context = []): ?string |
| 56 |
{ |
| 57 |
return $this->extract('getLongDescription', [$class, $property, $context]); |
| 58 |
} |
| 59 |
/** |
| 60 |
* {@inheritdoc} |
| 61 |
*/ |
| 62 |
public function getProperties(string $class, array $context = []): ?array |
| 63 |
{ |
| 64 |
return $this->extract('getProperties', [$class, $context]); |
| 65 |
} |
| 66 |
/** |
| 67 |
* {@inheritdoc} |
| 68 |
*/ |
| 69 |
public function getTypes(string $class, string $property, array $context = []): ?array |
| 70 |
{ |
| 71 |
return $this->extract('getTypes', [$class, $property, $context]); |
| 72 |
} |
| 73 |
/** |
| 74 |
* {@inheritdoc} |
| 75 |
*/ |
| 76 |
public function isInitializable(string $class, string $property, array $context = []): ?bool |
| 77 |
{ |
| 78 |
return $this->extract('isInitializable', [$class, $property, $context]); |
| 79 |
} |
| 80 |
/** |
| 81 |
* Retrieves the cached data if applicable or delegates to the decorated extractor. |
| 82 |
* |
| 83 |
* @return mixed |
| 84 |
*/ |
| 85 |
private function extract(string $method, array $arguments) |
| 86 |
{ |
| 87 |
try { |
| 88 |
$serializedArguments = serialize($arguments); |
| 89 |
} catch (\Exception $exception) { |
| 90 |
// If arguments are not serializable, skip the cache |
| 91 |
return $this->propertyInfoExtractor->{$method}(...$arguments); |
| 92 |
} |
| 93 |
// Calling rawurlencode escapes special characters not allowed in PSR-6's keys |
| 94 |
$key = rawurlencode($method . '.' . $serializedArguments); |
| 95 |
if (\array_key_exists($key, $this->arrayCache)) { |
| 96 |
return $this->arrayCache[$key]; |
| 97 |
} |
| 98 |
$item = $this->cacheItemPool->getItem($key); |
| 99 |
if ($item->isHit()) { |
| 100 |
return $this->arrayCache[$key] = $item->get(); |
| 101 |
} |
| 102 |
$value = $this->propertyInfoExtractor->{$method}(...$arguments); |
| 103 |
$item->set($value); |
| 104 |
$this->cacheItemPool->save($item); |
| 105 |
return $this->arrayCache[$key] = $value; |
| 106 |
} |
| 107 |
} |
| 108 |
|