0b904c14400d019774ccc20988c1202bdbf6a6f5
[yaffs-website] / serializer / Mapping / Factory / CacheClassMetadataFactory.php
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
12 namespace Symfony\Component\Serializer\Mapping\Factory;
13
14 use Psr\Cache\CacheItemPoolInterface;
15
16 /**
17  * Caches metadata using a PSR-6 implementation.
18  *
19  * @author Kévin Dunglas <dunglas@gmail.com>
20  */
21 class CacheClassMetadataFactory implements ClassMetadataFactoryInterface
22 {
23     use ClassResolverTrait;
24
25     /**
26      * @var ClassMetadataFactoryInterface
27      */
28     private $decorated;
29
30     /**
31      * @var CacheItemPoolInterface
32      */
33     private $cacheItemPool;
34
35     public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemPoolInterface $cacheItemPool)
36     {
37         $this->decorated = $decorated;
38         $this->cacheItemPool = $cacheItemPool;
39     }
40
41     /**
42      * {@inheritdoc}
43      */
44     public function getMetadataFor($value)
45     {
46         $class = $this->getClass($value);
47         // Key cannot contain backslashes according to PSR-6
48         $key = strtr($class, '\\', '_');
49
50         $item = $this->cacheItemPool->getItem($key);
51         if ($item->isHit()) {
52             return $item->get();
53         }
54
55         $metadata = $this->decorated->getMetadataFor($value);
56         $this->cacheItemPool->save($item->set($metadata));
57
58         return $metadata;
59     }
60
61     /**
62      * {@inheritdoc}
63      */
64     public function hasMetadataFor($value)
65     {
66         return $this->decorated->hasMetadataFor($value);
67     }
68 }