-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathRouteParamsResolver.php
167 lines (150 loc) · 4.72 KB
/
RouteParamsResolver.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Url;
use Magento\Framework\Url\RouteParamsResolverInterface;
/**
* Route params resolver.
*
* @method $this setType(string $type)
* @method string getType()
* @method $this setScope(string $scope)
* @method string getScope()
* @method $this setSecureIsForced(bool $isForced)
* @method bool getSecureIsForced()
* @method $this setSecure(bool $isForced)
* @method bool getSecure()
*/
class RouteParamsResolver extends \Magento\Framework\DataObject implements RouteParamsResolverInterface
{
/**
* @var \Magento\Framework\App\RequestInterface
*/
protected $request;
/**
* @var \Magento\Framework\Url\QueryParamsResolverInterface
*/
protected $queryParamsResolver;
/**
* @var \Magento\Framework\Escaper
*/
protected $escaper;
/**
* @param \Magento\Framework\App\RequestInterface $request
* @param \Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver
* @param array $data
*/
public function __construct(
\Magento\Framework\App\RequestInterface $request,
\Magento\Framework\Url\QueryParamsResolverInterface $queryParamsResolver,
array $data = []
) {
parent::__construct($data);
$this->request = $request;
$this->queryParamsResolver = $queryParamsResolver;
}
/**
* {@inheritdoc}
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function setRouteParams(array $data, $unsetOldParams = true)
{
if (isset($data['_type'])) {
$this->setType($data['_type']);
unset($data['_type']);
}
if (isset($data['_forced_secure'])) {
$this->setSecure((bool)$data['_forced_secure']);
$this->setSecureIsForced(true);
unset($data['_forced_secure']);
} elseif (isset($data['_secure'])) {
$this->setSecure((bool)$data['_secure']);
unset($data['_secure']);
}
if (isset($data['_absolute'])) {
unset($data['_absolute']);
}
if ($unsetOldParams) {
$this->unsetData('route_params');
}
if (isset($data['_current'])) {
if (is_array($data['_current'])) {
foreach ($data['_current'] as $key) {
if (array_key_exists($key, $data) || !$this->request->getUserParam($key)) {
continue;
}
$data[$key] = $this->request->getUserParam($key);
}
} elseif ($data['_current']) {
foreach ($this->request->getUserParams() as $key => $value) {
if (array_key_exists($key, $data) || $this->getRouteParam($key)) {
continue;
}
$data[$key] = $value;
}
foreach ($this->request->getQuery() as $key => $value) {
$this->queryParamsResolver->setQueryParam($key, $value);
}
}
unset($data['_current']);
}
if (isset($data['_use_rewrite'])) {
unset($data['_use_rewrite']);
}
foreach ($data as $key => $value) {
if (!is_scalar($value) || $key == 'key' || !$this->getData('escape_params')) {
$this->setRouteParam($key, $value);
} else {
$this->setRouteParam(
$this->getEscaper()->encodeUrlParam($key),
$this->getEscaper()->encodeUrlParam($value)
);
}
}
return $this;
}
/**
* {@inheritdoc}
*/
public function setRouteParam($key, $data)
{
$params = $this->_getData('route_params');
if (isset($params[$key]) && $params[$key] == $data) {
return $this;
}
$params[$key] = $data;
$this->unsetData('route_path');
return $this->setData('route_params', $params);
}
/**
* {@inheritdoc}
*/
public function getRouteParams()
{
return $this->_getData('route_params');
}
/**
* {@inheritdoc}
*/
public function getRouteParam($key)
{
return $this->getData('route_params', $key);
}
/**
* Get escaper
*
* @return \Magento\Framework\Escaper
* @deprecated 101.0.0
*/
private function getEscaper()
{
if ($this->escaper == null) {
$this->escaper = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Escaper::class);
}
return $this->escaper;
}
}