1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Application\Routers;
9:
10: use Nette;
11: use Nette\Application;
12:
13:
14: 15: 16:
17: class CliRouter extends Nette\Object implements Application\IRouter
18: {
19: const PRESENTER_KEY = 'action';
20:
21:
22: private $defaults;
23:
24:
25: 26: 27:
28: public function __construct($defaults = array())
29: {
30: $this->defaults = $defaults;
31: }
32:
33:
34: 35: 36: 37:
38: public function match(Nette\Http\IRequest $httpRequest)
39: {
40: if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
41: return NULL;
42: }
43:
44: $names = array(self::PRESENTER_KEY);
45: $params = $this->defaults;
46: $args = $_SERVER['argv'];
47: array_shift($args);
48: $args[] = '--';
49:
50: foreach ($args as $arg) {
51: $opt = preg_replace('#/|-+#A', '', $arg);
52: if ($opt === $arg) {
53: if (isset($flag) || $flag = array_shift($names)) {
54: $params[$flag] = $arg;
55: } else {
56: $params[] = $arg;
57: }
58: $flag = NULL;
59: continue;
60: }
61:
62: if (isset($flag)) {
63: $params[$flag] = TRUE;
64: $flag = NULL;
65: }
66:
67: if ($opt !== '') {
68: $pair = explode('=', $opt, 2);
69: if (isset($pair[1])) {
70: $params[$pair[0]] = $pair[1];
71: } else {
72: $flag = $pair[0];
73: }
74: }
75: }
76:
77: if (!isset($params[self::PRESENTER_KEY])) {
78: throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
79: }
80: $presenter = $params[self::PRESENTER_KEY];
81: if ($a = strrpos($presenter, ':')) {
82: $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
83: $presenter = substr($presenter, 0, $a);
84: }
85:
86: return new Application\Request(
87: $presenter,
88: 'CLI',
89: $params
90: );
91: }
92:
93:
94: 95: 96: 97:
98: public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
99: {
100: return NULL;
101: }
102:
103:
104: 105: 106: 107:
108: public function getDefaults()
109: {
110: return $this->defaults;
111: }
112:
113: }
114: