CompiledClassMetadataCacheWarmer.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\Component\Serializer\CacheWarmer;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryCompiler;
  14. use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
  15. /**
  16. * @author Fabien Bourigault <bourigaultfabien@gmail.com>
  17. */
  18. final class CompiledClassMetadataCacheWarmer implements CacheWarmerInterface
  19. {
  20. private $classesToCompile;
  21. private $classMetadataFactory;
  22. private $classMetadataFactoryCompiler;
  23. private $filesystem;
  24. public function __construct(array $classesToCompile, ClassMetadataFactoryInterface $classMetadataFactory, ClassMetadataFactoryCompiler $classMetadataFactoryCompiler, Filesystem $filesystem)
  25. {
  26. $this->classesToCompile = $classesToCompile;
  27. $this->classMetadataFactory = $classMetadataFactory;
  28. $this->classMetadataFactoryCompiler = $classMetadataFactoryCompiler;
  29. $this->filesystem = $filesystem;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function warmUp($cacheDir)
  35. {
  36. $metadatas = [];
  37. foreach ($this->classesToCompile as $classToCompile) {
  38. $metadatas[] = $this->classMetadataFactory->getMetadataFor($classToCompile);
  39. }
  40. $code = $this->classMetadataFactoryCompiler->compile($metadatas);
  41. $this->filesystem->dumpFile("{$cacheDir}/serializer.class.metadata.php", $code);
  42. return [];
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function isOptional()
  48. {
  49. return true;
  50. }
  51. }