-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #19443 [Console] Move AddConsoleCommandPass from FrameworkBun…
…dle to Console. (bcremer) This PR was merged into the 3.3-dev branch. Discussion ---------- [Console] Move AddConsoleCommandPass from FrameworkBundle to Console. | Q | A | | --- | --- | | Branch? | master | | Bug fix? | no | | New feature? | yes | | BC breaks? | yes | | Deprecations? | yes | | Tests pass? | yes | | Fixed tickets | #19440 | | License | MIT | | Doc PR | - | Commits ------- 7743989 Move AddConsoleCommandPass from FrameworkBundle to Console.
- Loading branch information
Showing
11 changed files
with
169 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
3.3.0 | ||
----- | ||
|
||
* added `AddConsoleCommandPass` (originally in FrameworkBundle) | ||
|
||
3.2.0 | ||
------ | ||
|
||
|
46 changes: 46 additions & 0 deletions
46
src/Symfony/Component/Console/DependencyInjection/AddConsoleCommandPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\DependencyInjection; | ||
|
||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
|
||
/** | ||
* Registers console commands. | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class AddConsoleCommandPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
$commandServices = $container->findTaggedServiceIds('console.command'); | ||
$serviceIds = array(); | ||
|
||
foreach ($commandServices as $id => $tags) { | ||
$definition = $container->getDefinition($id); | ||
|
||
if ($definition->isAbstract()) { | ||
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must not be abstract.', $id)); | ||
} | ||
|
||
$class = $container->getParameterBag()->resolveValue($definition->getClass()); | ||
if (!is_subclass_of($class, 'Symfony\\Component\\Console\\Command\\Command')) { | ||
throw new \InvalidArgumentException(sprintf('The service "%s" tagged "console.command" must be a subclass of "Symfony\\Component\\Console\\Command\\Command".', $id)); | ||
} | ||
$container->setAlias($serviceId = 'console.command.'.strtolower(str_replace('\\', '_', $class)), $id); | ||
$serviceIds[] = $definition->isPublic() ? $id : $serviceId; | ||
} | ||
|
||
$container->setParameter('console.command.ids', $serviceIds); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Symfony/Component/Console/Tests/DependencyInjection/AddConsoleCommandPassTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Console\Tests\DependencyInjection; | ||
|
||
use Symfony\Component\Console\DependencyInjection\AddConsoleCommandPass; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Definition; | ||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class AddConsoleCommandPassTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider visibilityProvider | ||
*/ | ||
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'); | ||
|
||
$definition = new Definition('%my-command.class%'); | ||
$definition->setPublic($public); | ||
$definition->addTag('console.command'); | ||
$container->setDefinition('my-command', $definition); | ||
|
||
$container->compile(); | ||
|
||
$id = $public ? 'my-command' : 'console.command.symfony_component_console_tests_dependencyinjection_mycommand'; | ||
$this->assertTrue($container->hasParameter('console.command.ids')); | ||
$this->assertSame(array($id), $container->getParameter('console.command.ids')); | ||
} | ||
|
||
public function visibilityProvider() | ||
{ | ||
return array( | ||
array(true), | ||
array(false), | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage The service "my-command" tagged "console.command" must not be abstract. | ||
*/ | ||
public function testProcessThrowAnExceptionIfTheServiceIsAbstract() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->setResourceTracking(false); | ||
$container->addCompilerPass(new AddConsoleCommandPass()); | ||
|
||
$definition = new Definition('Symfony\Component\Console\Tests\DependencyInjection\MyCommand'); | ||
$definition->addTag('console.command'); | ||
$definition->setAbstract(true); | ||
$container->setDefinition('my-command', $definition); | ||
|
||
$container->compile(); | ||
} | ||
|
||
/** | ||
* @expectedException \InvalidArgumentException | ||
* @expectedExceptionMessage The service "my-command" tagged "console.command" must be a subclass of "Symfony\Component\Console\Command\Command". | ||
*/ | ||
public function testProcessThrowAnExceptionIfTheServiceIsNotASubclassOfCommand() | ||
{ | ||
$container = new ContainerBuilder(); | ||
$container->setResourceTracking(false); | ||
$container->addCompilerPass(new AddConsoleCommandPass()); | ||
|
||
$definition = new Definition('SplObjectStorage'); | ||
$definition->addTag('console.command'); | ||
$container->setDefinition('my-command', $definition); | ||
|
||
$container->compile(); | ||
} | ||
} | ||
|
||
class MyCommand extends Command | ||
{ | ||
} | ||
|
||
class ExtensionPresentBundle extends Bundle | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters