Debian Codegraph @

lib/3rdParty/composer/symfony/property-info/PropertyInfoCacheExtractor.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\PropertyInfo;
13
14 use Psr\Cache\CacheItemPoolInterface;
15 use Symfony\Component\PropertyInfo\Util\LegacyTypeConverter;
16 use Symfony\Component\TypeInfo\Type;
17
18 /**
19 * Adds a PSR-6 cache layer on top of an extractor.
20 *
21 * @author Kévin Dunglas <[email protected]>
22 *
23 * @final
24 */
25 class PropertyInfoCacheExtractor implements PropertyInfoExtractorInterface, PropertyInitializableExtractorInterface
26 {
27 private array $arrayCache = [];
28
29 public function __construct(
30 private readonly PropertyInfoExtractorInterface $propertyInfoExtractor,
31 private readonly CacheItemPoolInterface $cacheItemPool,
32 ) {
33 }
34
35 public function isReadable(string $class, string $property, array $context = []): ?bool
36 {
37 return $this->extract('isReadable', [$class, $property, $context]);
38 }
39
40 public function isWritable(string $class, string $property, array $context = []): ?bool
41 {
42 return $this->extract('isWritable', [$class, $property, $context]);
43 }
44
45 public function getShortDescription(string $class, string $property, array $context = []): ?string
46 {
47 return $this->extract('getShortDescription', [$class, $property, $context]);
48 }
49
50 public function getLongDescription(string $class, string $property, array $context = []): ?string
51 {
52 return $this->extract('getLongDescription', [$class, $property, $context]);
53 }
54
55 public function getProperties(string $class, array $context = []): ?array
56 {
57 return $this->extract('getProperties', [$class, $context]);
58 }
59
60 public function getType(string $class, string $property, array $context = []): ?Type
61 {
62 try {
63 $serializedArguments = serialize([$class, $property, $context]);
64 } catch (\Exception) {
65 // If arguments are not serializable, skip the cache
66 if (method_exists($this->propertyInfoExtractor, 'getType')) {
67 return $this->propertyInfoExtractor->getType($class, $property, $context);
68 }
69
70 return LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
71 }
72
73 // Calling rawurlencode escapes special characters not allowed in PSR-6's keys
74 $key = rawurlencode('getType.'.$serializedArguments);
75
76 if (\array_key_exists($key, $this->arrayCache)) {
77 return $this->arrayCache[$key];
78 }
79
80 $item = $this->cacheItemPool->getItem($key);
81
82 if ($item->isHit()) {
83 return $this->arrayCache[$key] = $item->get();
84 }
85
86 if (method_exists($this->propertyInfoExtractor, 'getType')) {
87 $value = $this->propertyInfoExtractor->getType($class, $property, $context);
88 } else {
89 $value = LegacyTypeConverter::toTypeInfoType($this->propertyInfoExtractor->getTypes($class, $property, $context));
90 }
91
92 $item->set($value);
93 $this->cacheItemPool->save($item);
94
95 return $this->arrayCache[$key] = $value;
96 }
97
98 /**
99 * @deprecated since Symfony 7.3, use "getType" instead
100 */
101 public function getTypes(string $class, string $property, array $context = []): ?array
102 {
103 trigger_deprecation('symfony/property-info', '7.3', 'The "%s()" method is deprecated, use "%s::getType()" instead.', __METHOD__, self::class);
104
105 return $this->extract('getTypes', [$class, $property, $context]);
106 }
107
108 public function isInitializable(string $class, string $property, array $context = []): ?bool
109 {
110 return $this->extract('isInitializable', [$class, $property, $context]);
111 }
112
113 /**
114 * Retrieves the cached data if applicable or delegates to the decorated extractor.
115 */
116 private function extract(string $method, array $arguments): mixed
117 {
118 try {
119 $serializedArguments = serialize($arguments);
120 } catch (\Exception) {
121 // If arguments are not serializable, skip the cache
122 return $this->propertyInfoExtractor->{$method}(...$arguments);
123 }
124
125 // Calling rawurlencode escapes special characters not allowed in PSR-6's keys
126 $key = rawurlencode($method.'.'.$serializedArguments);
127
128 if (\array_key_exists($key, $this->arrayCache)) {
129 return $this->arrayCache[$key];
130 }
131
132 $item = $this->cacheItemPool->getItem($key);
133
134 if ($item->isHit()) {
135 return $this->arrayCache[$key] = $item->get();
136 }
137
138 $value = $this->propertyInfoExtractor->{$method}(...$arguments);
139 $item->set($value);
140 $this->cacheItemPool->save($item);
141
142 return $this->arrayCache[$key] = $value;
143 }
144 }