register commands from kernel when accessing list

This commit is contained in:
Toni Uebernickel 2016-01-27 13:18:03 +01:00
parent 025f761798
commit 0fe3088dea
3 changed files with 118 additions and 14 deletions

View File

@ -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.
@ -69,12 +69,6 @@ class Application extends BaseApplication
{
$this->kernel->boot();
if (!$this->commandsRegistered) {
$this->registerCommands();
$this->commandsRegistered = true;
}
$container = $this->kernel->getContainer();
foreach ($this->all() as $command) {
@ -96,8 +90,34 @@ class Application extends BaseApplication
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);

View File

@ -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;
@ -38,6 +39,89 @@ class ApplicationTest extends TestCase
$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()

View File

@ -436,7 +436,7 @@ class Application
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) {
@ -530,7 +530,7 @@ class Application
// 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)
@ -556,7 +556,7 @@ class Application
// 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
@ -1028,7 +1028,7 @@ class Application
return $item->getName();
};
return $this->findAlternatives($name, $this->commands, $abbrevs, $callback);
return $this->findAlternatives($name, $this->all(), $abbrevs, $callback);
}
/**