removed BC break in filesystem loader

This commit is contained in:
Fabien Potencier
2015-08-22 14:42:57 +02:00
parent 638eedb622
commit 3da99ce98b
+36 -26
View File
@@ -144,7 +144,17 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
*/ */
public function exists($name) public function exists($name)
{ {
return $this->doFindTemplate($this->normalizeName($name)); $name = $this->normalizeName($name);
if (isset($this->cache[$name])) {
return true;
}
try {
return false !== $this->findTemplate($name, false);
} catch (Twig_Error_Loader $exception) {
return false;
}
} }
/** /**
@@ -155,63 +165,63 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface
return filemtime($this->findTemplate($name)) <= $time; return filemtime($this->findTemplate($name)) <= $time;
} }
protected function findTemplate($name) /**
* Checks if the template can be found.
*
* @param string $name The template name
* @param Boolean $throw Whether to throw an exception when an error occurs
*
* @return string|false The template name or false
*/
protected function findTemplate($name, $throw = true)
{ {
$name = $this->normalizeName($name); $name = $this->normalizeName($name);
if ($this->doFindTemplate($name)) { if (isset($this->cache[$name])) {
return $this->cache[$name]; return $this->cache[$name];
} }
if (isset($this->errorCache[$name])) {
if (!$throw) {
return false;
}
throw new Twig_Error_Loader($this->errorCache[$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); $this->validateName($name);
if (isset($this->cache[$name])) {
return true;
}
if (isset($this->errorCache[$name])) {
return false;
}
list($namespace, $shortname) = $this->parseName($name); list($namespace, $shortname) = $this->parseName($name);
if (!isset($this->paths[$namespace])) { if (!isset($this->paths[$namespace])) {
$this->errorCache[$name] = 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; return false;
} }
throw new Twig_Error_Loader($this->errorCache[$name]);
}
foreach ($this->paths[$namespace] as $path) { foreach ($this->paths[$namespace] as $path) {
if (is_file($path.'/'.$shortname)) { if (is_file($path.'/'.$shortname)) {
if (false !== $realpath = realpath($path.'/'.$shortname)) { if (false !== $realpath = realpath($path.'/'.$shortname)) {
$this->cache[$name] = $realpath; return $this->cache[$name] = $realpath;
return true;
} }
$this->cache[$name] = $path.'/'.$shortname; return $this->cache[$name] = $path.'/'.$shortname;
return true;
} }
} }
$this->errorCache[$name] = 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; return false;
} }
throw new Twig_Error_Loader($this->errorCache[$name]);
}
protected function normalizeName($name) protected function normalizeName($name)
{ {
return preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); return preg_replace('#/{2,}#', '/', strtr($name, '\\', '/'));