Skip to content

Commit 1897374

Browse files
authored
Add support for Twig function (#2)
* Adding support for twig functions * CS
1 parent 6a29b18 commit 1897374

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"symfony/flex": "^1.3.1",
1515
"symfony/framework-bundle": "^5.2",
1616
"symfony/process": "^5.2",
17-
"symfony/yaml": "^5.2"
17+
"symfony/yaml": "^5.2",
18+
"twig/twig": "^3.3"
1819
},
1920
"replace": {
2021
"symfony/polyfill-ctype": "*",

src/Listener/ValidCodeNodeListener.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\Process\Process;
1111
use Symfony\Component\Yaml\Exception\ParseException;
1212
use Symfony\Component\Yaml\Yaml;
13+
use SymfonyCodeBlockChecker\Twig\DummyExtension;
1314
use Twig\Environment;
1415
use Twig\Error\SyntaxError;
1516
use Twig\Loader\ArrayLoader;
@@ -125,12 +126,15 @@ private function validateYaml(CodeNode $node)
125126

126127
private function validateTwig(CodeNode $node)
127128
{
128-
$twig = $this->twig ?? new Environment(new ArrayLoader());
129+
if (null === $this->twig) {
130+
$this->twig = new Environment(new ArrayLoader());
131+
$this->twig->addExtension(new DummyExtension());
132+
}
129133

130134
try {
131-
$tokens = $twig->tokenize(new Source($node->getValue(), $node->getEnvironment()->getCurrentFileName()));
135+
$tokens = $this->twig->tokenize(new Source($node->getValue(), $node->getEnvironment()->getCurrentFileName()));
132136
// We cannot parse the TokenStream because we dont have all extensions loaded.
133-
// $twig->parse($tokens);
137+
$this->twig->parse($tokens);
134138
} catch (SyntaxError $e) {
135139
$this->errorManager->error(sprintf(
136140
'Invalid Twig syntax: %s',

src/Twig/DummyExtension.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace SymfonyCodeBlockChecker\Twig;
4+
5+
use Twig\Extension\AbstractExtension;
6+
use Twig\TwigFilter;
7+
use Twig\TwigFunction;
8+
9+
/**
10+
* This extension will contain filters and functions that exists in Symfony. This
11+
* will help the parser to verify the correctness.
12+
*
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
class DummyExtension extends AbstractExtension
16+
{
17+
public function getFunctions()
18+
{
19+
return [
20+
new TwigFunction('asset'),
21+
new TwigFunction('asset_version'),
22+
new TwigFunction('csrf_token'),
23+
new TwigFunction('dump'),
24+
new TwigFunction('expression'),
25+
new TwigFunction('form_widget'),
26+
new TwigFunction('form_errors'),
27+
new TwigFunction('form_label'),
28+
new TwigFunction('form_help'),
29+
new TwigFunction('form_row'),
30+
new TwigFunction('form_rest'),
31+
new TwigFunction('form'),
32+
new TwigFunction('form_start'),
33+
new TwigFunction('form_end'),
34+
new TwigFunction('csrf_token'),
35+
new TwigFunction('form_parent'),
36+
new TwigFunction('absolute_url'),
37+
new TwigFunction('relative_path'),
38+
new TwigFunction('render'),
39+
new TwigFunction('render_*'),
40+
new TwigFunction('controller'),
41+
new TwigFunction('logout_url'),
42+
new TwigFunction('logout_path'),
43+
new TwigFunction('url'),
44+
new TwigFunction('path'),
45+
new TwigFunction('is_granted'),
46+
new TwigFunction('link'),
47+
new TwigFunction('preload'),
48+
new TwigFunction('dns_prefetch'),
49+
new TwigFunction('preconnect'),
50+
new TwigFunction('prefetch'),
51+
new TwigFunction('prerender'),
52+
new TwigFunction('workflow_can'),
53+
new TwigFunction('workflow_transitions'),
54+
new TwigFunction('workflow_has_marked_place'),
55+
new TwigFunction('workflow_marked_places'),
56+
new TwigFunction('workflow_metadata'),
57+
new TwigFunction('workflow_transition_blockers'),
58+
];
59+
}
60+
61+
public function getFilters()
62+
{
63+
return [
64+
new TwigFilter('abbr_class'),
65+
new TwigFilter('abbr_method'),
66+
new TwigFilter('format_args'),
67+
new TwigFilter('format_args_as_text'),
68+
new TwigFilter('file_excerpt'),
69+
new TwigFilter('format_file'),
70+
new TwigFilter('format_file_from_text'),
71+
new TwigFilter('format_log_message'),
72+
new TwigFilter('file_link'),
73+
new TwigFilter('file_relative'),
74+
new TwigFilter('humanize'),
75+
new TwigFilter('form_encode_currency'),
76+
new TwigFilter('yaml_encode'),
77+
new TwigFilter('yaml_dump'),
78+
new TwigFilter('trans'),
79+
new TwigFilter('transchoice'),
80+
];
81+
}
82+
}

tests/Listener/ValidCodeNodeListenerTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,15 @@ public function testInvalidYaml()
3939

4040
$this->assertStringContainsString('Invalid Yaml', $errors[0]);
4141
}
42+
43+
public function testParseTwig()
44+
{
45+
$node = new CodeNode(['{{ form(form) }}']);
46+
$node->setEnvironment($this->environment);
47+
$node->setLanguage('twig');
48+
$this->listener->postNodeCreate(new PostNodeCreateEvent($node));
49+
50+
$errors = $this->errorManager->getErrors();
51+
$this->assertCount(0, $errors);
52+
}
4253
}

0 commit comments

Comments
 (0)