Explorar o código

[HttpKernel] added back Bundle::getName() as it is quite useful

Fabien Potencier %!s(int64=14) %!d(string=hai) anos
pai
achega
e6f1248151

+ 18 - 1
src/Symfony/Component/HttpKernel/Bundle/Bundle.php

@@ -13,7 +13,6 @@ namespace Symfony\Component\HttpKernel\Bundle;
 
 
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\DependencyInjection\ContainerAware;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
-use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
 use Symfony\Component\Console\Application;
 use Symfony\Component\Console\Application;
 use Symfony\Component\Finder\Finder;
 use Symfony\Component\Finder\Finder;
 
 
@@ -25,6 +24,8 @@ use Symfony\Component\Finder\Finder;
  */
  */
 abstract class Bundle extends ContainerAware implements BundleInterface
 abstract class Bundle extends ContainerAware implements BundleInterface
 {
 {
+    protected $name;
+
     /**
     /**
      * Boots the Bundle.
      * Boots the Bundle.
      */
      */
@@ -49,6 +50,22 @@ abstract class Bundle extends ContainerAware implements BundleInterface
         return null;
         return null;
     }
     }
 
 
+    /**
+     * Returns the bundle name (the class short name).
+     *
+     * @return string The Bundle name
+     */
+    final public function getName()
+    {
+        if (null !== $this->name) {
+            return $this->name;
+        }
+
+        $pos = strrpos(get_class($this), '\\');
+
+        return $this->name = substr(get_class($this), $pos ? $pos + 1 : 0);
+    }
+
     /**
     /**
      * Finds and registers Dependency Injection Container extensions.
      * Finds and registers Dependency Injection Container extensions.
      *
      *

+ 7 - 0
src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php

@@ -35,6 +35,13 @@ interface BundleInterface
      */
      */
     function getParent();
     function getParent();
 
 
+    /**
+     * Returns the bundle name (the class short name).
+     *
+     * @return string The Bundle name
+     */
+    function getName();
+
     /**
     /**
      * Gets the Bundle namespace.
      * Gets the Bundle namespace.
      *
      *

+ 1 - 2
src/Symfony/Component/HttpKernel/Kernel.php

@@ -410,8 +410,7 @@ abstract class Kernel implements HttpKernelInterface, \Serializable
         // init bundles
         // init bundles
         $this->bundles = array();
         $this->bundles = array();
         foreach ($this->registerBundles() as $bundle) {
         foreach ($this->registerBundles() as $bundle) {
-            $parts = explode('\\', get_class($bundle));
-            $name = $parts[count($parts) - 1];
+            $name = $bundle->getName();
             $this->bundles[$name] = $bundle;
             $this->bundles[$name] = $bundle;
             if (!isset($this->bundleMap[$name])) {
             if (!isset($this->bundleMap[$name])) {
                 $this->bundleMap[$name] = array();
                 $this->bundleMap[$name] = array();

+ 12 - 1
tests/Symfony/Tests/Component/HttpKernel/KernelTest.php

@@ -145,6 +145,7 @@ class KernelTest extends \PHPUnit_Framework_TestCase
             ->method('registerBundles')
             ->method('registerBundles')
             ->will($this->returnValue(array($parent, $grandparent, $child)))
             ->will($this->returnValue(array($parent, $grandparent, $child)))
         ;
         ;
+
         $kernel->initializeBundles();
         $kernel->initializeBundles();
 
 
         $map = $kernel->getBundleMap();
         $map = $kernel->getBundleMap();
@@ -189,7 +190,11 @@ class KernelTest extends \PHPUnit_Framework_TestCase
 
 
     protected function getBundle($dir = null, $parent = null, $className = null)
     protected function getBundle($dir = null, $parent = null, $className = null)
     {
     {
-        $bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface');
+        $bundle = $this
+            ->getMockBuilder('Symfony\Tests\Component\HttpKernel\KernelForTest')
+            ->setMethods(array('getPath', 'getParent', 'getName'))
+            ->disableOriginalConstructor()
+        ;
 
 
         if ($className) {
         if ($className) {
             $bundle->setMockClassName($className);
             $bundle->setMockClassName($className);
@@ -197,6 +202,12 @@ class KernelTest extends \PHPUnit_Framework_TestCase
 
 
         $bundle = $bundle->getMock();
         $bundle = $bundle->getMock();
 
 
+        $bundle
+            ->expects($this->any())
+            ->method('getName')
+            ->will($this->returnValue(get_class($bundle)))
+        ;
+
         if (null !== $dir) {
         if (null !== $dir) {
             $bundle
             $bundle
                 ->expects($this->any())
                 ->expects($this->any())