-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #27535 [TwigBundle] Enhance the twig not found exception (beh…
…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
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Symfony/Bundle/TwigBundle/Loader/NativeFilesystemLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/Symfony/Bundle/TwigBundle/Tests/Loader/NativeFilesystemLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |