8000 [Console] Simplify parameters in DI by Tobion · Pull Request #25593 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Console] Simplify parameters in DI #25593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Load 8000 ing
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,11 @@ public function process(ContainerBuilder $container)
$lazyCommandMap = array();
$lazyCommandRefs = array();
$serviceIds = array();
$lazyServiceIds = array();

foreach ($commandServices as $id => $tags) {
$definition = $container->getDefinition($id);
$class = $container->getParameterBag()->resolveValue($definition->getClass());

$commandId = 'console.command.'.strtolower(str_replace('\\', '_', $class));

if (isset($tags[0]['command'])) {
$commandName = $tags[0]['command'];
} else {
Expand All @@ -62,20 +59,16 @@ public function process(ContainerBuilder $container)
}

if (null === $commandName) {
if (isset($serviceIds[$commandId]) || $container->hasAlias($commandId)) {
$commandId = $commandId.'_'.$id;
}
if (!$definition->isPublic() || $definition->isPrivate()) {
$commandId = 'console.command.public_alias.'.$id;
$container->setAlias($commandId, $id)->setPublic(true);
$id = $commandId;
}
$serviceIds[$commandId] = $id;
$serviceIds[] = $id;

continue;
}

$serviceIds[$commandId] = $id;
$lazyServiceIds[$id] = true;
unset($tags[0]);
$lazyCommandMap[$commandName] = $id;
$lazyCommandRefs[$id] = new TypedReference($id, $class);
Expand All @@ -101,6 +94,5 @@ public function process(ContainerBuilder $container)
->setArguments(array(ServiceLocatorTagPass::register($container, $lazyCommandRefs), $lazyCommandMap));

$container->setParameter('console.command.ids', $serviceIds);
$container->setParameter('console.lazy_command.ids', $lazyServiceIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,27 @@ public function testProcess($public)
$container->addCompilerPass(new AddConsoleCommandPass());
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');

$id = 'my-command';
$definition = new Definition('%my-command.class%');
$definition->setPublic($public);
$definition->addTag('console.command');
$container->setDefinition('my-command', $definition);
$container->setDefinition($id, $definition);

$container->compile();

$alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
$alias = 'console.command.public_alias.my-command';

if ($public) {
$this->assertFalse($container->hasAlias($alias));
$id = 'my-command';
} else {
$id = $alias;
// The alias is replaced by a Definition by the ReplaceAliasByActualDefinitionPass
// in case the original service is private
$this->assertFalse($container->hasDefinition('my-command'));
$this->assertFalse($container->hasDefinition($id));
$this->assertTrue($container->hasDefinition($alias));
}

$this->assertTrue($container->hasParameter('console.command.ids'));
$this->assertSame(array($alias => $id), $container->getParameter('console.command.ids'));
$this->assertSame(array($public ? $id : $alias), $container->getParameter('console.command.ids'));
}

public function testProcessRegistersLazyCommands()
Expand All @@ -73,8 +72,7 @@ public function testProcessRegistersLazyCommands()
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
$this->assertSame(array('my:command' => 'my-command', 'my:alias' => 'my-command'), $commandLoader->getArgument(1));
$this->assertEquals(array(array('my-command' => new ServiceClosureArgument(new TypedReference('my-command', MyCommand::class)))), $commandLocator->getArguments());
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_mycommand' => 'my-command'), $container->getParameter('console.command.ids'));
$this->assertSame(array('my-command' => true), $container->getParameter('console.lazy_command.ids'));
$this->assertSame(array(), $container->getParameter('console.command.ids'));
$this->assertSame(array(array('setName', array('my:command')), array('setAliases', array(array('my:alias')))), $command->getMethodCalls());
}

Expand All @@ -96,8 +94,7 @@ public function testProcessFallsBackToDefaultName()
$this->assertSame(ContainerCommandLoader::class, $commandLoader->getClass());
$this->assertSame(array('default' => 'with-default-name'), $commandLoader->getArgument(1));
$this->assertEquals(array(array('with-default-name' => new ServiceClosureArgument(new TypedReference('with-default-name', NamedCommand::class)))), $commandLocator->getArguments());
$this->assertSame(array('console.command.symfony_component_console_tests_dependencyinjection_namedcommand' => 'with-default-name'), $container->getParameter('console.command.ids'));
$this->assertSame(array('with-default-name' => true), $container->getParameter('console.lazy_command.ids'));
$this->assertSame(array(), $container->getParameter('console.command.ids'));

$container = new ContainerBuilder();
$container
Expand Down Expand Up @@ -170,10 +167,9 @@ public function testProcessPrivateServicesWithSameCommand()

(new AddConsoleCommandPass())->process($container);

$alias1 = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
$alias2 = $alias1.'_my-command2';
$this->assertTrue($container->hasAlias($alias1));
$this->assertTrue($container->hasAlias($alias2));
$aliasPrefix = 'console.command.public_alias.';
$this->assertTrue($container->hasAlias($aliasPrefix.'my-command1'));
$this->assertTrue($container->hasAlias($aliasPrefix.'my-command2'));
}
}

Expand Down
0