SerializerCacheWarmer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Serializer\Mapping\Factory\CacheClassMetadataFactory;
  14. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
  15. use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
  16. use Symfony\Component\Serializer\Mapping\Loader\LoaderInterface;
  17. use Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader;
  18. use Symfony\Component\Serializer\Mapping\Loader\YamlFileLoader;
  19. /**
  20. * Warms up XML and YAML serializer metadata.
  21. *
  22. * @author Titouan Galopin <galopintitouan@gmail.com>
  23. */
  24. class SerializerCacheWarmer extends AbstractPhpFileCacheWarmer
  25. {
  26. private $loaders;
  27. /**
  28. * @param LoaderInterface[] $loaders The serializer metadata loaders
  29. * @param string $phpArrayFile The PHP file where metadata are cached
  30. */
  31. public function __construct(array $loaders, string $phpArrayFile)
  32. {
  33. parent::__construct($phpArrayFile);
  34. $this->loaders = $loaders;
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter)
  40. {
  41. if (!class_exists(CacheClassMetadataFactory::class) || !method_exists(XmlFileLoader::class, 'getMappedClasses') || !method_exists(YamlFileLoader::class, 'getMappedClasses')) {
  42. return false;
  43. }
  44. $metadataFactory = new CacheClassMetadataFactory(new ClassMetadataFactory(new LoaderChain($this->loaders)), $arrayAdapter);
  45. foreach ($this->extractSupportedLoaders($this->loaders) as $loader) {
  46. foreach ($loader->getMappedClasses() as $mappedClass) {
  47. try {
  48. $metadataFactory->getMetadataFor($mappedClass);
  49. } catch (AnnotationException $e) {
  50. // ignore failing annotations
  51. } catch (\Exception $e) {
  52. $this->ignoreAutoloadException($mappedClass, $e);
  53. }
  54. }
  55. }
  56. return true;
  57. }
  58. /**
  59. * @param LoaderInterface[] $loaders
  60. *
  61. * @return XmlFileLoader[]|YamlFileLoader[]
  62. */
  63. private function extractSupportedLoaders(array $loaders): array
  64. {
  65. $supportedLoaders = [];
  66. foreach ($loaders as $loader) {
  67. if ($loader instanceof XmlFileLoader || $loader instanceof YamlFileLoader) {
  68. $supportedLoaders[] = $loader;
  69. } elseif ($loader instanceof LoaderChain) {
  70. $supportedLoaders = array_merge($supportedLoaders, $this->extractSupportedLoaders($loader->getLoaders()));
  71. }
  72. }
  73. return $supportedLoaders;
  74. }
  75. }