8000 [FrameworkBundle] read commands from bundles when accessing list by havvg · Pull Request #17574 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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

Closed
wants to merge 1 commit into from
Closed
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
40 changes: 31 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;

/**
* 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,36 @@ 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;

$this->kernel->boot();

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

foreach ($this->kernel->getBundles() as $bundle) {
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,69 @@ 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->getKernel(array($bundle), false);

$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->getKernel(array($bundle), false);

$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->getKernel(array($bundle), false);

$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->getKernel(array($bundle), false);

$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 All @@ -59,21 +123,23 @@ public function testBundleCommandsHaveRightContainer()
$tester->run(array('command' => 'foo'));
}

private function getKernel(array $bundles)
private function getKernel(array $bundles, $withDispatcher = true)
{
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher
->expects($this->atLeastOnce())
->method('dispatch')
;

$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
$container
->expects($this->atLeastOnce())
->method('get')
->with($this->equalTo('event_dispatcher'))
->will($this->returnValue($dispatcher))
;

if ($withDispatcher) {
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher
->expects($this->atLeastOnce())
->method('dispatch');

$container
->expects($this->atLeastOnce())
->method('get')
->with($this->equalTo('event_dispatcher'))
->will($this->returnValue($dispatcher));
}

$container
->expects($this->once())
->method('hasParameter')
Expand Down
0