Skip to content

Commit

Permalink
bug #17569 [FrameworkBundle] read commands from bundles when accessin…
Browse files Browse the repository at this point in the history
…g list (havvg)

This PR was merged into the 2.3 branch.

Discussion
----------

[FrameworkBundle] read commands from bundles when accessing list

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

This allows access to the list of commands registered by the kernel (bundle and later service ids) programmatically when you do not `run` the application.

Commits
-------

0fe3088 register commands from kernel when accessing list
  • Loading branch information
fabpot committed Feb 18, 2016
2 parents 1f22290 + 0fe3088 commit e932bae
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 14 deletions.
38 changes: 29 additions & 9 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Expand Up @@ -11,14 +11,14 @@

namespace Symfony\Bundle\FrameworkBundle\Console;

use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelInterface;

/**
* Application.
Expand Down Expand Up @@ -69,12 +69,6 @@ public function doRun(InputInterface $input, OutputInterface $output)
{
$this->kernel->boot();

if (!$this->commandsRegistered) {
$this->registerCommands();

$this->commandsRegistered = true;
}

$container = $this->kernel->getContainer();

foreach ($this->all() as $command) {
Expand All @@ -96,8 +90,34 @@ public function doRun(InputInterface $input, OutputInterface $output)
return parent::doRun($input, $output);
}

/**
* {@inheritdoc}
*/
public function get($name)
{
$this->registerCommands();

return parent::get($name);
}

/**
* {@inheritdoc}
*/
public function all($namespace = null)
{
$this->registerCommands();

return parent::all($namespace);
}

protected function registerCommands()
{
if ($this->commandsRegistered) {
return;
}

$this->commandsRegistered = true;

foreach ($this->kernel->getBundles() as $bundle) {
if ($bundle instanceof Bundle) {
$bundle->registerCommands($this);
Expand Down
Expand Up @@ -11,8 +11,9 @@

namespace Symfony\Bundle\FrameworkBundle\Tests\Console;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\ApplicationTester;
Expand All @@ -38,6 +39,89 @@ public function testBundleCommandsAreRegistered()

$application = new Application($kernel);
$application->doRun(new ArrayInput(array('list')), new NullOutput());

// Calling twice: registration should only be done once.
$application->doRun(new ArrayInput(array('list')), new NullOutput());
}

public function testBundleCommandsAreRetrievable()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');

$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array($bundle)))
;

$application = new Application($kernel);
$application->all();

// Calling twice: registration should only be done once.
$application->all();
}

public function testBundleSingleCommandIsRetrievable()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');

$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array($bundle)))
;

$application = new Application($kernel);

$command = new Command('example');
$application->add($command);

$this->assertSame($command, $application->get('example'));
}

public function testBundleCommandCanBeFound()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');

$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array($bundle)))
;

$application = new Application($kernel);

$command = new Command('example');
$application->add($command);

$this->assertSame($command, $application->find('example'));
}

public function testBundleCommandCanBeFoundByAlias()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');

$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array($bundle)))
;

$application = new Application($kernel);

$command = new Command('example');
$command->setAliases(array('alias'));
$application->add($command);

$this->assertSame($command, $application->find('alias'));
}

public function testBundleCommandsHaveRightContainer()
Expand Down
8 changes: 4 additions & 4 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -436,7 +436,7 @@ public function has($name)
public function getNamespaces()
{
$namespaces = array();
foreach ($this->commands as $command) {
foreach ($this->all() as $command) {
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));

foreach ($command->getAliases() as $alias) {
Expand Down Expand Up @@ -530,7 +530,7 @@ public function find($name)

// name
$commands = array();
foreach ($this->commands as $command) {
foreach ($this->all() as $command) {
$extractedNamespace = $this->extractNamespace($command->getName());
if ($extractedNamespace === $namespace
|| !empty($namespace) && 0 === strpos($extractedNamespace, $namespace)
Expand All @@ -556,7 +556,7 @@ public function find($name)

// aliases
$aliases = array();
foreach ($this->commands as $command) {
foreach ($this->all() as $command) {
foreach ($command->getAliases() as $alias) {
$extractedNamespace = $this->extractNamespace($alias);
if ($extractedNamespace === $namespace
Expand Down Expand Up @@ -1028,7 +1028,7 @@ private function findAlternativeCommands($name, $abbrevs)
return $item->getName();
};

return $this->findAlternatives($name, $this->commands, $abbrevs, $callback);
return $this->findAlternatives($name, $this->all(), $abbrevs, $callback);
}

/**
Expand Down

0 comments on commit e932bae

Please sign in to comment.