Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FrameworkBundle] read commands from bundles when accessing list #17569

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 1 commit into from
Feb 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 29 additions & 9 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reordering should be reverted


/**
* 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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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