register commands from kernel when accessing list
This commit is contained in:
parent
025f761798
commit
0fe3088dea
@ -11,14 +11,14 @@
|
|||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Console;
|
namespace Symfony\Bundle\FrameworkBundle\Console;
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
|
||||||
use Symfony\Component\Console\Application as BaseApplication;
|
use Symfony\Component\Console\Application as BaseApplication;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\HttpKernel\KernelInterface;
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||||
use Symfony\Component\HttpKernel\Kernel;
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||||
|
use Symfony\Component\HttpKernel\Kernel;
|
||||||
|
use Symfony\Component\HttpKernel\KernelInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Application.
|
* Application.
|
||||||
@ -69,12 +69,6 @@ class Application extends BaseApplication
|
|||||||
{
|
{
|
||||||
$this->kernel->boot();
|
$this->kernel->boot();
|
||||||
|
|
||||||
if (!$this->commandsRegistered) {
|
|
||||||
$this->registerCommands();
|
|
||||||
|
|
||||||
$this->commandsRegistered = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$container = $this->kernel->getContainer();
|
$container = $this->kernel->getContainer();
|
||||||
|
|
||||||
foreach ($this->all() as $command) {
|
foreach ($this->all() as $command) {
|
||||||
@ -96,8 +90,34 @@ class Application extends BaseApplication
|
|||||||
return parent::doRun($input, $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()
|
protected function registerCommands()
|
||||||
{
|
{
|
||||||
|
if ($this->commandsRegistered) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->commandsRegistered = true;
|
||||||
|
|
||||||
foreach ($this->kernel->getBundles() as $bundle) {
|
foreach ($this->kernel->getBundles() as $bundle) {
|
||||||
if ($bundle instanceof Bundle) {
|
if ($bundle instanceof Bundle) {
|
||||||
$bundle->registerCommands($this);
|
$bundle->registerCommands($this);
|
||||||
|
@ -11,8 +11,9 @@
|
|||||||
|
|
||||||
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
|
namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
|
||||||
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
|
|
||||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
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\Input\ArrayInput;
|
||||||
use Symfony\Component\Console\Output\NullOutput;
|
use Symfony\Component\Console\Output\NullOutput;
|
||||||
use Symfony\Component\Console\Tester\ApplicationTester;
|
use Symfony\Component\Console\Tester\ApplicationTester;
|
||||||
@ -38,6 +39,89 @@ class ApplicationTest extends TestCase
|
|||||||
|
|
||||||
$application = new Application($kernel);
|
$application = new Application($kernel);
|
||||||
$application->doRun(new ArrayInput(array('list')), new NullOutput());
|
$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()
|
public function testBundleCommandsHaveRightContainer()
|
||||||
|
@ -436,7 +436,7 @@ class Application
|
|||||||
public function getNamespaces()
|
public function getNamespaces()
|
||||||
{
|
{
|
||||||
$namespaces = array();
|
$namespaces = array();
|
||||||
foreach ($this->commands as $command) {
|
foreach ($this->all() as $command) {
|
||||||
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
|
$namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
|
||||||
|
|
||||||
foreach ($command->getAliases() as $alias) {
|
foreach ($command->getAliases() as $alias) {
|
||||||
@ -530,7 +530,7 @@ class Application
|
|||||||
|
|
||||||
// name
|
// name
|
||||||
$commands = array();
|
$commands = array();
|
||||||
foreach ($this->commands as $command) {
|
foreach ($this->all() as $command) {
|
||||||
$extractedNamespace = $this->extractNamespace($command->getName());
|
$extractedNamespace = $this->extractNamespace($command->getName());
|
||||||
if ($extractedNamespace === $namespace
|
if ($extractedNamespace === $namespace
|
||||||
|| !empty($namespace) && 0 === strpos($extractedNamespace, $namespace)
|
|| !empty($namespace) && 0 === strpos($extractedNamespace, $namespace)
|
||||||
@ -556,7 +556,7 @@ class Application
|
|||||||
|
|
||||||
// aliases
|
// aliases
|
||||||
$aliases = array();
|
$aliases = array();
|
||||||
foreach ($this->commands as $command) {
|
foreach ($this->all() as $command) {
|
||||||
foreach ($command->getAliases() as $alias) {
|
foreach ($command->getAliases() as $alias) {
|
||||||
$extractedNamespace = $this->extractNamespace($alias);
|
$extractedNamespace = $this->extractNamespace($alias);
|
||||||
if ($extractedNamespace === $namespace
|
if ($extractedNamespace === $namespace
|
||||||
@ -1028,7 +1028,7 @@ class Application
|
|||||||
return $item->getName();
|
return $item->getName();
|
||||||
};
|
};
|
||||||
|
|
||||||
return $this->findAlternatives($name, $this->commands, $abbrevs, $callback);
|
return $this->findAlternatives($name, $this->all(), $abbrevs, $callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user