Enhance the twig not found exception

Enhance the twig not found exception
This commit is contained in:
benoushnorouzi 2018-06-07 11:24:03 +02:00
parent b560883ffe
commit 32988b4294
3 changed files with 96 additions and 0 deletions

View File

@ -12,6 +12,7 @@
namespace Symfony\Bundle\TwigBundle\DependencyInjection;
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
use Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileExistenceResource;
use Symfony\Component\Console\Application;
@ -92,6 +93,10 @@ class TwigExtension extends Extension
$twigFilesystemLoaderDefinition = $container->getDefinition('twig.loader.native_filesystem');
if ($container->getParameter('kernel.debug')) {
$twigFilesystemLoaderDefinition->setClass(NativeFilesystemLoader::class);
}
// register user-configured paths
foreach ($config['paths'] as $path => $namespace) {
if (!$namespace) {

View File

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\Loader;
use Twig\Error\LoaderError;
use Twig\Loader\FilesystemLoader;
/**
* @author Behnoush Norouzali <behnoush.norouzi@gmail.com>
*
* @internal
*/
class NativeFilesystemLoader extends FilesystemLoader
{
/**
* {@inheritdoc}
*/
protected function findTemplate($template, $throw = true)
{
try {
return parent::findTemplate($template, $throw);
} catch (LoaderError $e) {
if ('' === $template || '@' === $template[0] || !preg_match('/^(?P<bundle>[^:]*?)(?:Bundle)?:(?P<path>[^:]*+):(?P<template>.+\.[^\.]+\.[^\.]+)$/', $template, $m)) {
throw $e;
}
if ('' !== $m['path']) {
$m['template'] = $m['path'].'/'.$m['template'];
}
if ('' !== $m['bundle']) {
$suggestion = '@'.$m['bundle'].'/'.$m['template'];
} else {
$suggestion = $m['template'];
}
if (false === parent::findTemplate($suggestion, false)) {
throw $e;
}
throw new LoaderError(sprintf('Template reference "%s" not found, did you mean "%s"?', $template, $suggestion), -1, null, $e);
}
}
}

View File

@ -0,0 +1,41 @@
<?php
namespace Symfony\Bundle\TwigBundle\Tests\Loader;
use Symfony\Bundle\TwigBundle\Loader\NativeFilesystemLoader;
use Symfony\Bundle\TwigBundle\Tests\TestCase;
class NativeFilesystemLoaderTest extends TestCase
{
public function testWithNativeNamespace()
{
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
$loader->addPath('Fixtures/templates', 'Test');
$this->assertSame('Fixtures'.\DIRECTORY_SEPARATOR.'templates'.\DIRECTORY_SEPARATOR.'Foo'.\DIRECTORY_SEPARATOR.'index.html.twig', $loader->getCacheKey('@Test/Foo/index.html.twig'));
}
/**
* @expectedException \Twig\Error\LoaderError
* @expectedExceptionMessage Template reference "TestBundle::Foo/index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
*/
public function testWithLegacyStyle1()
{
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
$loader->addPath('Fixtures/templates', 'Test');
$loader->getCacheKey('TestBundle::Foo/index.html.twig');
}
/**
* @expectedException \Twig\Error\LoaderError
* @expectedExceptionMessage Template reference "TestBundle:Foo:index.html.twig" not found, did you mean "@Test/Foo/index.html.twig"?
*/
public function testWithLegacyStyle2()
{
$loader = new NativeFilesystemLoader(null, __DIR__.'/../');
$loader->addPath('Fixtures/templates', 'Test');
$loader->getCacheKey('TestBundle:Foo:index.html.twig');
}
}