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