Skip to content

Commit 8d31598

Browse files
committed
Merge branch '2.x'
* 2.x: fixed typo Load templates from cache, even if they have just been compiled Allow construction without constructor arguments
2 parents 2b899d6 + b82accd commit 8d31598

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

lib/Twig/Environment.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,16 @@ public function loadTemplate($name, $index = null)
332332
if (!class_exists($cls, false)) {
333333
$content = $this->compileSource($this->getLoader()->getSourceContext($name));
334334
$this->cache->write($key, $content);
335-
eval('?>'.$content);
335+
$this->cache->load($key);
336+
337+
if (!class_exists($cls, false)) {
338+
/* Last line of defense if either $this->bcWriteCacheFile was used,
339+
* $this->cache is implemented as a no-op or we have a race condition
340+
* where the cache was cleared between the above calls to write to and load from
341+
* the cache.
342+
*/
343+
eval('?>'.$content);
344+
}
336345
}
337346
}
338347

lib/Twig/Loader/Array.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class Twig_Loader_Array implements Twig_LoaderInterface, Twig_ExistsLoaderInterf
3030
*
3131
* @param array $templates An array of templates (keys are the names, and values are the source code)
3232
*/
33-
public function __construct(array $templates)
33+
public function __construct(array $templates = array())
3434
{
3535
$this->templates = $templates;
3636
}

lib/Twig/Profiler/NodeVisitor/Profiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
4141
} elseif ($node instanceof Twig_Node_Block) {
4242
$varName = $this->getVarName();
4343
$node->setNode('body', new Twig_Node_Body(array(
44-
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getTemplateName(), $varName),
44+
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName),
4545
$node->getNode('body'),
4646
new Twig_Profiler_Node_LeaveProfile($varName),
4747
)));
4848
} elseif ($node instanceof Twig_Node_Macro) {
4949
$varName = $this->getVarName();
5050
$node->setNode('body', new Twig_Node_Body(array(
51-
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getTemplateName(), $varName),
51+
new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName),
5252
$node->getNode('body'),
5353
new Twig_Profiler_Node_LeaveProfile($varName),
5454
)));

test/Twig/Tests/EnvironmentTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ public function testAutoReloadCacheMiss()
178178
->will($this->returnValue(0));
179179
$loader->expects($this->never())
180180
->method('isFresh');
181-
$cache->expects($this->never())
181+
$cache->expects($this->once())
182+
->method('write');
183+
$cache->expects($this->once())
182184
->method('load');
183185

184186
$twig->loadTemplate($templateName);
@@ -206,7 +208,7 @@ public function testAutoReloadCacheHit()
206208
$loader->expects($this->once())
207209
->method('isFresh')
208210
->will($this->returnValue(true));
209-
$cache->expects($this->once())
211+
$cache->expects($this->atLeastOnce())
210212
->method('load');
211213

212214
$twig->loadTemplate($templateName);
@@ -232,7 +234,9 @@ public function testAutoReloadOutdatedCacheHit()
232234
$loader->expects($this->once())
233235
->method('isFresh')
234236
->will($this->returnValue(false));
235-
$cache->expects($this->never())
237+
$cache->expects($this->once())
238+
->method('write');
239+
$cache->expects($this->once())
236240
->method('load');
237241

238242
$twig->loadTemplate($templateName);

0 commit comments

Comments
 (0)