Skip to content

Commit

Permalink
Merge branch '2.x'
Browse files Browse the repository at this point in the history
* 2.x:
  fixed typo
  Load templates from cache, even if they have just been compiled
  Allow construction without constructor arguments
  • Loading branch information
fabpot committed Nov 10, 2016
2 parents 2b899d6 + b82accd commit 8d31598
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
11 changes: 10 additions & 1 deletion lib/Twig/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,16 @@ public function loadTemplate($name, $index = null)
if (!class_exists($cls, false)) {
$content = $this->compileSource($this->getLoader()->getSourceContext($name));
$this->cache->write($key, $content);
eval('?>'.$content);
$this->cache->load($key);

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);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Loader/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Twig/Profiler/NodeVisitor/Profiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
)));
Expand Down
10 changes: 7 additions & 3 deletions test/Twig/Tests/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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);
Expand Down Expand Up @@ -206,7 +208,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);
Expand All @@ -232,7 +234,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);
Expand Down

0 comments on commit 8d31598

Please sign in to comment.