Browse Source

merged branch web-dev/bundle-interface-command-fatal-error (PR #5133)

Commits
-------

30bcb57 Added a test case to demonstrate the fatal error occuring when a Bundle implementing BundleInterface only is registered in the kernel.

Discussion
----------

Fatal error in FrameworkBundle console application

A fatal error is generated in the `FrameworkBundle` console application when a bundle is added implementing [`BundleInterface`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php)

This is because the method `registerCommands` does not exist on this interface and is instead only defined on the concrete [`Bundle`](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/Bundle/Bundle.php#L173) implementation.

The workaround for this issue is simple - implement an empty method for `registerCommands` in the bundle implementation so that the fatal error is not triggered.

However this issue should probably be fixed by either restricting bundles to the Bundle class or expanding the `BundleInterface` to include the `registerCommands` method signature. Both of these fixes will introduce a BC break into the API so I would suggest creating a fix for 2.0 which includes method detection in the `registerCommands` method of the [`Console\Application`](https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Console/Application.php#L80) class.

I'm happy to submit the fix for this - however I would like some direction from the SF2 dev team as to which way they would like to resolve this.

The PR currently only contains a unit test that proves this behaviour.

---------------------------------------------------------------------------

by travisbot at 2012-08-01T02:42:55Z

This pull request [fails](http://travis-ci.org/symfony/symfony/builds/2006350) (merged 30bcb572 into 1da896dc).

---------------------------------------------------------------------------

by henrikbjorn at 2012-08-01T05:50:16Z

I am thinking a instanceof check might be the most reasonable in this case. But in master it should proberly be fixed by adding the method to the interface.

/cc @stof any comments if that is to be done?

---------------------------------------------------------------------------

by stof at 2012-08-01T08:53:02Z

yeah, for 2.0, we cannot change the interface.
Fabien Potencier 12 năm trước cách đây
mục cha
commit
bb57e54320

+ 35 - 0
src/Symfony/Bundle/FrameworkBundle/Tests/Console/ApplicationTest.php

@@ -0,0 +1,35 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Console;
+
+use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\NullOutput;
+
+class ApplicationTest extends TestCase
+{
+    public function testBundleInterfaceImplementation()
+    {
+        $bundle = $this->getMock("Symfony\Component\HttpKernel\Bundle\BundleInterface");
+
+        $kernel = $this->getMock("Symfony\Component\HttpKernel\KernelInterface");
+        $kernel
+            ->expects($this->any())
+            ->method('getBundles')
+            ->will($this->returnValue(array($bundle)))
+        ;
+
+        $application = new Application($kernel);
+        $application->doRun(new ArrayInput(array('list')), new NullOutput());
+    }
+}