RoutesShell.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Shell;
  16. use Cake\Console\Shell;
  17. use Cake\Routing\Exception\MissingRouteException;
  18. use Cake\Routing\Router;
  19. /**
  20. * Provides interactive CLI tools for routing.
  21. */
  22. class RoutesShell extends Shell
  23. {
  24. /**
  25. * Override main() to handle action
  26. * Displays all routes in an application.
  27. *
  28. * @return void
  29. */
  30. public function main()
  31. {
  32. $output = [
  33. ['Route name', 'URI template', 'Defaults']
  34. ];
  35. foreach (Router::routes() as $route) {
  36. $name = isset($route->options['_name']) ? $route->options['_name'] : $route->getName();
  37. $output[] = [$name, $route->template, json_encode($route->defaults)];
  38. }
  39. $this->helper('table')->output($output);
  40. $this->out();
  41. }
  42. /**
  43. * Checks a url for the route that will be applied.
  44. *
  45. * @param string $url The URL to parse
  46. * @return bool Success
  47. */
  48. public function check($url)
  49. {
  50. try {
  51. $route = Router::parse($url);
  52. $name = null;
  53. foreach (Router::routes() as $r) {
  54. if ($r->match($route)) {
  55. $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
  56. break;
  57. }
  58. }
  59. unset($route['_matchedRoute']);
  60. $output = [
  61. ['Route name', 'URI template', 'Defaults'],
  62. [$name, $url, json_encode($route)]
  63. ];
  64. $this->helper('table')->output($output);
  65. $this->out();
  66. } catch (MissingRouteException $e) {
  67. $this->warn("'$url' did not match any routes.");
  68. $this->out();
  69. return false;
  70. }
  71. return true;
  72. }
  73. /**
  74. * Generate a URL based on a set of parameters
  75. *
  76. * Takes variadic arguments of key/value pairs.
  77. * @return bool Success
  78. */
  79. public function generate()
  80. {
  81. try {
  82. $args = $this->_splitArgs($this->args);
  83. $url = Router::url($args);
  84. $this->out("> $url");
  85. $this->out();
  86. } catch (MissingRouteException $e) {
  87. $this->err("<warning>The provided parameters do not match any routes.</warning>");
  88. $this->out();
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Get the option parser.
  95. *
  96. * @return \Cake\Console\ConsoleOptionParser
  97. */
  98. public function getOptionParser()
  99. {
  100. $parser = parent::getOptionParser();
  101. $parser->setDescription(
  102. 'Get the list of routes connected in this application. ' .
  103. 'This tool also lets you test URL generation and URL parsing.'
  104. )->addSubcommand('check', [
  105. 'help' => 'Check a URL string against the routes. ' .
  106. 'Will output the routing parameters the route resolves to.'
  107. ])->addSubcommand('generate', [
  108. 'help' => 'Check a routing array against the routes. ' .
  109. "Will output the URL if there is a match.\n\n" .
  110. "Routing parameters should be supplied in a key:value format. " .
  111. "For example `controller:Articles action:view 2`"
  112. ]);
  113. return $parser;
  114. }
  115. /**
  116. * Split the CLI arguments into a hash.
  117. *
  118. * @param array $args The arguments to split.
  119. * @return array
  120. */
  121. protected function _splitArgs($args)
  122. {
  123. $out = [];
  124. foreach ($args as $arg) {
  125. if (strpos($arg, ':') !== false) {
  126. list($key, $value) = explode(':', $arg);
  127. if (in_array($value, ['true', 'false'])) {
  128. $value = $value === 'true';
  129. }
  130. $out[$key] = $value;
  131. } else {
  132. $out[] = $arg;
  133. }
  134. }
  135. return $out;
  136. }
  137. }