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

Skip to content

Commit 17385a7

Browse files
committed
Move AddConsoleCommandPass from FrameworkBundle to Console.
1 parent 194dcf3 commit 17385a7

File tree

8 files changed

+177
-3
lines changed

8 files changed

+177
-3
lines changed

UPGRADE-3.2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ FrameworkBundle
66

77
* The `Controller::getUser()` method has been deprecated and will be removed in
88
Symfony 4.0; typehint the security user object in the action instead.
9+
* The `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass` has been deprecated. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
910

1011
DependencyInjection
1112
-------------------

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* The `Controller::getUser()` method has been deprecated and will be removed in
88
Symfony 4.0; typehint the security user object in the action instead.
9+
* Deprecated `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass`. Use `Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass` instead.
910

1011
3.1.0
1112
-----

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/AddConsoleCommandPass.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;
1313

14+
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass instead.', AddConsoleCommandPass::class), E_USER_DEPRECATED);
15+
1416
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618

1719
/**
18-
* AddConsoleCommandPass.
20+
* Registers console commands.
1921
*
2022
* @author Grégoire Pineau <[email protected]>
23+
*
24+
* @deprecated since version 3.2, to be removed in 4.0. Use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass instead.
2125
*/
2226
class AddConsoleCommandPass implements CompilerPassInterface
2327
{

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConstraintValidatorsPass;
1515
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddValidatorInitializersPass;
16-
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass;
16+
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddConsoleCommandPass as LegacyAddConsoleCommandPass;
1717
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\CachePoolPass;
1818
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ControllerArgumentValueResolverPass;
1919
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\FormPass;
@@ -33,6 +33,7 @@
3333
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\SerializerPass;
3434
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\UnusedTagsPass;
3535
use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\ConfigCachePass;
36+
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
3637
use Symfony\Component\Debug\ErrorHandler;
3738
use Symfony\Component\DependencyInjection\ContainerBuilder;
3839
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
@@ -77,7 +78,6 @@ public function build(ContainerBuilder $container)
7778
$container->addCompilerPass(new TemplatingPass());
7879
$container->addCompilerPass(new AddConstraintValidatorsPass());
7980
$container->addCompilerPass(new AddValidatorInitializersPass());
80-
$container->addCompilerPass(new AddConsoleCommandPass());
8181
$container->addCompilerPass(new FormPass());
8282
$container->addCompilerPass(new TranslatorPass());
8383
$container->addCompilerPass(new LoggingTranslatorPass());
@@ -92,6 +92,12 @@ public function build(ContainerBuilder $container)
9292
$container->addCompilerPass(new ControllerArgumentValueResolverPass());
9393
$container->addCompilerPass(new CachePoolPass());
9494

95+
if (class_exists(AddConsoleCommandPass::class)) {
96+
$container->addCompilerPass(new AddConsoleCommandPass());
97+
} else {
98+
$container->addCompilerPass(new LegacyAddConsoleCommandPass());
99+
}
100+
95101
if ($container->getParameter('kernel.debug')) {
96102
$container->addCompilerPass(new UnusedTagsPass(), PassConfig::TYPE_AFTER_REMOVING);
97103
$container->addCompilerPass(new ContainerBuilderDebugDumpPass(), PassConfig::TYPE_AFTER_REMOVING);

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* added `setStream()` and `getStream()` methods to Input (implement StreamableInputInterface)
99
* added StreamableInputInterface
1010
* added LockableTrait
11+
* added `AddConsoleCommandPass` (originally in FrameworkBundle)
1112

1213
3.1.0
1314
-----
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* Registers console commands.
19+
*
20+
* @author Grégoire Pineau <[email protected]>
21+
*/
22+
class AddConsoleCommandPass implements CompilerPassInterface
23+
{
24+
public function process(ContainerBuilder $container)
25+
{
26+
$commandServices = $container->findTaggedServiceIds('console.command');
27+
$serviceIds = array();
28+
29+
foreach ($commandServices as $id => $tags) {
30+
$definition = $container->getDefinition($id);
31+
32+
if ($definition->isAbstract()) {
33+
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id));
34+
}
35+
36+
$class = $container->getParameterBag()->resolveValue($definition->getClass());
37+
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) {
38+
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id));
39+
}
40+
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id);
41+
$serviceIds[] = $definition->isPublic() ? $id : $serviceId;
42+
}
43+
44+
$container->setParameter('console.command.ids', $serviceIds);
45+
}
46+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\DependencyInjection;
13+
14+
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass;
15+
use Symfony\Component\Console\Command\Command;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\HttpKernel\Bundle\Bundle;
19+
20+
class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @dataProvider visibilityProvider
24+
*/
25+
public function testProcess($public)
26+
{
27+
$container = new ContainerBuilder();
28+
$container->addCompilerPass(new AddConsoleCommandPass());
29+
$container->setParameter('my-command.class', 'Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
30+
31+
$definition = new Definition('%my-command.class%');
32+
$definition->setPublic($public);
33+
$definition->addTag('console.command');
34+
$container->setDefinition('my-command', $definition);
35+
36+
$container->compile();
37+
38+
$id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand';
39+
$this->assertTrue($container->hasParameter('console.command.ids'));
40+
$this->assertSame(array($id), $container->getParameter('console.command.ids'));
41+
}
42+
43+
public function visibilityProvider()
44+
{
45+
return array(
46+
array(true),
47+
array(false),
48+
);
49+
}
50+
51+
/**
52+
* @expectedException \InvalidArgumentException
53+
* @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract.
54+
*/
55+
public function testProcessThrowAnExceptionIfTheServiceIsAbstract()
56+
{
57+
$container = new ContainerBuilder();
58+
$container->addCompilerPass(new AddConsoleCommandPass());
59+
60+
$definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
61+
$definition->addTag('console.command');
62+
$definition->setAbstract(true);
63+
$container->setDefinition('my-command', $definition);
64+
65+
$container->compile();
66+
}
67+
68+
/**
69+
* @expectedException \InvalidArgumentException
70+
* @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command".
71+
*/
72+
public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand()
73+
{
74+
$container = new ContainerBuilder();
75+
$container->addCompilerPass(new AddConsoleCommandPass());
76+
77+
$definition = new Definition('SplObjectStorage');
78+
$definition->addTag('console.command');
79+
$container->setDefinition('my-command', $definition);
80+
81+
$container->compile();
82+
}
83+
84+
public function testHttpKernelRegisterCommandsIngoreCommandAsAService()
85+
{
86+
$container = new ContainerBuilder();
87+
$container->addCompilerPass(new AddConsoleCommandPass());
88+
$definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand');
89+
$definition->addTag('console.command');
90+
$container->setDefinition('my-command', $definition);
91+
$container->compile();
92+
93+
$application = $this->getMock('Symfony\Component\Console\Application');
94+
// Never called, because it's the
95+
// Symfony\Bundle\FrameworkBundle\Console\Application that register
96+
// commands as a service
97+
$application->expects($this->never())->method('add');
98+
99+
$bundle = new ExtensionPresentBundle();
100+
101+
$bundle->setContainer($container);
102+
$bundle->registerCommands($application);
103+
}
104+
}
105+
106+
class MyCommand extends Command
107+
{
108+
}
109+
110+
class ExtensionPresentBundle extends Bundle
111+
{
112+
}

src/Symfony/Component/Console/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
"symfony/polyfill-mbstring": "~1.0"
2121
},
2222
"require-dev": {
23+
"symfony/http-kernel": "~2.8|~3.0",
2324
"symfony/event-dispatcher": "~2.8|~3.0",
25+
"symfony/dependency-injection": "~2.8|~3.0",
2426
"symfony/filesystem": "~2.8|~3.0",
2527
"symfony/process": "~2.8|~3.0",
2628
"psr/log": "~1.0"
2729
},
2830
"suggest": {
31+
"symfony/dependency-injection": "",
2932
"symfony/event-dispatcher": "",
3033
"symfony/filesystem": "",
3134
"symfony/process": "",

0 commit comments

Comments
 (0)