Skip to content

Commit

Permalink
optimized the filesystem loader
Browse files Browse the repository at this point in the history
* added a cache for templates that do not exist
* sped up Filesystem::exists() by avoiding the creation of exceptions

same as d2b97b4 that was reverted because of a BC break
and same as what is in 2.0 (but in a BC way).
  • Loading branch information
fabpot committed Aug 22, 2015
1 parent 5fdbd99 commit 21b128a
Showing 1 changed file with 27 additions and 7 deletions.
34 changes: 27 additions & 7 deletions lib/Twig/Loader/Filesystem.php
Expand Up @@ -21,6 +21,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI

protected $paths = array();
protected $cache = array();
protected $errorCache = array();

/**
* Constructor.
Expand Down Expand Up @@ -87,7 +88,7 @@ public function setPaths($paths, $namespace = self::MAIN_NAMESPACE)
public function addPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
$this->cache = array();
$this->cache = $this->errorCache = array();

if (!is_dir($path)) {
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
Expand All @@ -107,7 +108,7 @@ public function addPath($path, $namespace = self::MAIN_NAMESPACE)
public function prependPath($path, $namespace = self::MAIN_NAMESPACE)
{
// invalidate the cache
$this->cache = array();
$this->cache = $this->errorCache = array();

if (!is_dir($path)) {
throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path));
Expand Down Expand Up @@ -150,9 +151,7 @@ public function exists($name)
}

try {
$this->findTemplate($name);

return true;
return false !== $this->findTemplate($name, false);
} catch (Twig_Error_Loader $exception) {
return false;
}
Expand All @@ -168,18 +167,33 @@ public function isFresh($name, $time)

protected function findTemplate($name)
{
$throw = func_num_args() > 1 ? func_get_arg(1) : true;
$name = $this->normalizeName($name);

if (isset($this->cache[$name])) {
return $this->cache[$name];
}

if (isset($this->errorCache[$name])) {
if (!$throw) {
return false;
}

throw new Twig_Error_Loader($this->errorCache[$name]);
}

$this->validateName($name);

list($namespace, $shortname) = $this->parseName($name);

if (!isset($this->paths[$namespace])) {
throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace));
$this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace);

if (!$throw) {
return false;
}

throw new Twig_Error_Loader($this->errorCache[$name]);
}

foreach ($this->paths[$namespace] as $path) {
Expand All @@ -192,7 +206,13 @@ protected function findTemplate($name)
}
}

throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])));
$this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]));

if (!$throw) {
return false;
}

throw new Twig_Error_Loader($this->errorCache[$name]);
}

protected function parseName($name, $default = self::MAIN_NAMESPACE)
Expand Down

0 comments on commit 21b128a

Please sign in to comment.