Repository navigation
Expand file tree
/
Copy pathUndefinedCallableHandler.php
More file actions
131 lines (116 loc) · 4.31 KB
/
Copy pathUndefinedCallableHandler.php
File metadata and controls
131 lines (116 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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\Bridge\Twig;
use Composer\InstalledVersions;
use Symfony\Bundle\FullStack;
use Twig\Error\SyntaxError;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* @internal
*/
class UndefinedCallableHandler
{
private const FILTER_COMPONENTS = [
'emojify' => 'emoji',
'humanize' => 'form',
'form_encode_currency' => 'form',
'normalize' => 'serializer',
'serialize' => 'serializer',
'trans' => 'translation',
'sanitize_html' => 'html-sanitizer',
'yaml_encode' => 'yaml',
'yaml_dump' => 'yaml',
];
private const FUNCTION_COMPONENTS = [
'asset' => 'asset',
'asset_version' => 'asset',
'importmap' => 'asset-mapper',
'dump' => 'debug-bundle',
'encore_entry_link_tags' => 'webpack-encore-bundle',
'encore_entry_script_tags' => 'webpack-encore-bundle',
'expression' => 'expression-language',
'form_widget' => 'form',
'form_errors' => 'form',
'form_label' => 'form',
'form_help' => 'form',
'form_row' => 'form',
'form_rest' => 'form',
'form' => 'form',
'form_start' => 'form',
'form_end' => 'form',
'csrf_token' => 'form',
'form_parent' => 'form',
'field_name' => 'form',
'field_value' => 'form',
'field_label' => 'form',
'field_help' => 'form',
'field_errors' => 'form',
'field_choices' => 'form',
'logout_url' => 'security-http',
'logout_path' => 'security-http',
'is_granted' => 'security-core',
'is_granted_for_user' => 'security-core',
'impersonation_path' => 'security-http',
'impersonation_url' => 'security-http',
'impersonation_exit_path' => 'security-http',
'impersonation_exit_url' => 'security-http',
't' => 'translation',
'link' => 'web-link',
'preload' => 'web-link',
'dns_prefetch' => 'web-link',
'preconnect' => 'web-link',
'prefetch' => 'web-link',
'prerender' => 'web-link',
'workflow_can' => 'workflow',
'workflow_transitions' => 'workflow',
'workflow_transition' => 'workflow',
'workflow_has_marked_place' => 'workflow',
'workflow_marked_places' => 'workflow',
'workflow_metadata' => 'workflow',
'workflow_transition_blockers' => 'workflow',
];
private const FULL_STACK_ENABLE = [
'html-sanitizer' => 'enable "html_sanitizer"',
'form' => 'enable "framework.form"',
'security-core' => 'add the "SecurityBundle"',
'security-http' => 'add the "SecurityBundle"',
'web-link' => 'enable "framework.web_link"',
'workflow' => 'enable "workflow"',
];
public static function onUndefinedFilter(string $name): TwigFilter|false
{
if (!isset(self::FILTER_COMPONENTS[$name])) {
return false;
}
throw new SyntaxError(self::onUndefined($name, 'filter', self::FILTER_COMPONENTS[$name]));
}
public static function onUndefinedFunction(string $name): TwigFunction|false
{
if (!isset(self::FUNCTION_COMPONENTS[$name])) {
return false;
}
if ('webpack-encore-bundle' === self::FUNCTION_COMPONENTS[$name]) {
return new TwigFunction($name, static fn () => '');
}
throw new SyntaxError(self::onUndefined($name, 'function', self::FUNCTION_COMPONENTS[$name]));
}
private static function onUndefined(string $name, string $type, string $component): string
{
if (class_exists(FullStack::class) && isset(self::FULL_STACK_ENABLE[$component])) {
return \sprintf('Did you forget to %s? Unknown %s "%s".', self::FULL_STACK_ENABLE[$component], $type, $name);
}
$missingPackage = 'symfony/'.$component;
if (class_exists(InstalledVersions::class) && InstalledVersions::isInstalled($missingPackage)) {
$missingPackage = 'symfony/twig-bundle';
}
return \sprintf('Did you forget to run "composer require %s"? Unknown %s "%s".', $missingPackage, $type, $name);
}
}