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