From d3fe07b1600f50c0b0f926317c3bb01cab21cdfb Mon Sep 17 00:00:00 2001 From: Roy Van Ginneken Date: Sun, 11 Oct 2015 14:15:29 +0200 Subject: [PATCH 1/7] [TwigBundle] fixed Include file locations in "Template could not be found" exception --- src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index 65afe2a33e..faf5ce3217 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -89,7 +89,10 @@ class FilesystemLoader extends \Twig_Loader_Filesystem } if (false === $file || null === $file) { - throw new \Twig_Error_Loader(sprintf('Unable to find template "%s".', $logicalName), -1, null, $previous); + list($namespace, $name) = $this->parseName($logicalName); + $paths = $this->getPaths($namespace); + array_walk($paths, function (&$path) use ($name) { $path .= '/' . $name; }); + throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (tried: %s).', $name, implode(', ', $paths)), -1, null, $previous); } return $this->cache[$logicalName] = $file; From e9d951a8821df4df3fcce48ec79806cca97dbae7 Mon Sep 17 00:00:00 2001 From: Roy Van Ginneken Date: Sun, 11 Oct 2015 14:34:15 +0200 Subject: [PATCH 2/7] [CodingStandards] Conformed to coding standards --- src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index faf5ce3217..73e38cbe86 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -91,7 +91,7 @@ class FilesystemLoader extends \Twig_Loader_Filesystem if (false === $file || null === $file) { list($namespace, $name) = $this->parseName($logicalName); $paths = $this->getPaths($namespace); - array_walk($paths, function (&$path) use ($name) { $path .= '/' . $name; }); + array_walk($paths, function (&$path) use ($name) { $path .= '/'.$name; }); throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (tried: %s).', $name, implode(', ', $paths)), -1, null, $previous); } From 968476b707c75caf517519a47d8385d4dc23ec05 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 19 Jan 2016 09:57:11 +0100 Subject: [PATCH 3/7] Improved the error message when a template is not found --- .../Bundle/TwigBundle/Loader/FilesystemLoader.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index 73e38cbe86..8b9f47b4de 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -89,10 +89,14 @@ class FilesystemLoader extends \Twig_Loader_Filesystem } if (false === $file || null === $file) { - list($namespace, $name) = $this->parseName($logicalName); - $paths = $this->getPaths($namespace); - array_walk($paths, function (&$path) use ($name) { $path .= '/'.$name; }); - throw new \Twig_Error_Loader(sprintf('Unable to find template "%s" (tried: %s).', $name, implode(', ', $paths)), -1, null, $previous); + try { + list($namespace, $name) = $this->parseName($logicalName); + $paths = sprintf(' (looked into: %s)', implode(', ', $this->getPaths($namespace)));; + } catch (\Twig_Error_Loader $e) { + $paths = ''; + } + + throw new \Twig_Error_Loader(sprintf('Unable to find template "%s"%s.', $name, $paths, -1, null, $previous)); } return $this->cache[$logicalName] = $file; From 35f082f6ef954b78af20e36f8b1bae7d32dda83d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 19 Jan 2016 10:10:19 +0100 Subject: [PATCH 4/7] Fixed a syntax issue --- src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index 8b9f47b4de..0bc4bc5583 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -91,7 +91,7 @@ class FilesystemLoader extends \Twig_Loader_Filesystem if (false === $file || null === $file) { try { list($namespace, $name) = $this->parseName($logicalName); - $paths = sprintf(' (looked into: %s)', implode(', ', $this->getPaths($namespace)));; + $paths = sprintf(' (looked into: %s)', implode(', ', $this->getPaths($namespace))); } catch (\Twig_Error_Loader $e) { $paths = ''; } From 88b913b0c465dd805d679a0642aba4fd17a20241 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 27 Jan 2016 17:49:58 +0100 Subject: [PATCH 5/7] Fixed the problem in an easier way --- .../Bundle/TwigBundle/Loader/FilesystemLoader.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index 0bc4bc5583..e3170aff94 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -78,6 +78,7 @@ class FilesystemLoader extends \Twig_Loader_Filesystem $file = parent::findTemplate($logicalName); } catch (\Twig_Error_Loader $e) { $previous = $e; + $errorMessage = $e->getMessage(); // for BC try { @@ -89,14 +90,7 @@ class FilesystemLoader extends \Twig_Loader_Filesystem } if (false === $file || null === $file) { - try { - list($namespace, $name) = $this->parseName($logicalName); - $paths = sprintf(' (looked into: %s)', implode(', ', $this->getPaths($namespace))); - } catch (\Twig_Error_Loader $e) { - $paths = ''; - } - - throw new \Twig_Error_Loader(sprintf('Unable to find template "%s"%s.', $name, $paths, -1, null, $previous)); + throw new \Twig_Error_Loader($errorMessage, -1, null, $previous); } return $this->cache[$logicalName] = $file; From 19bfa2ed8c4c216ed827670ed53e1aa408a3a00d Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 27 Jan 2016 18:13:21 +0100 Subject: [PATCH 6/7] Added a test --- .../Tests/Loader/FilesystemLoaderTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php index 3fd0888cda..cc993c7527 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/Loader/FilesystemLoaderTest.php @@ -98,4 +98,21 @@ class FilesystemLoaderTest extends TestCase $loader = new FilesystemLoader($locator, $parser); $loader->getCacheKey('name.format.engine'); } + + /** + * @expectedException \Twig_Error_Loader + * @expectedExceptionMessageRegExp /Unable to find template "name\.format\.engine" \(looked into: .*\/Tests\/Loader\/\.\.\/DependencyInjection\/Fixtures\/Resources\/views\)/ + */ + public function testTwigErrorIfTemplateDoesNotExist() + { + $parser = $this->getMock('Symfony\Component\Templating\TemplateNameParserInterface'); + $locator = $this->getMock('Symfony\Component\Config\FileLocatorInterface'); + + $loader = new FilesystemLoader($locator, $parser); + $loader->addPath(__DIR__.'/../DependencyInjection/Fixtures/Resources/views'); + + $method = new \ReflectionMethod('Symfony\Bundle\TwigBundle\Loader\FilesystemLoader', 'findTemplate'); + $method->setAccessible(true); + $method->invoke($loader, 'name.format.engine'); + } } From 0134d7678516f6c9ec56059e0596685c501724b4 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 1 Mar 2016 15:47:09 +0100 Subject: [PATCH 7/7] Simplified everything --- src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php index e3170aff94..2a21c30fba 100644 --- a/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php +++ b/src/Symfony/Bundle/TwigBundle/Loader/FilesystemLoader.php @@ -77,20 +77,18 @@ class FilesystemLoader extends \Twig_Loader_Filesystem try { $file = parent::findTemplate($logicalName); } catch (\Twig_Error_Loader $e) { - $previous = $e; - $errorMessage = $e->getMessage(); + $twigLoaderException = $e; // for BC try { $template = $this->parser->parse($template); $file = $this->locator->locate($template); } catch (\Exception $e) { - $previous = $e; } } if (false === $file || null === $file) { - throw new \Twig_Error_Loader($errorMessage, -1, null, $previous); + throw $twigLoaderException; } return $this->cache[$logicalName] = $file;