diff --git a/src/Shell/RoutesShell.php b/src/Shell/RoutesShell.php
index daf5d71f319..e88e0543cb8 100644
--- a/src/Shell/RoutesShell.php
+++ b/src/Shell/RoutesShell.php
@@ -36,7 +36,7 @@ public function main()
{
$output = [];
foreach (Router::routes() as $route) {
- $output[] = [$route->getName(), $route->template, $this->_stringifyDefaults($route->defaults)];
+ $output[] = [$route->getName(), $route->template, json_encode($route->defaults)];
}
$this->_outWithColumns($output);
@@ -52,7 +52,7 @@ public function check($url)
{
try {
$route = Router::parse($url);
- $this->_outWithColumns(['', $url, $this->_stringifyDefaults($route)]);
+ $this->_outWithColumns(['', $url, json_encode($route)]);
} catch (MissingRouteException $e) {
$this->err("'$url' did not match any routes.");
return false;
@@ -77,6 +77,24 @@ public function generate()
}
}
+ public function getOptionParser()
+ {
+ $parser = parent::getOptionParser();
+ $parser->description(
+ 'Get the list of routes connected in this application. ' .
+ 'This tool also lets you test URL generation and URL parsing.'
+ )->addSubcommand('check', [
+ 'help' => 'Check a URL string against the routes. ' .
+ 'Will output the routing parameters the route resolves to.'
+ ])->addSubcommand('generate', [
+ 'help' => 'Check a routing array agains the routes. ' .
+ "Will output the URL if there is a match.\n\n" .
+ "Routing parameters should be supplied in a key:value format. " .
+ "For example `controller:Articles action:view 2`"
+ ]);
+ return $parser;
+ }
+
/**
* Split the CLI arguments into a hash.
*
@@ -87,12 +105,12 @@ protected function _splitArgs($args)
{
$out = [];
foreach ($args as $arg) {
- if (strpos($arg, ':') === false) {
- $this->err("The '$arg' is malformed. It should be formated like `key:value`.");
- continue;
+ if (strpos($arg, ':') !== false) {
+ list($key, $value) = explode(':', $arg);
+ $out[$key] = $value;
+ } else {
+ $out[] = $arg;
}
- list($key, $value) = explode(':', $arg);
- $out[$key] = $value;
}
return $out;
}
@@ -129,31 +147,4 @@ protected function _outWithColumns($rows)
$this->out();
}
-
- /**
- * Get defaults from the route object as a string
- *
- * @param array $defaults The defaults to use for creating a route array.
- * @return string
- */
- protected function _stringifyDefaults($defaults)
- {
- $results = [];
- if (!empty($defaults['controller'])) {
- $results['controller'] = $defaults['controller'];
- }
- if (!empty($defaults['action'])) {
- $results['action'] = $defaults['action'];
- }
- if (!empty($defaults[0])) {
- $pass = [];
- $i = 0;
- while (!empty($defaults[$i])) {
- $pass[$i] = $defaults[$i];
- $i++;
- }
- $results['pass'] = $pass;
- }
- return json_encode($results);
- }
}
diff --git a/tests/TestCase/Shell/RoutesShellTest.php b/tests/TestCase/Shell/RoutesShellTest.php
index d8106ad0c8c..2676ce0ce3f 100644
--- a/tests/TestCase/Shell/RoutesShellTest.php
+++ b/tests/TestCase/Shell/RoutesShellTest.php
@@ -38,7 +38,7 @@ public function setUp()
$this->io = $this->getMock('Cake\Console\ConsoleIo');
$this->shell = new RoutesShell($this->io);
- Router::connect('/articles/:action', ['controller' => 'Articles']);
+ Router::connect('/articles/:action/*', ['controller' => 'Articles']);
Router::connect('/bake/:controller/:action', ['plugin' => 'Bake']);
}
@@ -73,7 +73,7 @@ public function testMain()
->with($this->logicalAnd(
$this->stringContains('articles:_action'),
$this->stringContains('/articles/:action'),
- $this->stringContains('{"controller":"Articles",')
+ $this->stringContains('"controller":"Articles",')
));
$this->shell->main();
}
@@ -89,7 +89,7 @@ public function testCheck()
->method('out')
->with($this->logicalAnd(
$this->stringContains('/articles/index'),
- $this->stringContains('{"controller":"Articles",')
+ $this->stringContains('"controller":"Articles",')
));
$this->shell->check('/articles/index');
}
@@ -114,11 +114,20 @@ public function testCheckNotFound()
*/
public function testGenerate()
{
+ $this->io->expects($this->never())
+ ->method('err');
$this->io->expects($this->at(0))
->method('out')
->with($this->stringContains('> /articles/index'));
+ $this->io->expects($this->at(1))
+ ->method('out')
+ ->with($this->stringContains('> /articles/view/2/3'));
+
$this->shell->args = ['controller:Articles', 'action:index'];
$this->shell->generate();
+
+ $this->shell->args = ['controller:Articles', 'action:view', '2', '3'];
+ $this->shell->generate();
}
/**