Debian Codegraph @

lib/3rdParty/composer/symfony/serializer/Mapping/Factory/CacheClassMetadataFactory.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\Serializer\Mapping\Factory;
13
14 use Psr\Cache\CacheItemPoolInterface;
15 use Symfony\Component\Serializer\Mapping\ClassMetadataInterface;
16
17 /**
18 * Caches metadata using a PSR-6 implementation.
19 *
20 * @author Kévin Dunglas <[email protected]>
21 */
22 class CacheClassMetadataFactory implements ClassMetadataFactoryInterface
23 {
24 use ClassResolverTrait;
25
26 /**
27 * @var array<string, ClassMetadataInterface>
28 */
29 private array $loadedClasses = [];
30
31 public function __construct(
32 private readonly ClassMetadataFactoryInterface $decorated,
33 private readonly CacheItemPoolInterface $cacheItemPool,
34 ) {
35 }
36
37 public function getMetadataFor(string|object $value): ClassMetadataInterface
38 {
39 $class = $this->getClass($value);
40
41 if (isset($this->loadedClasses[$class])) {
42 return $this->loadedClasses[$class];
43 }
44
45 $key = rawurlencode(strtr($class, '\\', '_'));
46
47 $item = $this->cacheItemPool->getItem($key);
48 if ($item->isHit()) {
49 return $this->loadedClasses[$class] = $item->get();
50 }
51
52 $metadata = $this->decorated->getMetadataFor($value);
53 $this->cacheItemPool->save($item->set($metadata));
54
55 return $this->loadedClasses[$class] = $metadata;
56 }
57
58 public function hasMetadataFor(mixed $value): bool
59 {
60 return $this->decorated->hasMetadataFor($value);
61 }
62 }