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