-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Base.php
372 lines (326 loc) · 11 KB
/
Base.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
/**
* Base router
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\App\Router;
use Magento\Framework\App\RequestInterface;
use Magento\Store\Model\ScopeInterface;
/**
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class Base implements \Magento\Framework\App\RouterInterface
{
/**
* @var \Magento\Framework\App\ActionFactory
*/
protected $actionFactory;
/**
* @var string
*/
protected $actionInterface = '\Magento\Framework\App\ActionInterface';
/**
* @var array
*/
protected $_modules = [];
/**
* @var array
*/
protected $_dispatchData = [];
/**
* List of required request parameters
* Order sensitive
* @var string[]
*/
protected $_requiredParams = ['moduleFrontName', 'actionPath', 'actionName'];
/**
* @var \Magento\Framework\App\Route\ConfigInterface
*/
protected $_routeConfig;
/**
* Url security information.
*
* @var \Magento\Framework\Url\SecurityInfoInterface
*/
protected $_urlSecurityInfo;
/**
* Core store config
*
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;
/**
* @var \Magento\Framework\UrlInterface
*/
protected $_url;
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
protected $_storeManager;
/**
* @var \Magento\Framework\App\ResponseFactory
*/
protected $_responseFactory;
/**
* @var \Magento\Framework\App\DefaultPathInterface
*/
protected $_defaultPath;
/**
* @var \Magento\Framework\Code\NameBuilder
*/
protected $nameBuilder;
/**
* @var array
*/
protected $reservedNames = ['new', 'print', 'switch', 'return'];
/**
* Allows to control if we need to enable no route functionality in current router
*
* @var bool
*/
protected $applyNoRoute = false;
/**
* @var string
*/
protected $pathPrefix = null;
/**
* @var \Magento\Framework\App\Router\ActionList
*/
protected $actionList;
/** @var PathConfigInterface */
protected $pathConfig;
/**
* @param \Magento\Framework\App\Router\ActionList $actionList
* @param \Magento\Framework\App\ActionFactory $actionFactory
* @param \Magento\Framework\App\DefaultPathInterface $defaultPath
* @param \Magento\Framework\App\ResponseFactory $responseFactory
* @param \Magento\Framework\App\Route\ConfigInterface $routeConfig
* @param \Magento\Framework\UrlInterface $url
* @param string $routerId
* @param \Magento\Framework\Code\NameBuilder $nameBuilder
* @param \Magento\Framework\App\Router\PathConfigInterface $pathConfig
*
* @throws \InvalidArgumentException
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function __construct(
\Magento\Framework\App\Router\ActionList $actionList,
\Magento\Framework\App\ActionFactory $actionFactory,
\Magento\Framework\App\DefaultPathInterface $defaultPath,
\Magento\Framework\App\ResponseFactory $responseFactory,
\Magento\Framework\App\Route\ConfigInterface $routeConfig,
\Magento\Framework\UrlInterface $url,
$routerId,
\Magento\Framework\Code\NameBuilder $nameBuilder,
\Magento\Framework\App\Router\PathConfigInterface $pathConfig
) {
$this->actionList = $actionList;
$this->actionFactory = $actionFactory;
$this->_responseFactory = $responseFactory;
$this->_defaultPath = $defaultPath;
$this->_routeConfig = $routeConfig;
$this->_url = $url;
$this->nameBuilder = $nameBuilder;
$this->pathConfig = $pathConfig;
}
/**
* Match provided request and if matched - return corresponding controller
*
* @param \Magento\Framework\App\RequestInterface $request
* @return \Magento\Framework\App\ActionInterface|null
*/
public function match(\Magento\Framework\App\RequestInterface $request)
{
$params = $this->parseRequest($request);
return $this->matchAction($request, $params);
}
/**
* Parse request URL params
*
* @param \Magento\Framework\App\RequestInterface $request
* @return array
*/
protected function parseRequest(\Magento\Framework\App\RequestInterface $request)
{
$output = [];
$path = trim($request->getPathInfo(), '/');
$params = explode('/', $path ? $path : $this->pathConfig->getDefaultPath());
foreach ($this->_requiredParams as $paramName) {
$output[$paramName] = array_shift($params);
}
for ($i = 0, $l = sizeof($params); $i < $l; $i += 2) {
$output['variables'][$params[$i]] = isset($params[$i + 1]) ? urldecode($params[$i + 1]) : '';
}
return $output;
}
/**
* Match module front name
*
* @param \Magento\Framework\App\RequestInterface $request
* @param string $param
* @return string|null
*/
protected function matchModuleFrontName(\Magento\Framework\App\RequestInterface $request, $param)
{
// get module name
if ($request->getModuleName()) {
$moduleFrontName = $request->getModuleName();
} elseif (!empty($param)) {
$moduleFrontName = $param;
} else {
$moduleFrontName = $this->_defaultPath->getPart('module');
$request->setAlias(\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS, '');
}
if (!$moduleFrontName) {
return null;
}
return $moduleFrontName;
}
/**
* Match controller name
*
* @param \Magento\Framework\App\RequestInterface $request
* @param string $param
* @return string
*/
protected function matchActionPath(\Magento\Framework\App\RequestInterface $request, $param)
{
if ($request->getControllerName()) {
$actionPath = $request->getControllerName();
} elseif (!empty($param)) {
$actionPath = $param;
} else {
$actionPath = $this->_defaultPath->getPart('controller');
$request->setAlias(
\Magento\Framework\Url::REWRITE_REQUEST_PATH_ALIAS,
ltrim($request->getOriginalPathInfo(), '/')
);
}
return $actionPath;
}
/**
* Get not found controller instance
*
* @param string $currentModuleName
* @return \Magento\Framework\App\ActionInterface|null
*/
protected function getNotFoundAction($currentModuleName)
{
if (!$this->applyNoRoute) {
return null;
}
$actionClassName = $this->getActionClassName($currentModuleName, 'noroute');
if (!$actionClassName || !is_subclass_of($actionClassName, $this->actionInterface)) {
return null;
}
// instantiate action class
return $this->actionFactory->create($actionClassName);
}
/**
* Create matched controller instance
*
* @param \Magento\Framework\App\RequestInterface $request
* @param array $params
* @return \Magento\Framework\App\ActionInterface|null
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
protected function matchAction(\Magento\Framework\App\RequestInterface $request, array $params)
{
$moduleFrontName = $this->matchModuleFrontName($request, $params['moduleFrontName']);
if (empty($moduleFrontName)) {
return null;
}
/**
* Searching router args by module name from route using it as key
*/
$modules = $this->_routeConfig->getModulesByFrontName($moduleFrontName);
if (empty($modules) === true) {
return null;
}
/**
* Going through modules to find appropriate controller
*/
$currentModuleName = null;
$actionPath = null;
$action = null;
$actionInstance = null;
$actionPath = $this->matchActionPath($request, $params['actionPath']);
$action = $request->getActionName() ?: ($params['actionName'] ?: $this->_defaultPath->getPart('action'));
$this->_checkShouldBeSecure($request, '/' . $moduleFrontName . '/' . $actionPath . '/' . $action);
foreach ($modules as $moduleName) {
$currentModuleName = $moduleName;
$actionClassName = $this->actionList->get($moduleName, $this->pathPrefix, $actionPath, $action);
if (!$actionClassName || !is_subclass_of($actionClassName, $this->actionInterface)) {
continue;
}
$actionInstance = $this->actionFactory->create($actionClassName);
break;
}
if (null == $actionInstance) {
$actionInstance = $this->getNotFoundAction($currentModuleName);
if ($actionInstance === null) {
return null;
}
$action = 'noroute';
}
// set values only after all the checks are done
$request->setModuleName($moduleFrontName);
$request->setControllerName($actionPath);
$request->setActionName($action);
$request->setControllerModule($currentModuleName);
$request->setRouteName($this->_routeConfig->getRouteByFrontName($moduleFrontName));
if (isset($params['variables'])) {
$request->setParams($params['variables']);
}
return $actionInstance;
}
/**
* Build controller class name
*
* @param string $module
* @param string $actionPath
* @return string
*/
public function getActionClassName($module, $actionPath)
{
$prefix = $this->pathPrefix ? 'Controller\\' . $this->pathPrefix : 'Controller';
return $this->nameBuilder->buildClassName([$module, $prefix, $actionPath]);
}
/**
* Check that request uses https protocol if it should.
* Function redirects user to correct URL if needed.
*
* @param \Magento\Framework\App\RequestInterface $request
* @param string $path
* @return void
* @SuppressWarnings(PHPMD.ExitExpression)
*/
protected function _checkShouldBeSecure(\Magento\Framework\App\RequestInterface $request, $path = '')
{
if ($request->getPostValue()) {
return;
}
if ($this->pathConfig->shouldBeSecure($path) && !$request->isSecure()) {
$url = $this->pathConfig->getCurrentSecureUrl($request);
if ($this->_shouldRedirectToSecure()) {
$url = $this->_url->getRedirectUrl($url);
}
$this->_responseFactory->create()->setRedirect($url)->sendResponse();
exit;
}
}
/**
* Check whether redirect url should be used for secure routes
*
* @return bool
*/
protected function _shouldRedirectToSecure()
{
return $this->_url->getUseSession();
}
}