vendor/symfony/framework-bundle/CacheWarmer/ValidatorCacheWarmer.php line 57

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
  11. use Doctrine\Common\Annotations\AnnotationException;
  12. use Psr\Cache\CacheItemPoolInterface;
  13. use Symfony\Component\Cache\Adapter\ArrayAdapter;
  14. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  15. use Symfony\Component\Validator\Mapping\Cache\Psr6Cache;
  16. use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
  17. use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
  18. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  19. use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  20. use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
  21. use Symfony\Component\Validator\ValidatorBuilderInterface;
  22. /**
  23.  * Warms up XML and YAML validator metadata.
  24.  *
  25.  * @author Titouan Galopin <galopintitouan@gmail.com>
  26.  */
  27. class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
  28. {
  29.     private $validatorBuilder;
  30.     /**
  31.      * @param string $phpArrayFile The PHP file where metadata are cached
  32.      */
  33.     public function __construct(ValidatorBuilderInterface $validatorBuilderstring $phpArrayFile)
  34.     {
  35.         if (< \func_num_args() && func_get_arg(2) instanceof CacheItemPoolInterface) {
  36.             @trigger_error(sprintf('The CacheItemPoolInterface $fallbackPool argument of "%s()" is deprecated since Symfony 4.2, you should not pass it anymore.'__METHOD__), E_USER_DEPRECATED);
  37.         }
  38.         parent::__construct($phpArrayFile);
  39.         $this->validatorBuilder $validatorBuilder;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function doWarmUp($cacheDirArrayAdapter $arrayAdapter)
  45.     {
  46.         if (!method_exists($this->validatorBuilder'getLoaders')) {
  47.             return false;
  48.         }
  49.         $loaders $this->validatorBuilder->getLoaders();
  50.         $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), new Psr6Cache($arrayAdapter));
  51.         foreach ($this->extractSupportedLoaders($loaders) as $loader) {
  52.             foreach ($loader->getMappedClasses() as $mappedClass) {
  53.                 try {
  54.                     if ($metadataFactory->hasMetadataFor($mappedClass)) {
  55.                         $metadataFactory->getMetadataFor($mappedClass);
  56.                     }
  57.                 } catch (AnnotationException $e) {
  58.                     // ignore failing annotations
  59.                 } catch (\Exception $e) {
  60.                     $this->ignoreAutoloadException($mappedClass$e);
  61.                 }
  62.             }
  63.         }
  64.         return true;
  65.     }
  66.     protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
  67.     {
  68.         // make sure we don't cache null values
  69.         parent::warmUpPhpArrayAdapter($phpArrayAdapterarray_filter($values));
  70.     }
  71.     /**
  72.      * @param LoaderInterface[] $loaders
  73.      *
  74.      * @return XmlFileLoader[]|YamlFileLoader[]
  75.      */
  76.     private function extractSupportedLoaders(array $loaders)
  77.     {
  78.         $supportedLoaders = [];
  79.         foreach ($loaders as $loader) {
  80.             if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) {
  81.                 $supportedLoaders[] = $loader;
  82.             } elseif ($loader instanceof LoaderChain) {
  83.                 $supportedLoaders array_merge($supportedLoaders$this->extractSupportedLoaders($loader->getLoaders()));
  84.             }
  85.         }
  86.         return $supportedLoaders;
  87.     }
  88. }