ValidatorCacheWarmer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Symfony\Component\Cache\Adapter\ArrayAdapter;
  13. use Symfony\Component\Cache\Adapter\PhpArrayAdapter;
  14. use Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory;
  15. use Symfony\Component\Validator\Mapping\Loader\LoaderChain;
  16. use Symfony\Component\Validator\Mapping\Loader\LoaderInterface;
  17. use Symfony\Component\Validator\Mapping\Loader\XmlFileLoader;
  18. use Symfony\Component\Validator\Mapping\Loader\YamlFileLoader;
  19. use Symfony\Component\Validator\ValidatorBuilder;
  20. /**
  21. * Warms up XML and YAML validator metadata.
  22. *
  23. * @author Titouan Galopin <galopintitouan@gmail.com>
  24. */
  25. class ValidatorCacheWarmer extends AbstractPhpFileCacheWarmer
  26. {
  27. private $validatorBuilder;
  28. /**
  29. * @param string $phpArrayFile The PHP file where metadata are cached
  30. */
  31. public function __construct(ValidatorBuilder $validatorBuilder, string $phpArrayFile)
  32. {
  33. parent::__construct($phpArrayFile);
  34. $this->validatorBuilder = $validatorBuilder;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter)
  40. {
  41. if (!method_exists($this->validatorBuilder, 'getLoaders')) {
  42. return false;
  43. }
  44. $loaders = $this->validatorBuilder->getLoaders();
  45. $metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter);
  46. foreach ($this->extractSupportedLoaders($loaders) as $loader) {
  47. foreach ($loader->getMappedClasses() as $mappedClass) {
  48. try {
  49. if ($metadataFactory->hasMetadataFor($mappedClass)) {
  50. $metadataFactory->getMetadataFor($mappedClass);
  51. }
  52. } catch (AnnotationException $e) {
  53. // ignore failing annotations
  54. } catch (\Exception $e) {
  55. $this->ignoreAutoloadException($mappedClass, $e);
  56. }
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * @return string[] A list of classes to preload on PHP 7.4+
  63. */
  64. protected function warmUpPhpArrayAdapter(PhpArrayAdapter $phpArrayAdapter, array $values)
  65. {
  66. // make sure we don't cache null values
  67. return parent::warmUpPhpArrayAdapter($phpArrayAdapter, array_filter($values));
  68. }
  69. /**
  70. * @param LoaderInterface[] $loaders
  71. *
  72. * @return XmlFileLoader[]|YamlFileLoader[]
  73. */
  74. private function extractSupportedLoaders(array $loaders): array
  75. {
  76. $supportedLoaders = [];
  77. foreach ($loaders as $loader) {
  78. if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) {
  79. $supportedLoaders[] = $loader;
  80. } elseif ($loader instanceof LoaderChain) {
  81. $supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getLoaders()));
  82. }
  83. }
  84. return $supportedLoaders;
  85. }
  86. }