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: public function match(Nette\Http\IRequest $httpRequest)
50: {
51: if (empty($_SERVER['argv']) || !is_array($_SERVER['argv'])) {
52: return NULL;
53: }
54:
55: $names = array(self::PRESENTER_KEY);
56: $params = $this->defaults;
57: $args = $_SERVER['argv'];
58: array_shift($args);
59: $args[] = '--';
60:
61: foreach ($args as $arg) {
62: $opt = preg_replace('#/|-+#A', '', $arg);
63: if ($opt === $arg) {
64: if (isset($flag) || $flag = array_shift($names)) {
65: $params[$flag] = $arg;
66: } else {
67: $params[] = $arg;
68: }
69: $flag = NULL;
70: continue;
71: }
72:
73: if (isset($flag)) {
74: $params[$flag] = TRUE;
75: $flag = NULL;
76: }
77:
78: if ($opt !== '') {
79: $pair = explode('=', $opt, 2);
80: if (isset($pair[1])) {
81: $params[$pair[0]] = $pair[1];
82: } else {
83: $flag = $pair[0];
84: }
85: }
86: }
87:
88: if (!isset($params[self::PRESENTER_KEY])) {
89: throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
90: }
91: $presenter = $params[self::PRESENTER_KEY];
92: if ($a = strrpos($presenter, ':')) {
93: $params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
94: $presenter = substr($presenter, 0, $a);
95: }
96:
97: return new Application\Request(
98: $presenter,
99: 'CLI',
100: $params
101: );
102: }
103:
104:
105:
106: 107: 108: 109:
110: public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
111: {
112: return NULL;
113: }
114:
115:
116:
117: 118: 119: 120:
121: public function getDefaults()
122: {
123: return $this->defaults;
124: }
125:
126: }
127: