-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Console] Move AddConsoleCommandPass from FrameworkBundle to Console. #19443
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | F438 tr>||
<?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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The deprecated pass must still be tested (the test will be removed at the same time than the class).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted the test.