Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bbe23a7

Browse files
committed
bug #25601 [TwigBundle/Brige] catch missing requirements to throw meaningful exceptions (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [TwigBundle/Brige] catch missing requirements to throw meaningful exceptions | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #25256, symfony/webpack-encore#173 | License | MIT | Doc PR | - It is possible to register a handler for undefined twig functions/filters. IMHO, this is the hook point we should leverage to throw meaningful exception messages. This works well on its own. We now just need to list the functions/filters with appropriate messages. There is one case we could enhance: at warmup time, Twig exceptions are swallowed, thus not visible. Shouldn't we make these visible instead? ping @weaverryan since this is related to two issues of yours. Commits ------- ac891ac [TwigBundle/Brige] catch missing requirements to throw meaningful exceptions
2 parents 0422471 + ac891ac commit bbe23a7

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig;
13+
14+
use Twig\Error\SyntaxError;
15+
16+
/**
17+
* @internal
18+
*/
19+
class UndefinedCallableHandler
20+
{
21+
private static $filterComponents = array(
22+
'humanize' => 'form',
23+
'trans' => 'translation',
24+
'transchoice' => 'translation',
25+
'yaml_encode' => 'yaml',
26+
'yaml_dump' => 'yaml',
27+
);
28+
29+
private static $functionComponents = array(
30+
'asset' => 'asset',
31+
'asset_version' => 'asset',
32+
'dump' => 'debug-bundle',
33+
'expression' => 'expression-language',
34+
'form_widget' => 'form',
35+
'form_errors' => 'form',
36+
'form_label' => 'form',
37+
'form_row' => 'form',
38+
'form_rest' => 'form',
39+
'form' => 'form',
40+
'form_start' => 'form',
41+
'form_end' => 'form',
42+
'csrf_token' => 'form',
43+
'logout_url' => 'security-http',
44+
'logout_path' => 'security-http',
45+
'is_granted' => 'security-core',
46+
'link' => 'web-link',
47+
'preload' => 'web-link',
48+
'dns_prefetch' => 'web-link',
49+
'preconnect' => 'web-link',
50+
'prefetch' => 'web-link',
51+
'prerender' => 'web-link',
52+
'workflow_can' => 'workflow',
53+
'workflow_transitions' => 'workflow',
54+
'workflow_has_marked_place' => 'workflow',
55+
'workflow_marked_places' => 'workflow',
56+
);
57+
58+
public static function onUndefinedFilter($name)
59+
{
60+
if (!isset(self::$filterComponents[$name])) {
61+
return false;
62+
}
63+
64+
// Twig will append the source context to the message, so that it will end up being like "[...] Unknown filter "%s" in foo.html.twig on line 123."
65+
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown filter "%s".', $name, self::$filterComponents[$name]));
66+
}
67+
68+
public static function onUndefinedFunction($name)
69+
{
70+
if (!isset(self::$functionComponents[$name])) {
71+
return false;
72+
}
73+
74+
throw new SyntaxError(sprintf('Did you forget to run "composer require symfony/%s"? Unknown function "%s".', $name, self::$functionComponents[$name]));
75+
}
76+
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configurator/EnvironmentConfigurator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Configurator;
1313

14+
use Symfony\Bridge\Twig\UndefinedCallableHandler;
1415
use Twig\Environment;
1516

1617
// BC/FC with namespaced Twig
@@ -49,5 +50,9 @@ public function configure(Environment $environment)
4950
}
5051

5152
$environment->getExtension('Twig\Extension\CoreExtension')->setNumberFormat($this->decimals, $this->decimalPoint, $this->thousandsSeparator);
53+
54+
// wrap UndefinedCallableHandler in closures for lazy-autoloading
55+
$environment->registerUndefinedFilterCallback(function ($name) { return UndefinedCallableHandler::onUndefinedFilter($name); });
56+
$environment->registerUndefinedFunctionCallback(function ($name) { return UndefinedCallableHandler::onUndefinedFunction($name); });
5257
}
5358
}

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^5.5.9|>=7.0.8",
2020
"symfony/config": "~3.2|~4.0",
21-
"symfony/twig-bridge": "^3.4|~4.0",
21+
"symfony/twig-bridge": "^3.4.3|~4.0",
2222
"symfony/http-foundation": "~2.8|~3.0|~4.0",
2323
"symfony/http-kernel": "^3.3|~4.0",
2424
"twig/twig": "~1.34|~2.4"

0 commit comments

Comments
 (0)