mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 11:56:50 +00:00
bug #2354 Turned fatal error into exception when a previously generated cache prevents loading its newly compiled version (nicolas-grekas)
This PR was merged into the 1.x branch.
Discussion
----------
Turned fatal error into exception when a previously generated cache prevents loading its newly compiled version
Not sure if this is testable, yet this should save some hours to people that end up in the same trap we did on our project.
It turned out we generated some twig template cache in two separate commands, in a situation where "index" tracking for embeds where lost.
This PR detects the situation and invites to clear the cache properly, because that's the only way to fix the issue.
It also fixes a mistake in the cache $key generation, that should not take $index into account.
Doing so were making offline cache warmup useless in some situations.
Commits
-------
95c10991 Turned fatal error into exception when a previously generated cache prevents loading its newly compiled version
This commit is contained in:
@@ -3,6 +3,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)
|
||||
|
||||
|
||||
@@ -408,14 +408,18 @@ class Twig_Environment
|
||||
*
|
||||
* @return Twig_TemplateInterface 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];
|
||||
@@ -425,7 +429,7 @@ class Twig_Environment
|
||||
if ($this->bcGetCacheFilename) {
|
||||
$key = $this->getCacheFilename($name);
|
||||
} else {
|
||||
$key = $this->cache->generateKey($name, $cls);
|
||||
$key = $this->cache->generateKey($name, $mainCls);
|
||||
}
|
||||
|
||||
if (!$this->isAutoReload() || $this->isTemplateFresh($name, $this->cache->getTimestamp($key))) {
|
||||
@@ -449,7 +453,7 @@ class Twig_Environment
|
||||
$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
|
||||
@@ -458,6 +462,10 @@ class Twig_Environment
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$this->runtimeInitialized) {
|
||||
|
||||
Reference in New Issue
Block a user