|
| 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\Bundle\FrameworkBundle\CacheWarmer; |
| 13 | + |
| 14 | +use Psr\Cache\CacheItemPoolInterface; |
| 15 | +use Symfony\Component\Cache\Adapter\AdapterInterface; |
| 16 | +use Symfony\Component\Cache\Adapter\ArrayAdapter; |
| 17 | +use Symfony\Component\Cache\Adapter\PhpArrayAdapter; |
| 18 | +use Symfony\Component\Cache\Adapter\ProxyAdapter; |
| 19 | +use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
| 20 | +use Symfony\Component\Serializer\Mapping\Factory\CacheClassMetadataFactory; |
| 21 | +use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory; |
| 22 | +use Symfony\Component\Serializer\Mapping\Loader\LoaderChain; |
| 23 | +use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface; |
| 24 | +use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader; |
| 25 | +use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader; |
| 26 | + |
| 27 | +/** |
| 28 | + * Warms up XML and YAML serializer metadata. |
| 29 | + * |
| 30 | + * @author Titouan Galopin <galopintitouan@gmail.com> |
| 31 | + */ |
| 32 | +class SerializerCacheWarmer implements CacheWarmerInterface |
| 33 | +{ |
| 34 | + private $loaders; |
| 35 | + private $phpArrayFile; |
| 36 | + private $fallbackPool; |
| 37 | + |
| 38 | + /** |
| 39 | + * @param LoaderInterface[] $loaders The serializer metadata loaders. |
| 40 | + * @param string $phpArrayFile The PHP file where metadata are cached. |
| 41 | + * @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered metadata are cached. |
| 42 | + */ |
| 43 | + public function __construct(array $loaders, $phpArrayFile, CacheItemPoolInterface $fallbackPool) |
| 44 | + { |
| 45 | + $this->loaders = $loaders; |
| 46 | + $this->phpArrayFile = $phpArrayFile; |
| 47 | + if (!$fallbackPool instanceof AdapterInterface) { |
| 48 | + $fallbackPool = new ProxyAdapter($fallbackPool); |
| 49 | + } |
| 50 | + $this->fallbackPool = $fallbackPool; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * {@inheritdoc} |
| 55 | + */ |
| 56 | + public function warmUp($cacheDir) |
| 57 | + { |
| 58 | + if (!class_exists(CacheClassMetadataFactory::class) |
| 59 | + || !method_exists(XmlFileLoader::class, 'getMappedClasses') |
| 60 | + || !method_exists(YamlFileLoader::class, 'getMappedClasses')) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + $adapter = new PhpArrayAdapter($this->phpArrayFile, $this->fallbackPool); |
| 65 | + $arrayPool = new ArrayAdapter(0, false); |
| 66 | + |
| 67 | + $metadataFactory = new CacheClassMetadataFactory( |
| 68 | + new ClassMetadataFactory(new LoaderChain($this->loaders)), |
| 69 | + $arrayPool |
| 70 | + ); |
| 71 | + |
| 72 | + foreach ($this->extractSupportedLoaders($this->loaders) as $loader) { |
| 73 | + foreach ($loader->getMappedClasses() as $mappedClass) { |
| 74 | + $metadataFactory->getMetadataFor($mappedClass); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $values = $arrayPool->getValues(); |
| 79 | + $adapter->warmUp($values); |
| 80 | + |
| 81 | + foreach ($values as $k => $v) { |
| 82 | + $item = $this->fallbackPool->getItem($k); |
| 83 | + $this->fallbackPool->saveDeferred($item->set($v)); |
| 84 | + } |
| 85 | + $this->fallbackPool->commit(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * {@inheritdoc} |
| 90 | + */ |
| 91 | + public function isOptional() |
| 92 | + { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param LoaderInterface[] $loaders |
| 98 | + * |
| 99 | + * @return XmlFileLoader[]|YamlFileLoader[] |
| 100 | + */ |
| 101 | + private function extractSupportedLoaders(array $loaders) |
| 102 | + { |
| 103 | + $supportedLoaders = array(); |
| 104 | + |
| 105 | + foreach ($loaders as $loader) { |
| 106 | + if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) { |
| 107 | + $supportedLoaders[] = $loader; |
| 108 | + } elseif ($loader instanceof LoaderChain) { |
| 109 | + $supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getDelegatedLoaders())); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + return $supportedLoaders; |
| 114 | + } |
| 115 | +} |
0 commit comments