8000 Enhance the twig not found exception · symfony/symfony@82d1db4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82d1db4

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

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
8000 lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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 ('' === $template || '@' === $template[0] || !preg_match('/^([^:]*?)(?:Bundle)?:([^:]*+):(.+\.[^\.]+\.[^\.]+)$/', $template, $m)) {
28+
throw $e;
29+
}
30+
if ('' !== $m[2]) {
31+
$m[3] = $m[2].'/'.$m[3];
32+
}
33+
if ('' !== $m[1]) {
34+
$suggestion = '@'.$m[1].'/'.$m[3];
35+
} else {
36+
$suggestion = $m[3];
37+
}
38+
if (false === parent::findTemplate($suggestion, false)) {
39+
throw $e;
40+
}
41+
42+
throw new LoaderError(sprintf('Template reference "%s" not found, did you mean "%s"?', $template, $suggestion), -1, null, $e);
43+
}
44+
}
45+
}

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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
const DS = DIRECTORY_SEPARATOR;
11+
12+
public function testWithNativeNamespace()
13+
{
14+
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
15+
$loader->addPath('Fixtures/templates', 'Test');
16+
17+
$this->assertSame('Fixtures'.self::DS.'templates'.self::DS.'Foo'.self::DS.'index.html.twig', $loader->getCacheKey('@Test/Foo/index.html.twig'));
18+
}
19+
20+
/**
21+
* @expectedException \Twig\Error\LoaderError
22+
* @expectedExceptionMessage Template reference "TestBundle::Foo/index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
23+
*/
24+
public function testWithLegacyStyle1()
25+
{
26+
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
27+
$loader->addPath('Fixtures/templates', 'Test');
28+
29+
$loader->getCacheKey('TestBundle::Foo/index.html.twig');
30+
}
31+
32+
/**
33+
* @expectedException \Twig\Error\LoaderError
34+
* @expectedExceptionMessage Template reference "TestBundle:Foo:index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
35+
*/
36+
public function testWithLegacyStyle2()
37+
{
38+
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
39+
$loader->addPath('Fixtures/templates', 'Test');
40+
41+
$loader->getCacheKey('TestBundle:Foo:index.html.twig');
42+
}
43+
}

0 commit comments

Comments
 (0)
0