mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-14 19:36:43 +00:00
optimized template loading as the source code is now never fetched if the cache exists and autoreload is false (which should be the case for websites in production)
git-svn-id: http://svn.twig-project.org/trunk@129 93ef8e89-cb99-4229-a87c-7fa0fa45744b
This commit is contained in:
+36
-12
@@ -66,31 +66,34 @@ abstract class Twig_Loader implements Twig_LoaderInterface
|
||||
return $cls;
|
||||
}
|
||||
|
||||
list($template, $mtime) = $this->getSource($name);
|
||||
|
||||
if (false === $this->cache)
|
||||
{
|
||||
$this->evalString($template, $name);
|
||||
list($source, ) = $this->getSource($name);
|
||||
$this->evalString($source, $name);
|
||||
|
||||
return $cls;
|
||||
}
|
||||
|
||||
$cache = $this->getCacheFilename($name);
|
||||
if (!file_exists($cache) || false === $mtime || ($this->autoReload && (filemtime($cache) < $mtime)))
|
||||
if (!file_exists($cache))
|
||||
{
|
||||
// compile first to avoid empty files when an Exception occurs
|
||||
$content = $this->compile($template, $name);
|
||||
|
||||
$fp = @fopen($cache, 'wb');
|
||||
if (!$fp)
|
||||
list($source, $mtime) = $this->getSource($name);
|
||||
if (false === $mtime)
|
||||
{
|
||||
eval('?>'.$content);
|
||||
$this->evalString($source, $name);
|
||||
|
||||
return $cls;
|
||||
}
|
||||
fclose($fp);
|
||||
|
||||
file_put_contents($cache, $content);
|
||||
$this->save($this->compile($source, $name), $cache);
|
||||
}
|
||||
elseif ($this->autoReload)
|
||||
{
|
||||
list($source, $mtime) = $this->getSource($name);
|
||||
if (filemtime($cache) < $mtime)
|
||||
{
|
||||
$this->save($this->compile($source, $name), $cache);
|
||||
}
|
||||
}
|
||||
|
||||
require_once $cache;
|
||||
@@ -98,6 +101,27 @@ abstract class Twig_Loader implements Twig_LoaderInterface
|
||||
return $cls;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves a PHP string in the cache.
|
||||
*
|
||||
* If the cache file cannot be written, then the PHP string is evaluated.
|
||||
*
|
||||
* @param string $content The PHP string
|
||||
* @param string $cache The absolute path of the cache
|
||||
*/
|
||||
protected function save($content, $cache)
|
||||
{
|
||||
if ($fp = @fopen($cache, 'w'))
|
||||
{
|
||||
fclose($fp);
|
||||
file_put_contents($cache, $content);
|
||||
}
|
||||
else
|
||||
{
|
||||
eval('?>'.$content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Environment related to this loader.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user