10000 feature #23519 [TwigBundle] Commands as a service (ro0NL) · symfony/symfony@68d9df6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 68d9df6

Browse files
committed
feature #23519 [TwigBundle] Commands as a service (ro0NL)
This PR was squashed before being merged into the 3.4 branch (closes #23519). Discussion ---------- [TwigBundle] Commands as a service | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | Fixed tickets | #... <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | symfony/symfony-docs#... <!--highly recommended for new features--> tiny step towards #23488 Commits ------- 9839140 [TwigBundle] Commands as a service
2 parents 04f3e60 + 9839140 commit 68d9df6

File tree

11 files changed

+156
-34
lines changed

11 files changed

+156
-34
lines changed

UPGRADE-3.4.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,21 @@ TwigBridge
8383
* deprecated the `Symfony\Bridge\Twig\Form\TwigRenderer` class, use the `FormRenderer`
8484
class from the Form component instead
8585

86+
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
87+
to pass a command name as first argument
88+
89+
* deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
90+
to pass a command name as first argument
91+
92+
TwigBundle
93+
----------
94+
95+
* deprecated the `Symfony\Bundle\TwigBundle\Command\DebugCommand` class, use the `DebugCommand`
96+
class from the Twig bridge instead
97+
98+
* deprecated relying on the `ContainerAwareInterface` implementation for
99+
`Symfony\Bundle\TwigBundle\Command\LintCommand`
100+
86101
Validator
87102
---------
88103

UPGRADE-4.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,10 @@ TwigBundle
511511
* The `ContainerAwareRuntimeLoader` class has been removed. Use the
512512
Twig `Twig_ContainerRuntimeLoader` class instead.
513513

514+
* Removed `DebugCommand` in favor of `Symfony\Bridge\Twig\Command\DebugCommand`.
515+
516+
* Removed `ContainerAwareInterface` implementation in `Symfony\Bundle\TwigBundle\Command\LintCommand`.
517+
514518
TwigBridge
515519
----------
516520

@@ -550,6 +554,12 @@ TwigBridge
550554

551555
* The `TwigRendererEngine::setEnvironment()` method has been removed.
552556
Pass the Twig Environment as second argument of the constructor instead.
557+
558+
* Removed `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability
559+
to pass a command name as first argument.
560+
561+
* Removed `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability
562+
to pass a command name as first argument.
553563

554564
Validator
555565
---------

src/Symfony/Bridge/Twig/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ CHANGELOG
55
-----
66

77
* deprecated `Symfony\Bridge\Twig\Form\TwigRenderer`
8+
* deprecated `Symfony\Bridge\Twig\Command\DebugCommand::set/getTwigEnvironment` and the ability to pass a command name as first argument
9+
* deprecated `Symfony\Bridge\Twig\Command\LintCommand::set/getTwigEnvironment` and the ability to pass a command name as first argument
810

911
3.3.0
1012
-----

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,27 @@ class DebugCommand extends Command
2929
private $twig;
3030

3131
/**
32-
* {@inheritdoc}
32+
* @param Environment $twig
3333
*/
34-
public function __construct($name = 'debug:twig')
34+
public function __construct($twig = null)
3535
{
36-
parent::__construct($name);
36+
parent::__construct();
37+
38+
if (!$twig instanceof Environment) {
39+
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
40+
41+
$this->setName(null === $twig ? 'debug:twig' : $twig);
42+
43+
return;
44+
}
45+
46+
$this->twig = $twig;
3747
}
3848

3949
public function setTwigEnvironment(Environment $twig)
4050
{
51+
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
52+
4153
$this->twig = $twig;
4254
}
4355

@@ -46,12 +58,15 @@ public function setTwigEnvironment(Environment $twig)
4658
*/
4759
protected function getTwigEnvironment()
4860
{
61+
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
62+
4963
return $this->twig;
5064
}
5165

5266
protected function configure()
5367
{
5468
$this
69+
->setName('debug:twig')
5570
->setDefinition(array(
5671
new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'),
5772
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'),
@@ -80,9 +95,17 @@ protected function configure()
8095
protected function execute(InputInterface $input, OutputInterface $output)
8196
{
8297
$io = new SymfonyStyle($input, $output);
83-
$twig = $this->getTwigEnvironment();
8498

85-
if (null === $twig) {
99+
// BC to be removed in 4.0
100+
if (__CLASS__ !== get_class($this)) {
101+
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
102+
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
103+
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
104+
105+
$this->twig = $this->getTwigEnvironment();
106+
}
107+
}
108+
if (null === $this->twig) {
86109
throw new \RuntimeException('The Twig environment needs to be set.');
87110
}
88111

@@ -91,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
91114
if ($input->getOption('format') === 'json') {
92115
$data = array();
93116
foreach ($types as $type) {
94-
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
117+
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
95118
$data[$type][$name] = $this->getMetadata($type, $entity);
96119
}
97120
}
@@ -105,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105128

106129
foreach ($types as $index => $type) {
107130
$items = array();
108-
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) {
131+
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) {
109132
if (!$filter || false !== strpos($name, $filter)) {
110133
$items[$name] = $name.$this->getPrettyMetadata($type, $entity);
111134
}

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,27 @@ class LintCommand extends Command
3434
private $twig;
3535

3636
/**
37-
* {@inheritdoc}
37+
* @param Environment $twig
3838
*/
39-
public function __construct($name = 'lint:twig')
39+
public function __construct($twig = null)
4040
{
41-
parent::__construct($name);
41+
parent::__construct();
42+
43+
if (!$twig instanceof Environment) {
44+
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED);
45+
46+
$this->setName(null === $twig ? 'lint:twig' : $twig);
47+
48+
return;
49+
}
50+
51+
$this->twig = $twig;
4252
}
4353

4454
public function setTwigEnvironment(Environment $twig)
4555
{
56+
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
57+
4658
$this->tw 10000 ig = $twig;
4759
}
4860

@@ -51,12 +63,15 @@ public function setTwigEnvironment(Environment $twig)
5163
*/
5264
protected function getTwigEnvironment()
5365
{
66+
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED);
67+
5468
return $this->twig;
5569
}
5670

5771
protected function configure()
5872
{
5973
$this
74+
->setName('lint:twig')
6075
->setDescription('Lints a template and outputs encountered errors')
6176
->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
6277
->addArgument('filename', InputArgument::IS_ARRAY)
@@ -86,7 +101,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
86101
{
87102
$io = new SymfonyStyle($input, $output);
88103

89-
if (null === $twig = $this->getTwigEnvironment()) {
104+
// BC to be removed in 4.0
105+
if (__CLASS__ !== get_class($this)) {
106+
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
107+
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
108+
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
109+
110+
$this->twig = $this->getTwigEnvironment();
111+
}
112+
}
113+
if (null === $this->twig) {
90114
throw new \RuntimeException('The Twig environment needs to be set.');
91115
}
92116

@@ -102,20 +126,20 @@ protected function execute(InputInterface $input, OutputInterface $output)
102126
$template .= fread(STDIN, 1024);
103127
}
104128

105-
return $this->display($input, $output, $io, array($this->validate($twig, $template, uniqid('sf_', true))));
129+
return $this->display($input, $output, $io, array($this->validate($template, uniqid('sf_', true))));
106130
}
107131

108-
$filesInfo = $this->getFilesInfo($twig, $filenames);
132+
$filesInfo = $this->getFilesInfo($filenames);
109133

110134
return $this->display($input, $output, $io, $filesInfo);
111135
}
112136

113-
private function getFilesInfo(Environment $twig, array $filenames)
137+
private function getFilesInfo(array $filenames)
114138
{
115139
$filesInfo = array();
116140
foreach ($filenames as $filename) {
117141
foreach ($this->findFiles($filename) as $file) {
118-
$filesInfo[] = $this->validate($twig, file_get_contents($file), $file);
142+
$filesInfo[] = $this->validate(file_get_contents($file), $file);
119143
}
120144
}
121145

@@ -133,17 +157,17 @@ protected function findFiles($filename)
133157
throw new \RuntimeException(sprintf('File or directory "%s" is not readable', $filename));
134158
}
135159

136-
private function validate(Environment $twig, $template, $file)
160+
private function validate($template, $file)
137161
{
138-
$realLoader = $twig->getLoader();
162+
$realLoader = $this->twig->getLoader();
139163
try {
140164
$temporaryLoader = new ArrayLoader(array((string) $file => $template));
141-
$twig->setLoader($temporaryLoader);
142-
$nodeTree = $twig->parse($twig->tokenize(new Source($template, (string) $file)));
143-
$twig->compile($nodeTree);
144-
$twig->setLoader($realLoader);
165+
$this->twig->setLoader($temporaryLoader);
166+
$nodeTree = $this->twig->parse($this->twig->tokenize(new Source($template, (string) $file)));
167+
$this->twig->compile($nodeTree);
168+
$this->twig->setLoader($realLoader);
145169
} catch (Error $e) {
146-
$twig->setLoader($realLoader);
170+
$this->twig->setLoader($realLoader);
147171

148172
return array('template' => $template, 'file' => $file, 'line' => $e->getTemplateLine(), 'valid' => false, 'exception' => $e);
149173
}

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,30 @@ public function testLintFileCompileTimeException()
6868
$this->assertRegExp('/ERROR in \S+ \(line /', trim($tester->getDisplay()));
6969
}
7070

71+
/**
72+
* @group legacy
73+
* @expectedDeprecation Passing a command name as the first argument of "Symfony\Bridge\Twig\Command\LintCommand::__construct" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.
74+
* @expectedException \RuntimeException
75+
* @expectedExceptionMessage The Twig environment needs to be set.
76+
*/
77+
public function testLegacyLintCommand()
78+
{
79+
$command = new LintCommand();
80+
81+
$application = new Application();
82+
$application->add($command);
83+
$command = $application->find('lint:twig');
84+
85+
$tester = new CommandTester($command);
86+
$tester->execute(array());
87+
}
88+
7189
/**
7290
* @return CommandTester
7391
*/
7492
private function createCommandTester()
7593
{
76-
$twig = new Environment(new FilesystemLoader());
77-
78-
$command = new LintCommand();
79-
$command->setTwigEnvironment($twig);
94+
$command = new LintCommand(new Environment(new FilesystemLoader()));
8095

8196
$application = new Application();
8297
$application->add($command);

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
3.4.0
5+
-----
6+
7+
* deprecated `Symfony\Bundle\TwigBundle\Command\DebugCommand`, use `Symfony\Bridge\Twig\Command\DebugCommand` instead
8+
* deprecated relying on the `ContainerAwareInterface` implementation for `Symfony\Bundle\TwigBundle\Command\LintCommand`
9+
410
3.3.0
511
-----
612

src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php

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

1212
namespace Symfony\Bundle\TwigBundle\Command;
1313

14+
@trigger_error(sprintf('The %s class is deprecated since version 3.4 and will be removed in 4.0. Use Symfony\Bridge\Twig\Command\DebugCommand instead.', DebugCommand::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Bridge\Twig\Command\DebugCommand as BaseDebugCommand;
1517
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1618
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
@@ -19,6 +21,8 @@
1921
* Lists twig functions, filters, globals and tests present in the current project.
2022
*
2123
* @author Jordi Boggiano <j.boggiano@seld.be>
24+
*
25+
* @deprecated since version 3.4, to be removed in 4.0.
2226
*/
2327
final class DebugCommand extends BaseDebugCommand implements ContainerAwareInterface
2428
{

src/Symfony/Bundle/TwigBundle/Command/LintCommand.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,9 @@
2424
*/
2525
final class LintCommand extends BaseLintCommand implements ContainerAwareInterface
2626
{
27+
// BC to be removed in 4.0
2728
use ContainerAwareTrait;
2829

29-
/**
30-
* {@inheritdoc}
31-
*/
32-
protected function getTwigEnvironment()
33-
{
34-
return $this->container->get('twig');
35-
}
36-
3730
/**
3831
* {@inheritdoc}
3932
*/

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

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

1414
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
1515
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Console\Application;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Reference;
1819
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -49,6 +50,10 @@ public function load(array $configs, ContainerBuilder $container)
4950
$loader->load('templating.xml');
5051
}
5152

53+
if (class_exists(Application::class)) {
54+
$loader->load('console.xml');
55+
}
56+
5257
if (!interface_exists('Symfony\Component\Translation\TranslatorInterface')) {
5358
$container->removeDefinition('twig.translation.extractor');
5459
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
<defaults public="false" />
9+
10+
<service id="twig.command.debug" class="Symfony\Bridge\Twig\Command\DebugCommand">
11+
<argument type="service" id="twig" />
12+
<tag name="console.command" command="debug:twig" />
13+
</service>
14+
15+
<service id="twig.command.lint" class="Symfony\Bundle\TwigBundle\Command\LintCommand">
16+
<argument type="service" id="twig" />
17+
<tag name="console.command" command="lint:twig" />
18+
</service>
19+
20+
<!-- BC to be removed in 4.0 -->
21+
<service id="console.command.symfony_bundle_twigbundle_command_debugcommand" class="Symfony\Bundle\TwigBundle\Command\DebugCommand" public="true">
22+
<deprecated>The "%service_id%" service is deprecated since Symfony 3.4 and will be removed in 4.0. Use "twig.command.debug" instead.</deprecated>
23+
</service>
24+
</services>
25+
</container>

0 commit comments

Comments
 (0)
0