From 91ebba420a2b399304e0ebf4f3187ea47c9370eb Mon Sep 17 00:00:00 2001 From: Bart van den Burg Date: Mon, 29 Oct 2012 14:43:13 +0100 Subject: [PATCH] new method to create shorthand name from full Controller name --- .../Controller/ControllerNameParser.php | 22 ++++++++++++++ .../Controller/ControllerNameParserTest.php | 30 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php index 667ff587de..987356124e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php +++ b/src/Symfony/Bundle/FrameworkBundle/Controller/ControllerNameParser.php @@ -71,4 +71,26 @@ class ControllerNameParser throw new \InvalidArgumentException($msg); } + + public function build($controller) { + if (2 != count($parts = explode('::', $controller))) { + throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid aController::cAction controller string.', $controller)); + } + + list ($className, $action) = $parts; + if (5 !== strripos($action, 'Action')) { + throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid aController::cAction controller string.', $controller)); + } + $action = substr($action, 0, strlen($action) - 6); + + foreach ($this->kernel->getBundles() as $bundles) { + foreach($bundles as $bundle) { + if (preg_match('/^'.preg_quote($bundle->getNamespace()).'\\\\Controller\\\\(.*)Controller/', $className, $m)) { + return sprintf('%s:%s:%s', $bundle->getName(), $m[1], $action); + } + } + } + + throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid aController::cAction controller string.', $controller)); + } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php index 3f1dc249d2..ac20e601f7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerNameParserTest.php @@ -55,6 +55,31 @@ class ControllerNameParserTest extends TestCase } } + public function testBuild() + { + $parser = $this->createParser(); + + $this->assertEquals('FooBundle:Default:index', $parser->build('TestBundle\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); + $this->assertEquals('FooBundle:Sub\Default:index', $parser->build('TestBundle\FooBundle\Controller\Sub\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); + $this->assertEquals('FabpotFooBundle:Default:index', $parser->build('TestBundle\Fabpot\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); + $this->assertEquals('SensioCmsFooBundle:Default:index', $parser->build('TestBundle\Sensio\Cms\FooBundle\Controller\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); + $this->assertEquals('FooBundle:Test\\Default:index', $parser->build('TestBundle\FooBundle\Controller\Test\DefaultController::indexAction'), '->parse() converts a class::method string to a short a:b:c notation string'); + + try { + $parser->build('TestBundle\FooBundle\Controller\DefaultController::index'); + $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string'); + } catch (\Exception $e) { + $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string'); + } + + try { + $parser->build('TestBundle\FooBundle\Controller\Default::indexAction'); + $this->fail('->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string'); + } catch (\Exception $e) { + $this->assertInstanceOf('\InvalidArgumentException', $e, '->parse() throws an \InvalidArgumentException if the controller is not an aController::cAction string'); + } + } + /** * @dataProvider getMissingControllersTest */ @@ -95,6 +120,11 @@ class ControllerNameParserTest extends TestCase return $bundles[$bundle]; })) ; + $kernel + ->expects($this->any()) + ->method('getBundles') + ->will($this->returnValue($bundles)) + ; return new ControllerNameParser($kernel); }