PropertyInfoCacheExtractor.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\PropertyInfo;
  11. use Psr\Cache\CacheItemPoolInterface;
  12. use Symfony\Component\PropertyInfo\Util\LegacyTypeConverter;
  13. use Symfony\Component\TypeInfo\Type;
  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 array $arrayCache = [];
  24. public function __construct(
  25. private readonly PropertyInfoExtractorInterface $propertyInfoExtractor,
  26. private readonly CacheItemPoolInterface $cacheItemPool,
  27. ) {
  28. }
  29. public function isReadable(string $class, string $property, array $context = []): ?bool
  30. {
  31. return $this->extract('isReadable', [$class, $property, $context]);
  32. }
  33. public function isWritable(string $class, string $property, array $context = []): ?bool
  34. {
  35. return $this->extract('isWritable', [$class, $property, $context]);
  36. }
  37. public function getShortDescription(string $class, string $property, array $context = []): ?string
  38. {
  39. return $this->extract('getShortDescription', [$class, $property, $context]);
  40. }
  41. public function getLongDescription(string $class, string $property, array $context = []): ?string
  42. {
  43. return $this->extract('getLongDescription', [$class, $property, $context]);
  44. }
  45. public function getProperties(string $class, array $context = []): ?array
  46. {
  47. return $this->extract('getProperties', [$class, $context]);
  48. }
  49. public function getType(string $class, string $property, array $context = []): ?Type
  50. {
  51. try {
  52. $serializedArguments = serialize([$class, $property, $context]);
  53. } catch (\Exception) {
  54. // If arguments are not serializable, skip the cache
  55. if (method_exists($this->propertyInfoExtractor, 'getType')) {
  56. return $this->propertyInfoExtractor->getType($class, $property, $context);
  57. }
  58. return LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
  59. }
  60. // Calling rawurlencode escapes special characters not allowed in PSR-6's keys
  61. $key = rawurlencode('getType.'.$serializedArguments);
  62. if (\array_key_exists($key, $this->arrayCache)) {
  63. return $this->arrayCache[$key];
  64. }
  65. $item = $this->cacheItemPool->getItem($key);
  66. if ($item->isHit()) {
  67. return $this->arrayCache[$key] = $item->get();
  68. }
  69. if (method_exists($this->propertyInfoExtractor, 'getType')) {
  70. $value = $this->propertyInfoExtractor->getType($class, $property, $context);
  71. } else {
  72. $value = LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
  73. }
  74. $item->set($value);
  75. $this->cacheItemPool->save($item);
  76. return $this->arrayCache[$key] = $value;
  77. }
  78. public function getTypes(string $class, string $property, array $context = []): ?array
  79. {
  80. return $this->extract('getTypes', [$class, $property, $context]);
  81. }
  82. public function isInitializable(string $class, string $property, array $context = []): ?bool
  83. {
  84. return $this->extract('isInitializable', [$class, $property, $context]);
  85. }
  86. /**
  87. * Retrieves the cached data if applicable or delegates to the decorated extractor.
  88. */
  89. private function extract(string $method, array $arguments): mixed
  90. {
  91. try {
  92. $serializedArguments = serialize($arguments);
  93. } catch (\Exception) {
  94. // If arguments are not serializable, skip the cache
  95. return $this->propertyInfoExtractor->{$method}(...$arguments);
  96. }
  97. // Calling rawurlencode escapes special characters not allowed in PSR-6's keys
  98. $key = rawurlencode($method.'.'.$serializedArguments);
  99. if (\array_key_exists($key, $this->arrayCache)) {
  100. return $this->arrayCache[$key];
  101. }
  102. $item = $this->cacheItemPool->getItem($key);
  103. if ($item->isHit()) {
  104. return $this->arrayCache[$key] = $item->get();
  105. }
  106. $value = $this->propertyInfoExtractor->{$method}(...$arguments);
  107. $item->set($value);
  108. $this->cacheItemPool->save($item);
  109. return $this->arrayCache[$key] = $value;
  110. }
  111. }