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

Skip to content

Commit 1bd45b3

Browse files
committed
[FrameworkBundle] fixed regression where the command might have the wrong container if the application is reused several times
1 parent 9d53905 commit 1bd45b3

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/Symfony/Bundle/FrameworkBundle/Console/Application.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bundle\FrameworkBundle\Console;
1313

14+
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1415
use Symfony\Component\Console\Application as BaseApplication;
1516
use Symfony\Component\Console\Input\InputInterface;
1617
use Symfony\Component\Console\Input\InputOption;
@@ -66,13 +67,23 @@ public function getKernel()
6667
*/
6768
public function doRun(InputInterface $input, OutputInterface $output)
6869
{
70+
$this->kernel->boot();
71+
6972
if (!$this->commandsRegistered) {
7073
$this->registerCommands();
7174

7275
$this->commandsRegistered = true;
7376
}
7477

75-
$this->setDispatcher($this->kernel->getContainer()->get('event_dispatcher'));
78+
$container = $this->kernel->getContainer();
79+
80+
foreach ($this->all() as $command) {
81+
if ($command instanceof ContainerAwareCommand) {
82+
$command->setContainer($container);
83+
}
84+
}
85+
86+
$this->setDispatcher($container->get('event_dispatcher'));
7687

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

8899
protected function registerCommands()
89100
{
90-
$this->kernel->boot();
91-
92101
foreach ($this->kernel->getBundles() as $bundle) {
93102
if ($bundle instanceof Bundle) {
94103
$bundle->registerCommands($this);

src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\Console\Application;
1616
use Symfony\Component\Console\Input\ArrayInput;
1717
use Symfony\Component\Console\Output\NullOutput;
18+
use Symfony\Component\Console\Tester\ApplicationTester;
1819

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

43+
public function testBundleCommandsHaveRightContainer()
44+
{
45+
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
46+
$command->setCode(function () {});
47+
$command->expects($this->exactly(2))->method('setContainer');
48+
49+
$application = new Application($this->getKernel(array()));
50+
$application->setAutoExit(false);
51+
$application->setCatchExceptions(false);
52+
$application->add($command);
53+
$tester = new ApplicationTester($application);
54+
55+
// set container is called here
56+
$tester->run(array('command' => 'foo'));
57+
58+
// as the container might have change between two runs, setContainer must called again
59+
$tester->run(array('command' => 'foo'));
60+
}
61+
4262
private function getKernel(array $bundles)
4363
{
4464
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
@@ -49,7 +69,7 @@ private function getKernel(array $bundles)
4969

5070
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
5171
$container
52-
->expects($this->once())
72+
->expects($this->atLeastOnce())
5373
->method('get')
5474
->with($this->equalTo('event_dispatcher'))
5575
->will($this->returnValue($dispatcher))

0 commit comments

Comments
 (0)