From: Fabien Potencier Date: Mon, 5 Aug 2013 08:14:58 +0000 (+0200) Subject: reverted optimizations done in the filesystem loader as it beaks BC X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=7c1ce36f87ab1dd1dea007749325b8e120208be6;p=konrad%2Ftwig.git reverted optimizations done in the filesystem loader as it beaks BC --- diff --git a/CHANGELOG b/CHANGELOG index 956dcb1..9dbd495 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,9 +1,7 @@ * 1.14.0 (2013-XX-XX) * fixed template_from_string when the template includes or extends other ones - * improved the performance of the filesystem loader * fixed a crash of the C extension on an edge case - * fixed the filesystem loader cache when a template name exists in several namespaces * added support for calling macros defined in a template without importing them first * added support for named arguments for macros * replaced a PHP fatal error with an exception when a macro does not exist diff --git a/lib/Twig/Loader/Filesystem.php b/lib/Twig/Loader/Filesystem.php index ffa40a3..d47781a 100644 --- a/lib/Twig/Loader/Filesystem.php +++ b/lib/Twig/Loader/Filesystem.php @@ -21,7 +21,6 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI protected $paths; protected $cache; - protected $errorCache; /** * Constructor. @@ -88,7 +87,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI public function addPath($path, $namespace = self::MAIN_NAMESPACE) { // invalidate the cache - $this->cache = $this->errorCache = array(); + $this->cache = array(); if (!is_dir($path)) { throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); @@ -108,7 +107,7 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI public function prependPath($path, $namespace = self::MAIN_NAMESPACE) { // invalidate the cache - $this->cache = $this->errorCache = array(); + $this->cache = array(); if (!is_dir($path)) { throw new Twig_Error_Loader(sprintf('The "%s" directory does not exist.', $path)); @@ -144,7 +143,18 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI */ public function exists($name) { - return $this->doFindTemplate($this->normalizeName($name)); + $name = (string) $name; + if (isset($this->cache[$name])) { + return true; + } + + try { + $this->findTemplate($name); + + return true; + } catch (Twig_Error_Loader $exception) { + return false; + } } /** @@ -157,62 +167,39 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI protected function findTemplate($name) { - $name = $this->normalizeName($name); + $name = (string) $name; - if ($this->doFindTemplate($name)) { - return $this->cache[$name]; - } - - throw new Twig_Error_Loader($this->errorCache[$name]); - } - - protected function doFindTemplate($name) - { - $this->validateName($name); + // normalize name + $name = preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')); if (isset($this->cache[$name])) { - return true; + return $this->cache[$name]; } - if (isset($this->errorCache[$name])) { - return false; - } + $this->validateName($name); $namespace = self::MAIN_NAMESPACE; - $shortname = $name; if (isset($name[0]) && '@' == $name[0]) { if (false === $pos = strpos($name, '/')) { - $this->errorCache[$name] = sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name); - - return false; + throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name)); } $namespace = substr($name, 1, $pos - 1); - $shortname = substr($name, $pos + 1); + + $name = substr($name, $pos + 1); } if (!isset($this->paths[$namespace])) { - $this->errorCache[$name] = sprintf('There are no registered paths for namespace "%s".', $namespace); - - return false; + throw new Twig_Error_Loader(sprintf('There are no registered paths for namespace "%s".', $namespace)); } foreach ($this->paths[$namespace] as $path) { - if (is_file($path.'/'.$shortname)) { - $this->cache[$name] = $path.'/'.$shortname; - - return true; + if (is_file($path.'/'.$name)) { + return $this->cache[$name] = $path.'/'.$name; } } - $this->errorCache[$name] = sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace])); - - return false; - } - - protected function normalizeName($name) - { - return preg_replace('#/{2,}#', '/', strtr((string) $name, '\\', '/')); + throw new Twig_Error_Loader(sprintf('Unable to find template "%s" (looked into: %s).', $name, implode(', ', $this->paths[$namespace]))); } protected function validateName($name) diff --git a/test/Twig/Tests/Loader/FilesystemTest.php b/test/Twig/Tests/Loader/FilesystemTest.php index 3ca580b..4c874b6 100644 --- a/test/Twig/Tests/Loader/FilesystemTest.php +++ b/test/Twig/Tests/Loader/FilesystemTest.php @@ -94,34 +94,4 @@ class Twig_Tests_Loader_FilesystemTest extends PHPUnit_Framework_TestCase $loader->addPath(sys_get_temp_dir(), 'named'); $this->assertEquals(array(Twig_Loader_Filesystem::MAIN_NAMESPACE, 'named'), $loader->getNamespaces()); } - - public function testFindTemplateExceptionNamespace() - { - $basePath = dirname(__FILE__).'/Fixtures'; - - $loader = new Twig_Loader_Filesystem(array($basePath.'/normal')); - $loader->addPath($basePath.'/named', 'named'); - - try { - $loader->getSource('@named/nowhere.html'); - } catch (Exception $e) { - $this->assertInstanceof('Twig_Error_Loader', $e); - $this->assertContains('Unable to find template "@named/nowhere.html"', $e->getMessage()); - } - } - - public function testFindTemplateWithCache() - { - $basePath = dirname(__FILE__).'/Fixtures'; - - $loader = new Twig_Loader_Filesystem(array($basePath.'/normal')); - $loader->addPath($basePath.'/named', 'named'); - - // prime the cache for index.html in the named namespace - $namedSource = $loader->getSource('@named/index.html'); - $this->assertEquals("named path\n", $namedSource); - - // get index.html from the main namespace - $this->assertEquals("path\n", $loader->getSource('index.html')); - } }