new method to create shorthand name from full Controller name

This commit is contained in:
Bart van den Burg 2012-10-29 14:43:13 +01:00 committed by Fabien Potencier
parent 6a18bfc130
commit 91ebba420a
2 changed files with 52 additions and 0 deletions

View File

@ -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));
}
}

View File

@ -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);
}