Pārlūkot izejas kodu

[HttpKernel] updated Bundle::getName() to validate bundle class name and rtrim "Bundle"

Kris Wallsmith 14 gadi atpakaļ
vecāks
revīzija
dd9ad97a0c

+ 9 - 3
src/Symfony/Component/HttpKernel/Bundle/Bundle.php

@@ -115,6 +115,8 @@ abstract class Bundle extends ContainerAware implements BundleInterface
      * Returns the bundle name (the class short name).
      *
      * @return string The Bundle name
+     *
+     * @throws RuntimeException If the bundle class name does not end with "Bundle"
      */
     final public function getName()
     {
@@ -122,10 +124,14 @@ abstract class Bundle extends ContainerAware implements BundleInterface
             return $this->name;
         }
 
-        $name = get_class($this);
-        $pos = strrpos($name, '\\');
+        $fqcn = get_class($this);
+        $name = false === ($pos = strrpos($fqcn, '\\')) ? $fqcn : substr($fqcn, $pos + 1);
+
+        if ('Bundle' != substr($name, -6)) {
+            throw new \RuntimeException(sprintf('The bundle class name "%s" must end with "Bundle" to be valid.', $name));
+        }
 
-        return $this->name = false === $pos ? $name :  substr($name, $pos + 1);
+        return $this->name = substr($name, 0, -6);
     }
 
     /**

+ 17 - 5
tests/Symfony/Tests/Component/HttpKernel/Bundle/BundleTest.php

@@ -24,13 +24,25 @@ class BundleTest extends \PHPUnit_Framework_TestCase
         $cmd = new FooCommand();
         $app = $this->getMock('Symfony\Component\Console\Application');
         $app->expects($this->once())->method('add')->with($this->equalTo($cmd));
-        
+
         $bundle = new ExtensionPresentBundle();
         $bundle->registerCommands($app);
-        
+
         $bundle2 = new ExtensionAbsentBundle();
-        
+
         $this->assertNull($bundle2->registerCommands($app));
-        
     }
-}
+
+    public function testGetName()
+    {
+        $bundle = new ExtensionPresentBundle();
+        $this->assertEquals('ExtensionPresent', $bundle->getName(), '->getName() rtrims "Bundle"');
+    }
+
+    public function testInvalidBundleName()
+    {
+        $this->setExpectedException('RuntimeException');
+        $bundle = $this->getMockForAbstractClass('Symfony\\Component\\HttpKernel\\Bundle\\Bundle');
+        $bundle->getName();
+    }
+}