Skip to content

Commit

Permalink
improved the performance of the filesystem loader
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Mar 2, 2015
1 parent d821ec2 commit 79f52aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 2.0.0 (201X-XX-XX)

* improved the performance of the filesystem loader
* removed features that were deprecated in 1.x

* 1.18.1 (2015-XX-XX)
Expand Down
57 changes: 37 additions & 20 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 @@ -143,19 +144,7 @@ public function getCacheKey($name)
*/
public function exists($name)
{
$name = $this->normalizeName($name);

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

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

return true;
} catch (Twig_Error_Loader $exception) {
return false;
}
return $this->doFindTemplate($this->normalizeName($name));
}

/**
Expand All @@ -170,29 +159,57 @@ protected function findTemplate($name)
{
$name = $this->normalizeName($name);

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

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

/**
* Checks if the template can be found.
*
* @param string $name The template name
*
* @return bool true if the template exists, false otherwise
*/
protected function doFindTemplate($name)
{
$this->validateName($name);

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

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

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);

return false;
}

foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$shortname)) {
if (false !== $realpath = realpath($path.'/'.$shortname)) {
return $this->cache[$name] = $realpath;
$this->cache[$name] = $realpath;

return true;
}

return $this->cache[$name] = $path.'/'.$shortname;
$this->cache[$name] = $path.'/'.$shortname;

return true;
}
}

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]));

return false;
}

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

0 comments on commit 79f52aa

Please sign in to comment.