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

Skip to content

Commit 32988b4

Browse files
Enhance the twig not found exception
Enhance the twig not found exception
1 parent b560883 commit 32988b4

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
1313

1414
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
15+
use Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
1516
use Symfony\Component\Config\FileLocator;
1617
use Symfony\Component\Config\Resource\FileExistenceResource;
1718
use Symfony\Component\Console\Application;
@@ -92,6 +93,10 @@ public function load(array $configs, ContainerBuilder $container)
9293

9394
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.native_filesystem');
9495

96+
if ($container->getParameter('kernel.debug')) {
97+
$twigFilesystemLoaderDefinition->setClass(NativeFilesystemLoader::class);
98+
}
99+
95100
// register user-configured paths
96101
foreach ($config['paths'] as $path => $namespace) {
97102
if (!$namespace) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
/**
18+
* @author Behnoush Norouzali <[email protected]>
19+
*
20+
* @internal
21+
*/
22+
class NativeFilesystemLoader extends FilesystemLoader
23+
{
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function findTemplate($template, $throw = true)
28+
{
29+
try {
30+
return parent::findTemplate($template, $throw);
31+
} catch (LoaderError $e) {
32+
if ('' === $template || '@' === $template[0] || !preg_match('/^(?P<bundle>[^:]*?)(?:Bundle)?:(?P<path>[^:]*+):(?P<template>.+\.[^\.]+\.[^\.]+)$/', $template, $m)) {
33+
throw $e;
34+
}
35+
if ('' !== $m['path']) {
36+
$m['template'] = $m['path'].'/'.$m['template'];
37+
}
38+
if ('' !== $m['bundle']) {
39+
$suggestion = '@'.$m['bundle'].'/'.$m['template'];
40+
} else {
41+
$suggestion = $m['template'];
42+
}
43+
if (false === parent::findTemplate($suggestion, false)) {
44+
throw $e;
45+
}
46+
47+
throw new LoaderError(sprintf('Template reference "%s" not found, did you mean "%s"?', $template, $suggestion), -1, null, $e);
48+
}
49+
}
50+
}
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'.\DIRECTORY_SEPARATOR.'templates'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'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)