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