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