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