From: Fabien Potencier Date: Mon, 5 Aug 2013 16:00:03 +0000 (+0200) Subject: fixed two edge case bug in the filesystem loader X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f888818fd4c087fcc99731d0447c23b33e7cc60e;p=konrad%2Ftwig.git fixed two edge case bug in the filesystem loader * fixed caching when two templates have the same template name but a different namespace * fixed the error message when a template has a namespace --- diff --git a/lib/Twig/Loader/Filesystem.php b/lib/Twig/Loader/Filesystem.php index d47781a..5ce13dd 100644 --- a/lib/Twig/Loader/Filesystem.php +++ b/lib/Twig/Loader/Filesystem.php @@ -179,14 +179,14 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI $this->validateName($name); $namespace = self::MAIN_NAMESPACE; + $shortname = $name; if (isset($name[0]) && '@' == $name[0]) { if (false === $pos = strpos($name, '/')) { throw new Twig_Error_Loader(sprintf('Malformed namespaced template name "%s" (expecting "@namespace/template_name").', $name)); } $namespace = substr($name, 1, $pos - 1); - - $name = substr($name, $pos + 1); + $shortname = substr($name, $pos + 1); } if (!isset($this->paths[$namespace])) { @@ -194,8 +194,8 @@ class Twig_Loader_Filesystem implements Twig_LoaderInterface, Twig_ExistsLoaderI } foreach ($this->paths[$namespace] as $path) { - if (is_file($path.'/'.$name)) { - return $this->cache[$name] = $path.'/'.$name; + if (is_file($path.'/'.$shortname)) { + return $this->cache[$name] = $path.'/'.$shortname; } } diff --git a/test/Twig/Tests/Loader/FilesystemTest.php b/test/Twig/Tests/Loader/FilesystemTest.php index 4c874b6..3ca580b 100644 --- a/test/Twig/Tests/Loader/FilesystemTest.php +++ b/test/Twig/Tests/Loader/FilesystemTest.php @@ -94,4 +94,34 @@ 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')); + } }