bug #2216 Load templates from cache, even if they have just been compiled (mpdude)

This PR was squashed before being merged into the 1.x branch (closes #2216).

Discussion
----------

Load templates from cache, even if they have just been compiled

Previously, when the cache was empty, the compiled template would be written to it, but `eval()`d from `$content`. In that case, it is not possible to step through (read: debug) the compiled template because at least xDebug does not have a clue where the code comes from.

With this change, PHP/xDebug can tell even on the first run (with an empty cache) where the code was loaded from.

Commits
-------

ff0abbb Load templates from cache, even if they have just been compiled
This commit is contained in:
Fabien Potencier
2016-11-07 19:10:29 -08:00
2 changed files with 16 additions and 4 deletions
+9 -1
View File
@@ -421,9 +421,17 @@ class Twig_Environment
$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);
}
}
}
+7 -3
View File
@@ -217,7 +217,9 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
->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 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$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 @@ class Twig_Tests_EnvironmentTest extends PHPUnit_Framework_TestCase
$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);