1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Shell;
16:
17: use Cake\Console\Shell;
18: use Cake\Http\ServerRequest;
19: use Cake\Routing\Exception\MissingRouteException;
20: use Cake\Routing\Router;
21:
22: 23: 24:
25: class RoutesShell extends Shell
26: {
27: 28: 29: 30: 31: 32:
33: public function main()
34: {
35: $output = [
36: ['Route name', 'URI template', 'Defaults']
37: ];
38: foreach (Router::routes() as $route) {
39: $name = isset($route->options['_name']) ? $route->options['_name'] : $route->getName();
40: ksort($route->defaults);
41: $output[] = [$name, $route->template, json_encode($route->defaults)];
42: }
43: $this->helper('table')->output($output);
44: $this->out();
45: }
46:
47: 48: 49: 50: 51: 52:
53: public function check($url)
54: {
55: try {
56: $request = new ServerRequest(['url' => $url]);
57: $route = Router::parseRequest($request);
58: $name = null;
59: foreach (Router::routes() as $r) {
60: if ($r->match($route)) {
61: $name = isset($r->options['_name']) ? $r->options['_name'] : $r->getName();
62: break;
63: }
64: }
65:
66: unset($route['_matchedRoute']);
67: ksort($route);
68:
69: $output = [
70: ['Route name', 'URI template', 'Defaults'],
71: [$name, $url, json_encode($route)]
72: ];
73: $this->helper('table')->output($output);
74: $this->out();
75: } catch (MissingRouteException $e) {
76: $this->warn("'$url' did not match any routes.");
77: $this->out();
78:
79: return false;
80: }
81:
82: return true;
83: }
84:
85: 86: 87: 88: 89: 90:
91: public function generate()
92: {
93: try {
94: $args = $this->_splitArgs($this->args);
95: $url = Router::url($args);
96: $this->out("> $url");
97: $this->out();
98: } catch (MissingRouteException $e) {
99: $this->err('<warning>The provided parameters do not match any routes.</warning>');
100: $this->out();
101:
102: return false;
103: }
104:
105: return true;
106: }
107:
108: 109: 110: 111: 112:
113: public function getOptionParser()
114: {
115: $parser = parent::getOptionParser();
116: $parser->setDescription(
117: 'Get the list of routes connected in this application. ' .
118: 'This tool also lets you test URL generation and URL parsing.'
119: )->addSubcommand('check', [
120: 'help' => 'Check a URL string against the routes. ' .
121: 'Will output the routing parameters the route resolves to.'
122: ])->addSubcommand('generate', [
123: 'help' => 'Check a routing array against the routes. ' .
124: "Will output the URL if there is a match.\n\n" .
125: 'Routing parameters should be supplied in a key:value format. ' .
126: 'For example `controller:Articles action:view 2`'
127: ]);
128:
129: return $parser;
130: }
131:
132: 133: 134: 135: 136: 137:
138: protected function _splitArgs($args)
139: {
140: $out = [];
141: foreach ($args as $arg) {
142: if (strpos($arg, ':') !== false) {
143: list($key, $value) = explode(':', $arg);
144: if (in_array($value, ['true', 'false'])) {
145: $value = $value === 'true';
146: }
147: $out[$key] = $value;
148: } else {
149: $out[] = $arg;
150: }
151: }
152:
153: return $out;
154: }
155: }
156: