Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] Don't create unused alias if the command is public #22410

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
Apr 13, 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.
Loading
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 @@ -40,16 +40,18 @@ public function testProcess($public)
$container->compile();

$alias = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
if ($container->hasAlias($alias)) {
$this->assertSame('my-command', (string) $container->getAlias($alias));

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->assertTrue($container->hasDefinition($alias));
}

$id = $public ? 'my-command' : 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
$this->assertTrue($container->hasParameter('console.command.ids'));
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
}
Expand Down Expand Up @@ -95,22 +97,21 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
$container->compile();
}

public function testProcessServicesWithSameCommand()
public function testProcessPrivateServicesWithSameCommand()
{
$container = new ContainerBuilder();
$container->addCompilerPass(new AddConsoleCommandPass());
$className = 'Symfony\Bundle\FrameworkBundle\Tests\DependencyInjection\Compiler\MyCommand';

$definition1 = new Definition($className);
$definition1->addTag('console.command');
$definition1->addTag('console.command')->setPublic(false);

$definition2 = new Definition($className);
$definition2->addTag('console.command');
$definition2->addTag('console.command')->setPublic(false);

$container->setDefinition('my-command1', $definition1);
$container->setDefinition('my-command2', $definition2);

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

$alias1 = 'console.command.symfony_bundle_frameworkbundle_tests_dependencyinjection_compiler_mycommand';
$alias2 = $alias1.'_my-command2';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@ public function process(ContainerBuilder $container)
throw new InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "%s".', $id, Command::class));
}

$serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
if ($container->hasAlias($serviceId)) {
$serviceId = $serviceId.'_'.$id;
if (!$definition->isPublic()) {
$serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class));
if ($container->hasAlias($serviceId)) {
$serviceId = $serviceId.'_'.$id;
}
$container->setAlias($serviceId, $id);
$id = $serviceId;
}
$container->setAlias($serviceId, $id);
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;

$serviceIds[] = $id;
}

$container->setParameter('console.command.ids', $serviceIds);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class AddConsoleCommandPassTest extends TestCase
public function testProcess($public)
{
$container = new ContainerBuilder();
$container->setResourceTracking(false);
$container->addCompilerPass(new AddConsoleCommandPass());
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');

Expand All @@ -37,7 +36,19 @@ public function testProcess($public)

$container->compile();

$id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
$alias = 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';

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->assertTrue($container->hasDefinition($alias));
}

$this->assertTrue($container->hasParameter('console.command.ids'));
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
}
Expand Down Expand Up @@ -84,6 +95,28 @@ public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()

$container->compile();
}

public function testProcessPrivateServicesWithSameCommand()
{
$container = new ContainerBuilder();
$className = 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand';

$definition1 = new Definition($className);
$definition1->addTag('console.command')->setPublic(false);

$definition2 = new Definition($className);
$definition2->addTag('console.command')->setPublic(false);

$container->setDefinition('my-command1', $definition1);
$container->setDefinition('my-command2', $definition2);

(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));
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test has been added to the legacy test case by mistake, copied it there.

}

class MyCommand extends Command
Expand Down