1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\Application\Routers;
9:
10: use Nette;
11: use Nette\Application;
12:
13:
14: 15: 16: 17: 18:
19: class SimpleRouter extends Nette\Object implements Application\IRouter
20: {
21: const PRESENTER_KEY = 'presenter';
22: const MODULE_KEY = 'module';
23:
24:
25: private $module = '';
26:
27:
28: private $defaults;
29:
30:
31: private $flags;
32:
33:
34: 35: 36: 37:
38: public function __construct($defaults = array(), $flags = 0)
39: {
40: if (is_string($defaults)) {
41: $a = strrpos($defaults, ':');
42: if (!$a) {
43: throw new Nette\InvalidArgumentException("Argument must be array or string in format Presenter:action, '$defaults' given.");
44: }
45: $defaults = array(
46: self::PRESENTER_KEY => substr($defaults, 0, $a),
47: 'action' => $a === strlen($defaults) - 1 ? Application\UI\Presenter::DEFAULT_ACTION : substr($defaults, $a + 1),
48: );
49: }
50:
51: if (isset($defaults[self::MODULE_KEY])) {
52: $this->module = $defaults[self::MODULE_KEY] . ':';
53: unset($defaults[self::MODULE_KEY]);
54: }
55:
56: $this->defaults = $defaults;
57: $this->flags = $flags;
58: }
59:
60:
61: 62: 63: 64:
65: public function match(Nette\Http\IRequest $httpRequest)
66: {
67: if ($httpRequest->getUrl()->getPathInfo() !== '') {
68: return NULL;
69: }
70:
71: $params = $httpRequest->getQuery();
72: $params += $this->defaults;
73:
74: if (!isset($params[self::PRESENTER_KEY]) || !is_string($params[self::PRESENTER_KEY])) {
75: return NULL;
76: }
77:
78: $presenter = $this->module . $params[self::PRESENTER_KEY];
79: unset($params[self::PRESENTER_KEY]);
80:
81: return new Application\Request(
82: $presenter,
83: $httpRequest->getMethod(),
84: $params,
85: $httpRequest->getPost(),
86: $httpRequest->getFiles(),
87: array(Application\Request::SECURED => $httpRequest->isSecured())
88: );
89: }
90:
91:
92: 93: 94: 95:
96: public function constructUrl(Application\Request $appRequest, Nette\Http\Url $refUrl)
97: {
98: if ($this->flags & self::ONE_WAY) {
99: return NULL;
100: }
101: $params = $appRequest->getParameters();
102:
103:
104: $presenter = $appRequest->getPresenterName();
105: if (strncasecmp($presenter, $this->module, strlen($this->module)) === 0) {
106: $params[self::PRESENTER_KEY] = substr($presenter, strlen($this->module));
107: } else {
108: return NULL;
109: }
110:
111:
112: foreach ($this->defaults as $key => $value) {
113: if (isset($params[$key]) && $params[$key] == $value) {
114: unset($params[$key]);
115: }
116: }
117:
118: $url = ($this->flags & self::SECURED ? 'https://' : 'http://') . $refUrl->getAuthority() . $refUrl->getPath();
119: $sep = ini_get('arg_separator.input');
120: $query = http_build_query($params, '', $sep ? $sep[0] : '&');
121: if ($query != '') {
122: $url .= '?' . $query;
123: }
124: return $url;
125: }
126:
127:
128: 129: 130: 131:
132: public function getDefaults()
133: {
134: return $this->defaults;
135: }
136:
137:
138: 139: 140: 141:
142: public function getFlags()
143: {
144: return $this->flags;
145: }
146:
147: }
148: