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