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