vendor/symfony/twig-bundle/CacheWarmer/TemplateCacheWarmer.php line 43

Open in your IDE?
  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\TwigBundle\CacheWarmer;
  11. use Psr\Container\ContainerInterface;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
  13. use Symfony\Contracts\Service\ServiceSubscriberInterface;
  14. use Twig\Environment;
  15. use Twig\Error\Error;
  16. /**
  17.  * Generates the Twig cache for all templates.
  18.  *
  19.  * @author Fabien Potencier <fabien@symfony.com>
  20.  */
  21. class TemplateCacheWarmer implements CacheWarmerInterface, ServiceSubscriberInterface
  22. {
  23.     private $container;
  24.     private $twig;
  25.     private $iterator;
  26.     public function __construct(ContainerInterface $container, iterable $iterator)
  27.     {
  28.         // As this cache warmer is optional, dependencies should be lazy-loaded, that's why a container should be injected.
  29.         $this->container = $container;
  30.         $this->iterator = $iterator;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      *
  35.      * @return string[] A list of template files to preload on PHP 7.4+
  36.      */
  37.     public function warmUp(string $cacheDir)
  38.     {
  39.         if (null === $this->twig) {
  40.             $this->twig = $this->container->get('twig');
  41.         }
  42.         $files = [];
  43.         foreach ($this->iterator as $template) {
  44.             try {
  45.                 $template = $this->twig->load($template);
  46.                 if (\is_callable([$template, 'unwrap'])) {
  47.                     $files[] = (new \ReflectionClass($template->unwrap()))->getFileName();
  48.                 }
  49.             } catch (Error $e) {
  50.                 /*
  51.                  * Problem during compilation, give up for this template (e.g. syntax errors).
  52.                  * Failing silently here allows to ignore templates that rely on functions that aren't available in
  53.                  * the current environment. For example, the WebProfilerBundle shouldn't be available in the prod
  54.                  * environment, but some templates that are never used in prod might rely on functions the bundle provides.
  55.                  * As we can't detect which templates are "really" important, we try to load all of them and ignore
  56.                  * errors. Error checks may be performed by calling the lint:twig command.
  57.                  */
  58.             }
  59.         }
  60.         return $files;
  61.     }
  62.     /**
  63.      * {@inheritdoc}
  64.      */
  65.     public function isOptional()
  66.     {
  67.         return true;
  68.     }
  69.     /**
  70.      * {@inheritdoc}
  71.      */
  72.     public static function getSubscribedServices()
  73.     {
  74.         return [
  75.             'twig' => Environment::class,
  76.         ];
  77.     }
  78. }