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

Skip to content

[FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times #8599

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
Jul 29, 2013
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
15 changes: 12 additions & 3 deletions src/Symfony/Bundle/FrameworkBundle/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Bundle\FrameworkBundle\Console;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -66,13 +67,23 @@ public function getKernel()
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->kernel->boot();

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

$this->commandsRegistered = true;
}

$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
$container = $this->kernel->getContainer();

foreach ($this->all() as $command) {
if ($command instanceof ContainerAwareCommand) {
$command->setContainer($container);
}
}

$this->setDispatcher($container->get('event_dispatcher'));

if (true === $input->hasParameterOption(array('--shell', '-s'))) {
$shell = new Shell($this);
Expand All @@ -87,8 +98,6 @@ public function doRun(InputInterface $input, OutputInterface $output)

protected function registerCommands()
{
$this->kernel->boot();

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 @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\ApplicationTester;

class ApplicationTest extends TestCase
{
Expand All @@ -39,6 +40,25 @@ public function testBundleCommandsAreRegistered()
$application->doRun(new ArrayInput(array('list')), new NullOutput());
}

public function testBundleCommandsHaveRightContainer()
{
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
$command->setCode(function () {});
$command->expects($this->exactly(2))->method('setContainer');

$application = new Application($this->getKernel(array()));
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command);
$tester = new ApplicationTester($application);

// set container is called here
$tester->run(array('command' => 'foo'));

// as the container might have change between two runs, setContainer must called again
$tester->run(array('command' => 'foo'));
}

private function getKernel(array $bundles)
{
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
Expand All @@ -49,7 +69,7 @@ private function getKernel(array $bundles)

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