TwigExtensionPass.php 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Drupal\Core\DependencyInjection\Compiler;
  3. use Drupal\Component\Utility\Crypt;
  4. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. /**
  7. * Adds the twig_extension_hash parameter to the container.
  8. *
  9. * Parameter twig_extension_hash is a hash of all extension mtimes for Twig
  10. * template invalidation.
  11. */
  12. class TwigExtensionPass implements CompilerPassInterface {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function process(ContainerBuilder $container) {
  17. $twig_extension_hash = '';
  18. foreach (array_keys($container->findTaggedServiceIds('twig.extension')) as $service_id) {
  19. $class_name = $container->getDefinition($service_id)->getClass();
  20. $reflection = new \ReflectionClass($class_name);
  21. // We use the class names as hash in order to invalidate on new extensions
  22. // and mtime for every time we change an existing file.
  23. $twig_extension_hash .= $class_name . filemtime($reflection->getFileName());
  24. }
  25. $container->setParameter('twig_extension_hash', Crypt::hashBase64($twig_extension_hash));
  26. }
  27. }