Skip to content

Commit

Permalink
Enhance the twig not found exception
Browse files Browse the repository at this point in the history
Enhance the twig not found exception
  • Loading branch information
behnoushnorouzi committed Jun 14, 2018
1 parent b560883 commit 32988b4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
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 comments on commit 32988b4

Please sign in to comment.