4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Serializer\Mapping\Factory;
14 use Psr\Cache\CacheItemPoolInterface;
17 * Caches metadata using a PSR-6 implementation.
19 * @author Kévin Dunglas <dunglas@gmail.com>
21 class CacheClassMetadataFactory implements ClassMetadataFactoryInterface
23 use ClassResolverTrait;
26 * @var ClassMetadataFactoryInterface
31 * @var CacheItemPoolInterface
33 private $cacheItemPool;
35 public function __construct(ClassMetadataFactoryInterface $decorated, CacheItemPoolInterface $cacheItemPool)
37 $this->decorated = $decorated;
38 $this->cacheItemPool = $cacheItemPool;
44 public function getMetadataFor($value)
46 $class = $this->getClass($value);
47 // Key cannot contain backslashes according to PSR-6
48 $key = strtr($class, '\\', '_');
50 $item = $this->cacheItemPool->getItem($key);
55 $metadata = $this->decorated->getMetadataFor($value);
56 $this->cacheItemPool->save($item->set($metadata));
64 public function hasMetadataFor($value)
66 return $this->decorated->hasMetadataFor($value);