TranslationsCacheWarmer.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface;
  14. use Symfony\Component\Translation\TranslatorInterface;
  15. /**
  16. * Generates the catalogues for translations.
  17. *
  18. * @author Xavier Leune <xavier.leune@gmail.com>
  19. */
  20. class TranslationsCacheWarmer implements CacheWarmerInterface
  21. {
  22. private $container;
  23. private $translator;
  24. /**
  25. * TranslationsCacheWarmer constructor.
  26. *
  27. * @param ContainerInterface|TranslatorInterface $container
  28. */
  29. public function __construct($container)
  30. {
  31. // As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
  32. if ($container instanceof ContainerInterface) {
  33. $this->container = $container;
  34. } elseif ($container instanceof TranslatorInterface) {
  35. $this->translator = $container;
  36. } else {
  37. throw new \InvalidArgumentException(sprintf('%s only accepts instance of Symfony\Component\DependencyInjection\ContainerInterface or Symfony\Component\Translation\TranslatorInterface as first argument.', __CLASS__));
  38. }
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function warmUp($cacheDir)
  44. {
  45. if (null === $this->translator) {
  46. $this->translator = $this->container->get('translator');
  47. }
  48. if ($this->translator instanceof WarmableInterface) {
  49. $this->translator->warmUp($cacheDir);
  50. }
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function isOptional()
  56. {
  57. return true;
  58. }
  59. }