Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit de2c952

Browse files
Enhance the twig not found exception
1 parent 50c4384 commit de2c952

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\Loader;
13+
14+
use Twig\Error\LoaderError;
15+
use Twig\Loader\FilesystemLoader;
16+
17+
class NativeFilesystemLoader extends FilesystemLoader
18+
{
19+
/**
20+
* {@inheritdoc}
21+
*/
22+
protected function findTemplate($template, $throw = true)
23+
{
24+
try {
25+
return parent::findTemplate($template, $throw);
26+
} catch (LoaderError $e) {
27+
if ($e instanceof \Twig_Error_Loader && preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m) && 0 !== strpos($template, '@')) {
28+
if ('' !== $m[2]) {
29+
$m[3] = $m[2].'/'.$m[3];
30+
}
31+
if ('' !== $m[1]) {
32+
$suggestion = '@'.$m[1].'/'.$m[3];
33+
} else {
34+
$suggestion = $m[3];
35+
}
36+
if (false === parent::findTemplate($suggestion, false)) {
37+
throw $e;
38+
}
39+
40+
throw new LoaderError(sprintf('Template reference "%s" not found, did you mean "%s"?', $template, $suggestion), -1, null, $e);
41+
}
42+
43+
throw $e;
44+
}
45+
}
46+
}

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
<argument type="service" id="twig.template_iterator" />
5151
</service>
5252

53-
<service id="twig.loader.native_filesystem" class="Twig\Loader\FilesystemLoader">
53+
<service id="twig.loader.native_filesystem" class="Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader">
5454
<argument type="collection" /> <!-- paths -->
5555
<argument>%kernel.project_dir%</argument>
5656
<tag name="twig.loader"/>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\TwigBundle\Tests\Loader;
4+
5+
use Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
6+
use Symfony\Bundle\TwigBundle\Tests\TestCase;
7+
8+
class NativeFilesystemLoaderTest extends TestCase
9+
{
10+
public function testWithNativeNamespace()
11+
{
12+
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
13+
$loader->addPath('Fixtures/templates', 'Test');
14+
15+
$this->assertSame('Fixtures/templates/Foo/index.html.twig', $loader->getCacheKey('@Test/Foo/index.html.twig'));
16+
}
17+
18+
/**
19+
* @expectedException \Twig\Error\LoaderError
20+
* @expectedExceptionMessage Template reference "TestBundle::Foo/index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
21+
*/
22+
public function testWithLegacyStyle1()
23+
{
24+
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
25+
$loader->addPath('Fixtures/templates', 'Test');
26+
27+
$loader->getCacheKey('TestBundle::Foo/index.html.twig');
28+
}
29+
30+
/**
31+
* @expectedException \Twig\Error\LoaderError
32+
* @expectedExceptionMessage Template reference "TestBundle:Foo:index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
33+
*/
34+
public function testWithLegacyStyle2()
35+
{
36+
$loader = new NativeFilesystemLoader(null, __DIR__ . '/../');
37+
$loader->addPath('Fixtures/templates', 'Test');
38+
39+
$loader->getCacheKey('TestBundle:Foo:index.html.twig');
40+
}
41+
}

0 commit comments

Comments
 (0)