Skip to content

Commit

Permalink
Merge branch '1.x' into 2.x
Browse files Browse the repository at this point in the history
* 1.x:
  Turned fatal error into exception when a previously generated cache prevents loading its newly compiled version
  • Loading branch information
fabpot committed Jan 11, 2017
2 parents 5527658 + feb9bf5 commit 9ef01e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
Expand Up @@ -28,6 +28,8 @@
* added Twig_NodeCaptureInterface for nodes that capture all output
* fixed marking the environment as initialized too early
* fixed C89 compat for the C extension
* turned fatal error into exception when a previously generated cache is corrupted
* fixed offline cache warm-ups for embedded templates

* 1.30.0 (2016-12-23)

Expand Down
18 changes: 13 additions & 5 deletions lib/Twig/Environment.php
Expand Up @@ -334,21 +334,25 @@ public function load($name)
*
* @return Twig_Template A template instance representing the given template name
*
* @throws Twig_Error_Loader When the template cannot be found
* @throws Twig_Error_Syntax When an error occurred during compilation
* @throws Twig_Error_Loader When the template cannot be found
* @throws Twig_Error_Runtime When a previously generated cache is corrupted
* @throws Twig_Error_Syntax When an error occurred during compilation
*
* @internal
*/
public function loadTemplate($name, $index = null)
{
$cls = $this->getTemplateClass($name, $index);
$cls = $mainCls = $this->getTemplateClass($name);
if (null !== $index) {
$cls .= '_'.$index;
}

if (isset($this->loadedTemplates[$cls])) {
return $this->loadedTemplates[$cls];
}

if (!class_exists($cls, false)) {
$key = $this->cache->generateKey($name, $cls);
$key = $this->cache->generateKey($name, $mainCls);

if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache->getTimestamp($key))) {
$this->cache->load($key);
Expand All @@ -359,7 +363,7 @@ public function loadTemplate($name, $index = null)
$this->cache->write($key, $content);
$this->cache->load($key);

if (!class_exists($cls, false)) {
if (!class_exists($mainCls, 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
Expand All @@ -368,6 +372,10 @@ public function loadTemplate($name, $index = null)
eval('?>'.$content);
}
}

if (!class_exists($cls, false)) {
throw new Twig_Error_Runtime(sprintf('Failed to load Twig template "%s", index "%s": cache is corrupted.', $name, $index), -1, $source);
}
}

// to be removed in 3.0
Expand Down

0 comments on commit 9ef01e8

Please sign in to comment.