7
7
*/
8
8
namespace Magento \Webapi \Controller \Rest \Router ;
9
9
10
- class Route extends \Zend_Controller_Router_Route
10
+ use Magento \Framework \App \RequestInterface as Request ;
11
+ use Magento \Framework \App \RouterInterface ;
12
+
13
+ class Route implements RouterInterface
11
14
{
12
- /** @var string */
13
- protected $ _serviceClass ;
15
+ /**
16
+ * @var string
17
+ */
18
+ protected $ serviceClass ;
19
+
20
+ /**
21
+ * @var string
22
+ */
23
+ protected $ serviceMethod ;
24
+
25
+ /**
26
+ * @var boolean
27
+ */
28
+ protected $ secure ;
14
29
15
- /** @var string */
16
- protected $ _serviceMethod ;
30
+ /**
31
+ * @var array
32
+ */
33
+ protected $ aclResources = [];
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ protected $ parameters = [];
17
39
18
- /** @var boolean */
19
- protected $ _secure ;
40
+ /**
41
+ * @var array
42
+ */
43
+ protected $ variables = [];
20
44
21
- /** @var array */
22
- protected $ _aclResources = [];
45
+ /**
46
+ * @var string
47
+ */
48
+ protected $ route ;
23
49
24
- /** @var array */
25
- protected $ _parameters = [];
50
+ /**
51
+ * @param string $route
52
+ */
53
+ public function __construct ($ route = '' )
54
+ {
55
+ $ this ->route = trim ($ route , '/ ' );
56
+ }
57
+
58
+ /**
59
+ * Split route by parts and variables
60
+ *
61
+ * @return array
62
+ */
63
+ protected function getRouteParts ()
64
+ {
65
+ $ result = [];
66
+ $ routeParts = explode ('/ ' , $ this ->route );
67
+ foreach ($ routeParts as $ key => $ value ) {
68
+ if ($ this ->isVariable ($ value )) {
69
+ $ this ->variables [$ key ] = substr ($ value , 1 );
70
+ $ value = null ;
71
+ }
72
+ $ result [$ key ] = strtolower ($ value );
73
+ }
74
+ return $ result ;
75
+ }
76
+
77
+ /**
78
+ * Check if current route part is a name of variable
79
+ *
80
+ * @param string $value
81
+ * @return bool
82
+ */
83
+ protected function isVariable ($ value )
84
+ {
85
+ if (substr ($ value , 0 , 1 ) == ': '
86
+ && substr ($ value , 1 , 1 ) != ': ' ) {
87
+ return true ;
88
+ }
89
+ return false ;
90
+ }
91
+
92
+ /**
93
+ * Retrieve unified requested path
94
+ *
95
+ * Lowercase all path chunks, except variables names.
96
+ * E.g. the path '/V1/Categories/:categoryId' will be converted to '/v1/categories/:categoryId'.
97
+ *
98
+ * @param string $path
99
+ * @return array
100
+ */
101
+ protected function getPathParts ($ path )
102
+ {
103
+ $ result = explode ('/ ' , trim ($ path , '/ ' ));
104
+ array_walk ($ result , function (&$ item ) {
105
+ $ item = substr ($ item , 0 , 1 ) === ": " ? $ item : strtolower ($ item );
106
+ });
107
+ return $ result ;
108
+ }
109
+
110
+ /**
111
+ * Check if current route matches the requested path
112
+ *
113
+ * @param Request $request
114
+ * @return array|bool
115
+ */
116
+ public function match (Request $ request )
117
+ {
118
+ /** @var \Magento\Webapi\Controller\Rest\Request $request */
119
+ $ pathParts = $ this ->getPathParts ($ request ->getPathInfo ());
120
+ $ routeParts = $ this ->getRouteParts ();
121
+ if (count ($ pathParts ) <> count ($ routeParts )) {
122
+ return false ;
123
+ }
124
+
125
+ $ result = [];
126
+ foreach ($ pathParts as $ key => $ value ) {
127
+ if (!array_key_exists ($ key , $ routeParts )) {
128
+ return false ;
129
+ }
130
+ $ variable = isset ($ this ->variables [$ key ]) ? $ this ->variables [$ key ] : null ;
131
+ if ($ variable ) {
132
+ $ result [$ variable ] = urldecode ($ pathParts [$ key ]);
133
+ } else {
134
+ if ($ value != $ routeParts [$ key ]) {
135
+ return false ;
136
+ }
137
+ }
138
+ }
139
+ return $ result ;
140
+ }
26
141
27
142
/**
28
143
* Set service class.
@@ -32,7 +147,7 @@ class Route extends \Zend_Controller_Router_Route
32
147
*/
33
148
public function setServiceClass ($ serviceClass )
34
149
{
35
- $ this ->_serviceClass = $ serviceClass ;
150
+ $ this ->serviceClass = $ serviceClass ;
36
151
return $ this ;
37
152
}
38
153
@@ -43,7 +158,7 @@ public function setServiceClass($serviceClass)
43
158
*/
44
159
public function getServiceClass ()
45
160
{
46
- return $ this ->_serviceClass ;
161
+ return $ this ->serviceClass ;
47
162
}
48
163
49
164
/**
@@ -54,7 +169,7 @@ public function getServiceClass()
54
169
*/
55
170
public function setServiceMethod ($ serviceMethod )
56
171
{
57
- $ this ->_serviceMethod = $ serviceMethod ;
172
+ $ this ->serviceMethod = $ serviceMethod ;
58
173
return $ this ;
59
174
}
60
175
@@ -65,7 +180,7 @@ public function setServiceMethod($serviceMethod)
65
180
*/
66
181
public function getServiceMethod ()
67
182
{
68
- return $ this ->_serviceMethod ;
183
+ return $ this ->serviceMethod ;
69
184
}
70
185
71
186
/**
@@ -76,7 +191,7 @@ public function getServiceMethod()
76
191
*/
77
192
public function setSecure ($ secure )
78
193
{
79
- $ this ->_secure = $ secure ;
194
+ $ this ->secure = $ secure ;
80
195
return $ this ;
81
196
}
82
197
@@ -87,7 +202,7 @@ public function setSecure($secure)
87
202
*/
88
203
public function isSecure ()
89
204
{
90
- return $ this ->_secure ;
205
+ return $ this ->secure ;
91
206
}
92
207
93
208
/**
@@ -98,7 +213,7 @@ public function isSecure()
98
213
*/
99
214
public function setAclResources ($ aclResources )
100
215
{
101
- $ this ->_aclResources = $ aclResources ;
216
+ $ this ->aclResources = $ aclResources ;
102
217
return $ this ;
103
218
}
104
219
@@ -109,7 +224,7 @@ public function setAclResources($aclResources)
109
224
*/
110
225
public function getAclResources ()
111
226
{
112
- return $ this ->_aclResources ;
227
+ return $ this ->aclResources ;
113
228
}
114
229
115
230
/**
@@ -120,7 +235,7 @@ public function getAclResources()
120
235
*/
121
236
public function setParameters ($ parameters )
122
237
{
123
- $ this ->_parameters = $ parameters ;
238
+ $ this ->parameters = $ parameters ;
124
239
return $ this ;
125
240
}
126
241
@@ -131,19 +246,6 @@ public function setParameters($parameters)
131
246
*/
132
247
public function getParameters ()
133
248
{
134
- return $ this ->_parameters ;
135
- }
136
-
137
- /**
138
- * Matches a Request with parts defined by a map. Assigns and
139
- * returns an array of variables on a successful match.
140
- *
141
- * @param \Magento\Webapi\Controller\Request $request
142
- * @param boolean $partial Partial path matching
143
- * @return array|bool An array of assigned values or a boolean false on a mismatch
144
- */
145
- public function match ($ request , $ partial = false )
146
- {
147
- return parent ::match (strtolower (ltrim ($ request ->getPathInfo (), $ this ->_urlDelimiter )), $ partial );
249
+ return $ this ->parameters ;
148
250
}
149
251
}
0 commit comments