TwigExtensionPass.php 1.11 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\DependencyInjection\Compiler;

5
use Drupal\Component\Utility\Crypt;
6 7 8 9 10 11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
 * Adds the twig_extension_hash parameter to the container.
 *
12 13
 * Parameter twig_extension_hash is a hash of all extension mtimes for Twig
 * template invalidation.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 */
class TwigExtensionPass implements CompilerPassInterface {

  /**
   * {@inheritdoc}
   */
  public function process(ContainerBuilder $container) {
    $twig_extension_hash = '';
    foreach (array_keys($container->findTaggedServiceIds('twig.extension')) as $service_id) {
      $class_name = $container->getDefinition($service_id)->getClass();
      $reflection = new \ReflectionClass($class_name);
      // We use the class names as hash in order to invalidate on new extensions
      // and mtime for every time we change an existing file.
      $twig_extension_hash .= $class_name . filemtime($reflection->getFileName());
    }

30
    $container->setParameter('twig_extension_hash', Crypt::hashBase64($twig_extension_hash));
31 32 33
  }

}