merged branch fabpot/create-controller-names (PR #7843)
This PR was merged into the master branch. Discussion ---------- Add a way to create a short controller name (a🅱️c) from a fully qualify one (controller::action) | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #5865 | License | MIT | Doc PR | n/a Working implementation of #5865 Commits -------0795ea8
[FrameworkBundle] fixed implementation of previous merge (refs #5865)91ebba4
new method to create shorthand name from full Controller name
This commit is contained in:
commit
e04ed3b9c2
|
@ -4,6 +4,7 @@ CHANGELOG
|
|||
2.3.0
|
||||
-----
|
||||
|
||||
* added ControllerNameParser::build() to converts a controller short notation (a:b:c) to a class::method notation
|
||||
* added possibility to run PHP built-in server in production environment
|
||||
* added possibility to load the serializer component in the service container
|
||||
* added route debug information when using the `router:match` command
|
||||
|
|
|
@ -39,7 +39,7 @@ class ControllerNameParser
|
|||
*
|
||||
* @param string $controller A short notation controller (a:b:c)
|
||||
*
|
||||
* @return string A string with class::method
|
||||
* @return string A string in the class::method notation
|
||||
*
|
||||
* @throws \InvalidArgumentException when the specified bundle is not enabled
|
||||
* or the controller cannot be found
|
||||
|
@ -47,7 +47,7 @@ class ControllerNameParser
|
|||
public function parse($controller)
|
||||
{
|
||||
if (3 != count($parts = explode(':', $controller))) {
|
||||
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
|
||||
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "a:b:c" controller string.', $controller));
|
||||
}
|
||||
|
||||
list($bundle, $controller, $action) = $parts;
|
||||
|
@ -71,4 +71,33 @@ class ControllerNameParser
|
|||
|
||||
throw new \InvalidArgumentException($msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a class::method notation to a short one (a:b:c).
|
||||
*
|
||||
* @param string $controller A string in the class::method notation
|
||||
*
|
||||
* @return string A short notation controller (a:b:c)
|
||||
*
|
||||
* @throws \InvalidArgumentException when the controller is not valid or cannot be found in any bundle
|
||||
*/
|
||||
public function build($controller)
|
||||
{
|
||||
if (0 === preg_match('#^(.*?\\\\Controller\\\\(.+)Controller)::(.+)Action$#', $controller, $match)) {
|
||||
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid "class::method" string.', $controller));
|
||||
}
|
||||
|
||||
$className = $match[1];
|
||||
$controllerName = $match[2];
|
||||
$actionName = $match[3];
|
||||
foreach ($this->kernel->getBundles() as $name => $bundle) {
|
||||
if (0 !== strpos($className, $bundle->getNamespace())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
return sprintf('%s:%s:%s', $name, $controllerName, $actionName);
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException(sprintf('Unable to find a bundle that defines controller "%s".', $controller));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -55,6 +55,35 @@ 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');
|
||||
|
||||
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');
|
||||
}
|
||||
|
||||
try {
|
||||
$parser->build('Foo\Controller\DefaultController::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
|
||||
*/
|
||||
|
@ -96,6 +125,18 @@ class ControllerNameParserTest extends TestCase
|
|||
}))
|
||||
;
|
||||
|
||||
$bundles = array(
|
||||
'SensioFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
|
||||
'SensioCmsFooBundle' => $this->getBundle('TestBundle\Sensio\Cms\FooBundle', 'SensioCmsFooBundle'),
|
||||
'FooBundle' => $this->getBundle('TestBundle\FooBundle', 'FooBundle'),
|
||||
'FabpotFooBundle' => $this->getBundle('TestBundle\Fabpot\FooBundle', 'FabpotFooBundle'),
|
||||
);
|
||||
$kernel
|
||||
->expects($this->any())
|
||||
->method('getBundles')
|
||||
->will($this->returnValue($bundles))
|
||||
;
|
||||
|
||||
return new ControllerNameParser($kernel);
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue