TranslationsCacheWarmer.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. /**
  17. * Generates the catalogues for translations.
  18. *
  19. * @author Xavier Leune <xavier.leune@gmail.com>
  20. */
  21. class TranslationsCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
  22. {
  23. private $container;
  24. private $translator;
  25. public function __construct(ContainerInterface $container)
  26. {
  27. // As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
  28. $this->container = $container;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. *
  33. * @return string[]
  34. */
  35. public function warmUp(string $cacheDir)
  36. {
  37. if (null === $this->translator) {
  38. $this->translator = $this->container->get('translator');
  39. }
  40. if ($this->translator instanceof WarmableInterface) {
  41. return (array) $this->translator->warmUp($cacheDir);
  42. }
  43. return [];
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function isOptional()
  49. {
  50. return true;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public static function getSubscribedServices()
  56. {
  57. return [
  58. 'translator' => TranslatorInterface::class,
  59. ];
  60. }
  61. }