Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Route.php
Go to the documentation of this file.
1 <?php
9 
12 
13 class Route implements RouterInterface
14 {
18  protected $serviceClass;
19 
23  protected $serviceMethod;
24 
28  protected $secure;
29 
33  protected $aclResources = [];
34 
38  protected $parameters = [];
39 
43  protected $variables = [];
44 
48  protected $route;
49 
53  public function __construct($route = '')
54  {
55  $this->route = trim($route, '/');
56  }
57 
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] = $value;
73  }
74  return $result;
75  }
76 
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 
98  protected function getPathParts($path)
99  {
100  return explode('/', trim($path, '/'));
101  }
102 
109  public function match(Request $request)
110  {
112  $pathParts = $this->getPathParts($request->getPathInfo());
113  $routeParts = $this->getRouteParts();
114  if (count($pathParts) <> count($routeParts)) {
115  return false;
116  }
117 
118  $result = [];
119  foreach ($pathParts as $key => $value) {
120  if (!array_key_exists($key, $routeParts)) {
121  return false;
122  }
123  $variable = isset($this->variables[$key]) ? $this->variables[$key] : null;
124  if ($variable) {
125  $result[$variable] = urldecode($pathParts[$key]);
126  } else {
127  if ($value != $routeParts[$key]) {
128  return false;
129  }
130  }
131  }
132  return $result;
133  }
134 
142  {
143  $this->serviceClass = $serviceClass;
144  return $this;
145  }
146 
152  public function getServiceClass()
153  {
154  return $this->serviceClass;
155  }
156 
164  {
165  $this->serviceMethod = $serviceMethod;
166  return $this;
167  }
168 
174  public function getServiceMethod()
175  {
176  return $this->serviceMethod;
177  }
178 
185  public function setSecure($secure)
186  {
187  $this->secure = $secure;
188  return $this;
189  }
190 
196  public function isSecure()
197  {
198  return $this->secure;
199  }
200 
208  {
209  $this->aclResources = $aclResources;
210  return $this;
211  }
212 
218  public function getAclResources()
219  {
220  return $this->aclResources;
221  }
222 
229  public function setParameters($parameters)
230  {
231  $this->parameters = $parameters;
232  return $this;
233  }
234 
240  public function getParameters()
241  {
242  return $this->parameters;
243  }
244 
250  public function getRoutePath()
251  {
252  return $this->route;
253  }
254 }
$variable
Definition: variable.php:7
$value
Definition: gender.phtml:16
match(RequestInterface $request)