8000 minor #23556 [Console] Fix registering lazy command services with aut… · symfony/symfony@e02ba32 · GitHub
[go: up one dir, main page]

Skip to content

Commit e02ba32

Browse files
committed
minor #23556 [Console] Fix registering lazy command services with autoconfigure enabled (chalasr)
This PR was merged into the 3.4 branch. Discussion ---------- [Console] Fix registering lazy command services with autoconfigure enabled | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a For ```yaml _defaults: autoconfigure: true App\: resource: '../../src/*' App\Command\FooCommand: tags: - { name: console.command, command: foo } ``` Before you get the following error: > Missing "command" attribute on tag "console.command" for service "App\Command\FooCommand" Now the command is lazy. ---- Btw, @Tobion's #22734 (comment) > Wouldn't it be more straightforward if aliases are just the additional tags using the command attribute as well? Then there is no need for an alias property at all and this strange condition doesn't apply either. Partially addressed here by removing the need for repeating the `command` attribute on each `console.command` tag ```yaml # before tags: - { name: console.command, command: foo } - { name: console.command, command: foo, alias: foobar } # after tags: - { name: console.command, command: foo } - { name: console.command, alias: foobar } ``` Tobias proposal: ```yaml tags: - { name: console.command, command: app:my-command } - { name: console.command, command: app:my-alias } ``` I wanted to propose exactly the same at first, but finally found more clear to add a specific attribute for aliases, especially because relying on the order on which tags are defined sounds less good to me. Please tell me about your preference. (And sorry for the noise around this feature, I want to polish it for 3.4) Commits ------- 8a71aa3 Fix registering lazy command services with autoconfigure enabled
2 parents 5556a3a + 8a71aa3 commit e02ba32

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,15 @@ public function process(ContainerBuilder $container)
7070

7171
$serviceIds[$commandId] = false;
7272
$commandName = $tags[0]['command'];
73+
unset($tags[0]);
7374
$lazyCommandMap[$commandName] = $id;
7475
$lazyCommandRefs[$id] = new TypedReference($id, $class);
7576
$aliases = array();
7677

7778
foreach ($tags as $tag) {
78-
if (!isset($tag['command'])) {
79-
throw new InvalidArgumentException(sprintf('Missing "command" attribute on tag "%s" for service "%s".', $this->commandTag, $id));
80-
}
81-
if ($commandName !== $tag['command']) {
82-
throw new InvalidArgumentException(sprintf('The "command" attribute must be the same on each "%s" tag for service "%s".', $this->commandTag, $id));
83-
}
84-
if (isset($tag['alias'])) {
85-
$aliases[] = $tag['alias'];
86-
$lazyCommandMap[$tag['alias']] = $id;
79+
if (isset($tag['command'])) {
80+
$aliases[] = $tag['command'];
81+
$lazyCommandMap[$tag['command']] = $id;
8782
}
8883
}
8984

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,8 +1429,9 @@ public function testRunLazyCommandService()
14291429
$container->addCompilerPass(new AddConsoleCommandPass());
14301430
$container
14311431
->register('lazy-command', LazyCommand::class)
1432-
->addTag('console.command', array('command' => 'lazy:command', 'alias' => 'lazy:alias'))
1433-
->addTag('console.command', array('command' => 'lazy:command', 'alias' => 'lazy:alias2'));
1432+
->addTag('console.command', array('command' => 'lazy:command'))
1433+
->addTag('console.command', array('command' => 'lazy:alias'))
1434+
->addTag('console.command', array('command' => 'lazy:alias2'));
14341435
$container->compile();
14351436

14361437
$application = new Application();

src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,14 @@ public function testProcess($public)
5656
$this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
5757
}
5858

59-
public function testProcessRegisterLazyCommands()
59+
public function testProcessRegistersLazyCommands()
6060
{
6161
$container = new ContainerBuilder();
62-
$container
62+
$command = $container
6363
->register('my-command', MyCommand::class)
6464
->setPublic(false)
65-
->addTag('console.command', array('command' => 'my:command', 'alias' => 'my:alias'))
65+
->addTag('console.command', array('command' => 'my:command'))
66+
->addTag('console.command', array('command' => 'my:alias'))
6667
;
6768

6869
(new AddConsoleCommandPass())->process($container);
@@ -74,6 +75,7 @@ public function testProcessRegisterLazyCommands()
7475
$this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
7576
$this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments());
7677
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_mycommand' => false), $container->getParameter('console.command.ids'));
78+
$this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
7779
}
7880

7981
public function visibilityProvider()

0 commit comments

Comments
 (0)
0