Skip to content

Commit

Permalink
feature #27535 [TwigBundle] Enhance the twig not found exception (beh…
Browse files Browse the repository at this point in the history
…noushnorouzi)

This PR was merged into the 4.2-dev branch.

Discussion
----------

[TwigBundle] Enhance the twig not found exception

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27468
| License       | MIT
| Doc PR        | -

Improve error message to make it clear to developers what mistake they have made.

Commits
-------

32988b4 Enhance the twig not found exception
  • Loading branch information
nicolas-grekas committed Jun 19, 2018
2 parents c694804 + 32988b4 commit 6c03064
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
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
@@ -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);
}
}
}
@@ -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 comments on commit 6c03064

Please sign in to comment.