diff --git a/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php b/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php index fe1ae25..7f74cb0 100644 --- a/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php +++ b/core/lib/Drupal/Core/Template/Loader/ThemeRegistryLoader.php @@ -38,6 +38,8 @@ public function __construct(Registry $theme_registry) { * * @param string $name * The name of the template to load. + * @param bool $throw + * Whether to throw an exception when an error occurs * * @return string * The path to the template. @@ -45,7 +47,7 @@ public function __construct(Registry $theme_registry) { * @throws \Twig_Error_Loader * Thrown if a template matching $name cannot be found. */ - protected function findTemplate($name) { + protected function findTemplate($name, $throw = TRUE) { // Allow for loading based on the Drupal theme registry. $hook = str_replace('.html.twig', '', strtr($name, '-', '_')); $theme_registry = $this->themeRegistry->getRuntime(); @@ -63,7 +65,9 @@ protected function findTemplate($name) { } } - throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name)); + if ($throw) { + throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" in the Drupal theme registry.', $name)); + } } } diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php index cb4df85..29de1e6 100644 --- a/core/lib/Drupal/Core/Template/TwigEnvironment.php +++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php @@ -120,11 +120,11 @@ public function getCacheFilename($name) { // and their mtime. // @see \Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass - if (!$this->cache) { + if (!$this->getCache()) { return FALSE; } - $class = substr($this->getTemplateClass($name), strlen($this->templateClassPrefix)); + $class = substr($this->getTemplateClass($name), strlen($this->getTemplateClassPrefix())); // The first part is what is invalidated. return $this->templateCacheFilenamePrefix . '_' . basename($name) . '_' . $class; @@ -216,7 +216,7 @@ public function getTemplateClass($name, $index = NULL) { // node.html.twig for the output of each node and the same compiled class. $cache_index = $name . (NULL === $index ? '' : '_' . $index); if (!isset($this->templateClasses[$cache_index])) { - $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index); + $this->templateClasses[$cache_index] = $this->getTemplateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index); } return $this->templateClasses[$cache_index]; } diff --git a/core/lib/Drupal/Core/Template/TwigNodeTrans.php b/core/lib/Drupal/Core/Template/TwigNodeTrans.php index a7b723a..fe2753a 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeTrans.php +++ b/core/lib/Drupal/Core/Template/TwigNodeTrans.php @@ -25,7 +25,7 @@ class TwigNodeTrans extends \Twig_Node { /** * {@inheritdoc} */ - public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) { + public function __construct(\Twig_Node $body, \Twig_Node $plural = NULL, \Twig_Node_Expression $count = NULL, \Twig_Node_Expression $options = NULL, $lineno, $tag = NULL) { parent::__construct(array( 'count' => $count, 'body' => $body, @@ -91,7 +91,7 @@ public function compile(\Twig_Compiler $compiler) { /** * Extracts the text and tokens for the "trans" tag. * - * @param \Twig_NodeInterface $body + * @param \Twig_Node $body * The node to compile. * * @return array @@ -101,7 +101,7 @@ public function compile(\Twig_Compiler $compiler) { * - array $tokens * The extracted tokens as new \Twig_Node_Expression_Name instances. */ - protected function compileString(\Twig_NodeInterface $body) { + protected function compileString(\Twig_Node $body) { if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant || $body instanceof \Twig_Node_Expression_TempName) { return array($body, array()); } diff --git a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php index ac1e676..860cc56 100644 --- a/core/lib/Drupal/Core/Template/TwigNodeVisitor.php +++ b/core/lib/Drupal/Core/Template/TwigNodeVisitor.php @@ -16,19 +16,19 @@ * * @see twig_render */ -class TwigNodeVisitor implements \Twig_NodeVisitorInterface { +class TwigNodeVisitor extends \Twig_BaseNodeVisitor { /** * {@inheritdoc} */ - public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env) { + protected function doEnterNode(\Twig_Node $node, \Twig_Environment $env) { return $node; } /** * {@inheritdoc} */ - public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env) { + protected function doLeaveNode(\Twig_Node $node, \Twig_Environment $env) { // We use this to inject a call to render_var -> TwigExtension->renderVar() // before anything is printed. if ($node instanceof \Twig_Node_Print) { diff --git a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php index c67f463..009cc9c 100644 --- a/core/lib/Drupal/Core/Template/TwigTransTokenParser.php +++ b/core/lib/Drupal/Core/Template/TwigTransTokenParser.php @@ -82,14 +82,14 @@ public function getTag() { /** * Ensure that any nodes that are parsed are only of allowed types. * - * @param \Twig_NodeInterface $body + * @param \Twig_Node $body * The expression to check. * @param integer $lineno * The source line. * * @throws \Twig_Error_Syntax */ - protected function checkTransString(\Twig_NodeInterface $body, $lineno) { + protected function checkTransString(\Twig_Node $body, $lineno) { foreach ($body as $node) { if ( $node instanceof \Twig_Node_Text diff --git a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php index 3c2b851..c4c6025 100644 --- a/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php +++ b/core/modules/system/tests/modules/twig_extension_test/src/TwigExtension/TestExtension.php @@ -12,7 +12,7 @@ /** * A test Twig extension that adds a custom function and a custom filter. */ -class TestExtension extends TwigExtension { +class TestExtension extends \Twig_Extension { /** * Generates a list of all Twig functions that this extension defines. @@ -27,9 +27,9 @@ class TestExtension extends TwigExtension { * The value is a standard PHP callback that defines what the function does. */ public function getFunctions() { - return array( - 'testfunc' => new \Twig_Function_Function(array('Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction')), - ); + return [ + new \Twig_SimpleFunction('testfunc', [$this, 'testFunction']), + ]; } /** @@ -45,9 +45,9 @@ public function getFunctions() { * The value is a standard PHP callback that defines what the filter does. */ public function getFilters() { - return array( - 'testfilter' => new \Twig_Filter_Function(array('Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter')), - ); + return [ + new \Twig_SimpleFilter('testfilter', [$this, 'testFilter']), + ]; } /** diff --git a/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml b/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml index 491d1e8..8784c0f 100644 --- a/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml +++ b/core/modules/system/tests/modules/twig_extension_test/twig_extension_test.services.yml @@ -1,6 +1,5 @@ services: twig_extension_test.twig.test_extension: - arguments: ['@renderer'] class: Drupal\twig_extension_test\TwigExtension\TestExtension tags: - { name: twig.extension } diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php index b4a192d..7060401 100644 --- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php +++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php @@ -10,6 +10,7 @@ use Drupal\Core\Template\Attribute; use Drupal\Core\Template\AttributeArray; use Drupal\Core\Template\AttributeString; +use Drupal\Core\Template\Loader\StringLoader; use Drupal\Tests\UnitTestCase; /** @@ -253,7 +254,7 @@ public function testChainAddRemoveClasses() { * @covers ::addClass */ public function testTwigAddRemoveClasses($template, $expected, $seed_attributes = array()) { - $loader = new \Twig_Loader_String(); + $loader = new StringLoader(); $twig = new \Twig_Environment($loader); $data = array('attributes' => new Attribute($seed_attributes)); $result = $twig->render($template, $data); diff --git a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php index 3e78e91..7d55719 100644 --- a/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php +++ b/core/tests/Drupal/Tests/Core/Template/TwigExtensionTest.php @@ -12,6 +12,7 @@ use Drupal\Core\Render\RendererInterface; use Drupal\Core\Template\TwigEnvironment; use Drupal\Core\Template\TwigExtension; +use Drupal\Core\Template\Loader\StringLoader; use Drupal\Tests\UnitTestCase; /** @@ -30,7 +31,8 @@ class TwigExtensionTest extends UnitTestCase { */ public function testEscaping($template, $expected) { $renderer = $this->getMock('\Drupal\Core\Render\RendererInterface'); - $twig = new \Twig_Environment(NULL, array( + $loader = new \Twig_Loader_Filesystem(); + $twig = new \Twig_Environment($loader, array( 'debug' => TRUE, 'cache' => FALSE, 'autoescape' => 'html', @@ -96,7 +98,7 @@ public function testActiveTheme() { ->willReturn($active_theme); $extension->setThemeManager($theme_manager); - $loader = new \Twig_Loader_String(); + $loader = new StringLoader(); $twig = new \Twig_Environment($loader); $twig->addExtension($extension); $result = $twig->render('{{ active_theme() }}'); @@ -110,7 +112,8 @@ public function testActiveTheme() { */ public function testSafeStringEscaping() { $renderer = $this->getMock('\Drupal\Core\Render\RendererInterface'); - $twig = new \Twig_Environment(NULL, array( + $loader = new \Twig_Loader_Filesystem(); + $twig = new \Twig_Environment($loader, array( 'debug' => TRUE, 'cache' => FALSE, 'autoescape' => 'html',