8000 [TwigBundle] Enhance the twig not found exception by behnoushnorouzi · Pull Request #27535 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Enhance the twig not found exception #27535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,6 +93,10 @@ public function load(array $configs, ContainerBuilder $container)

$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) {
Expand Down
50 changes: 50 additions & 0 deletions src/Symfony/Bundle/TwigBundle/Loader/NativeFilesystemLoader.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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');
}
}
0