+ /**
+ * {@inheritdoc}
+ */
+ public function register(ContainerBuilder $container) {
+ parent::register($container);
+
+ $container->setDefinition('twig_loader__file_system', new Definition('Twig_Loader_Filesystem', [[sys_get_temp_dir()]]))
+ ->addTag('twig.loader');
+ }
+
+ /**
+ * Test template invalidation.
+ */
+ public function testTemplateInvalidation() {
+ $template_before = <<<TWIG
+<div>Hello before</div>
+TWIG;
+ $template_after = <<<TWIG
+<div>Hello after</div>
+TWIG;
+
+ $tempfile = tempnam(sys_get_temp_dir(), '__METHOD__') . '.html.twig';
+ file_put_contents($tempfile, $template_before);
+
+ /** @var \Drupal\Core\Template\TwigEnvironment $environment */
+ $environment = \Drupal::service('twig');
+
+ $output = $environment->load(basename($tempfile))->render();
+ $this->assertEquals($template_before, $output);
+
+ file_put_contents($tempfile, $template_after);
+ $output = $environment->load(basename($tempfile))->render();
+ $this->assertEquals($template_before, $output);
+
+ $environment->invalidate();
+ // Manually change $templateClassPrefix to force a different template
+ // classname, as the other class is still loaded. This wouldn't be a problem
+ // on a real site where you reload the page.
+ $reflection = new \ReflectionClass($environment);
+ $property_reflection = $reflection->getProperty('templateClassPrefix');
+ $property_reflection->setAccessible(TRUE);
+ $property_reflection->setValue($environment, 'otherPrefix');
+
+ $output = $environment->load(basename($tempfile))->render();
+ $this->assertEquals($template_after, $output);
+ }
+