From 289f63c2582b8644d712a17412d573e0612b577b Mon Sep 17 00:00:00 2001 From: Nathanael Noblet Date: Tue, 1 Nov 2016 13:44:13 -0600 Subject: [PATCH 1/3] Allow construction without constructor arguments Since we can dynamically add templates and its possible to not need to construct the loader without any initial templates. I've added the default argument to the constructor for simpler instantiation IE new Twig_Loader_Array();. This helps us on a project where we've extended the loader, and have no initial templates but need to add constructor parameters for no real reason. --- lib/Twig/Loader/Array.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Twig/Loader/Array.php b/lib/Twig/Loader/Array.php index 4e9ddd2f825..c0f31d35389 100644 --- a/lib/Twig/Loader/Array.php +++ b/lib/Twig/Loader/Array.php @@ -30,7 +30,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf * * @param array $templates An array of templates (keys are the names, and values are the source code) */ - public function __construct(array $templates) + public function __construct(array $templates = array()) { $this->templates = $templates; } From ff0abbb7e89dd18ff113c696f84830f44631ba84 Mon Sep 17 00:00:00 2001 From: Matthias Pigulla Date: Thu, 3 Nov 2016 10:27:16 +0100 Subject: [PATCH 2/3] Load templates from cache, even if they have just been compiled --- lib/Twig/Environment.php | 10 +++++++++- test/Twig/Tests/EnvironmentTest.php | 10 +++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index e7b5dc45b02..7a3ac5f7218 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -416,9 +416,17 @@ public function loadTemplate($name, $index = null) $this->writeCacheFile($key, $content); } else { $this->cache->write($key, $content); + $this->cache->load($key); } - eval('?>'.$content); + if (!class_exists($cls, false)) { + /* Last line of defense if either $this->bcWriteCacheFile was used, + * $this->cache is implemented as a no-op or we have a race condition + * where the cache was cleared between the above calls to write to and load from + * the cache. + */ + eval('?>'.$content); + } } } diff --git a/test/Twig/Tests/EnvironmentTest.php b/test/Twig/Tests/EnvironmentTest.php index 340ec453b47..fac0d5337bc 100644 --- a/test/Twig/Tests/EnvironmentTest.php +++ b/test/Twig/Tests/EnvironmentTest.php @@ -217,7 +217,9 @@ public function testAutoReloadCacheMiss() ->will($this->returnValue(0)); $loader->expects($this->never()) ->method('isFresh'); - $cache->expects($this->never()) + $cache->expects($this->once()) + ->method('write'); + $cache->expects($this->once()) ->method('load'); $twig->loadTemplate($templateName); @@ -245,7 +247,7 @@ public function testAutoReloadCacheHit() $loader->expects($this->once()) ->method('isFresh') ->will($this->returnValue(true)); - $cache->expects($this->once()) + $cache->expects($this->atLeastOnce()) ->method('load'); $twig->loadTemplate($templateName); @@ -271,7 +273,9 @@ public function testAutoReloadOutdatedCacheHit() $loader->expects($this->once()) ->method('isFresh') ->will($this->returnValue(false)); - $cache->expects($this->never()) + $cache->expects($this->once()) + ->method('write'); + $cache->expects($this->once()) ->method('load'); $twig->loadTemplate($templateName); From dc3533c3479c9ffd4ebcdf3dd8f0cdd24a5a19ff Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 8 Nov 2016 12:40:21 -0800 Subject: [PATCH 3/3] fixed typo --- lib/Twig/Profiler/NodeVisitor/Profiler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Twig/Profiler/NodeVisitor/Profiler.php b/lib/Twig/Profiler/NodeVisitor/Profiler.php index 7e837f631b6..099a44f53b6 100644 --- a/lib/Twig/Profiler/NodeVisitor/Profiler.php +++ b/lib/Twig/Profiler/NodeVisitor/Profiler.php @@ -41,14 +41,14 @@ protected function doLeaveNode(Twig_Node $node, Twig_Environment $env) } elseif ($node instanceof Twig_Node_Block) { $varName = $this->getVarName(); $node->setNode('body', new Twig_Node_Body(array( - new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getTemplateName(), $varName), + new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName), ))); } elseif ($node instanceof Twig_Node_Macro) { $varName = $this->getVarName(); $node->setNode('body', new Twig_Node_Body(array( - new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getTemplateName(), $varName), + new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName), )));