Skip to content

Commit a814746

Browse files
committed
feature #1223 Twig extension: create with a runtime class (94noni)
This PR was squashed before being merged into the 1.0-dev branch. Discussion ---------- Twig extension: create with a runtime class This PR introduce a way to generate a twig runtime linked to the twig extension via the `make:twig-extension` command Fixes #1222 For now I did not touch the test, just want to gather early feedbacks :) Commits ------- dc640e2 Twig extension: create with a runtime class
2 parents e607f12 + dc640e2 commit a814746

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

src/Maker/MakeTwigExtension.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Console\Input\InputArgument;
2121
use Symfony\Component\Console\Input\InputInterface;
2222
use Twig\Extension\AbstractExtension;
23+
use Twig\Extension\RuntimeExtensionInterface;
2324
use Twig\TwigFilter;
2425
use Twig\TwigFunction;
2526

@@ -36,7 +37,7 @@ public static function getCommandName(): string
3637

3738
public static function getCommandDescription(): string
3839
{
39-
return 'Creates a new Twig extension class';
40+
return 'Creates a new Twig extension with its runtime class';
4041
}
4142

4243
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
@@ -49,22 +50,41 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
4950

5051
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
5152
{
53+
$name = $input->getArgument('name');
54+
5255
$extensionClassNameDetails = $generator->createClassNameDetails(
53-
$input->getArgument('name'),
56+
$name,
5457
'Twig\\Extension\\',
5558
'Extension'
5659
);
5760

61+
$runtimeClassNameDetails = $generator->createClassNameDetails(
62+
$name,
63+
'Twig\\Runtime\\',
64+
'Runtime'
65+
);
66+
5867
$useStatements = new UseStatementGenerator([
5968
AbstractExtension::class,
6069
TwigFilter::class,
6170
TwigFunction::class,
71+
$runtimeClassNameDetails->getFullName(),
72+
]);
73+
74+
$runtimeUseStatements = new UseStatementGenerator([
75+
RuntimeExtensionInterface::class,
6276
]);
6377

6478
$generator->generateClass(
6579
$extensionClassNameDetails->getFullName(),
6680
'twig/Extension.tpl.php',
67-
['use_statements' => $useStatements]
81+
['use_statements' => $useStatements, 'runtime_class_name' => $runtimeClassNameDetails->getShortName()]
82+
);
83+
84+
$generator->generateClass(
85+
$runtimeClassNameDetails->getFullName(),
86+
'twig/Runtime.tpl.php',
87+
['use_statements' => $runtimeUseStatements]
6888
);
6989

7090
$generator->writeChanges();

src/Resources/help/MakeTwigExtension.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The <info>%command.name%</info> command generates a new twig extension class.
1+
The <info>%command.name%</info> command generates a new twig extension with its runtime class.
22

33
<info>php %command.full_name% AppExtension</info>
44

src/Resources/skeleton/twig/Extension.tpl.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@ public function getFilters(): array
1212
// If your filter generates SAFE HTML, you should add a third
1313
// parameter: ['is_safe' => ['html']]
1414
// Reference: https://twig.symfony.com/doc/3.x/advanced.html#automatic-escaping
15-
new TwigFilter('filter_name', [$this, 'doSomething']),
15+
new TwigFilter('filter_name', [<?= $runtime_class_name ?>::class, 'doSomething']),
1616
];
1717
}
1818

1919
public function getFunctions(): array
2020
{
2121
return [
22-
new TwigFunction('function_name', [$this, 'doSomething']),
22+
new TwigFunction('function_name', [<?= $runtime_class_name ?>::class, 'doSomething']),
2323
];
2424
}
25-
26-
public function doSomething($value)
27-
{
28-
// ...
29-
}
3025
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?= "<?php\n" ?>
2+
3+
namespace <?= $namespace; ?>;
4+
5+
<?= $use_statements ?>
6+
7+
class <?= $class_name ?> implements RuntimeExtensionInterface
8+
{
9+
public function __construct()
10+
{
11+
// Inject dependencies if needed
12+
}
13+
14+
public function doSomething($value)
15+
{
16+
// ...
17+
}
18+
}

0 commit comments

Comments
 (0)