Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
route.php
1<?php
9namespace Bitrix\Main\Routing;
10
12
17class Route
18{
20 protected $uri;
21
23 protected $fullUri;
24
26 protected $matchUri;
27
29 protected $parameters;
30
33
35 protected $controller;
36
38 protected $options;
39
40 public function __construct($uri, $controller)
41 {
42 $this->uri = $uri;
43 $this->controller = $controller;
44 }
45
49 public function getOptions()
50 {
51 return $this->options;
52 }
53
57 public function setOptions($options)
58 {
59 $this->options = $options;
60 }
61
65 public function getController()
66 {
67 return $this->controller;
68 }
69
73 public function getParameters()
74 {
75 return $this->parameters;
76 }
77
81 public function getParametersValues()
82 {
83 if ($this->parametersValues === null)
84 {
85 $this->parametersValues = new Dictionary();
86 }
87
89 }
90
91 public function getParameterValue($name)
92 {
93 return $this->getParametersValues()->get($name);
94 }
95
96 public function compile()
97 {
98 if ($this->matchUri !== null)
99 {
100 return;
101 }
102
103 $this->matchUri = "#^{$this->getUri()}$#";
104 $this->parameters = [];
105
106 // there are parameters, collect them
107 preg_match_all('/{([a-z0-9_]+)}/i', $this->getUri(), $matches);
108 $parameterNames = $matches[1];
109
110 foreach ($parameterNames as $parameterName)
111 {
112 $pattern = null;
113
114 // check options for custom pattern
115 if ($this->options)
116 {
117 if ($this->options->hasWhere($parameterName))
118 {
119 // custom pattern
120 $pattern = $this->options->getWhere($parameterName);
121 }
122 elseif ($this->options->hasDefault($parameterName))
123 {
124 // can be empty
125 $pattern = '[^/]*';
126 }
127 }
128
129 if ($pattern === null)
130 {
131 // general case
132 $pattern = '[^/]+';
133 }
134
135 $this->parameters[$parameterName] = $pattern;
136
137 // put pattern in uri
138 $this->matchUri = str_replace(
139 "{{$parameterName}}",
140 "(?<{$parameterName}>{$pattern})",
141 $this->matchUri
142 );
143 }
144 }
145
146 public function compileFromCache($cacheData)
147 {
148 $this->matchUri = $cacheData['matchUri'];
149 $this->parameters = $cacheData['parameters'];
150 }
151
152 public function getCompileCache()
153 {
154 $this->compile();
155
156 return [
157 'matchUri' => $this->matchUri,
158 'parameters' => $this->parameters
159 ];
160 }
161
162 public function match($uriPath)
163 {
164 if (strpos($this->getUri(), '{') !== false)
165 {
166 // compile regexp
167 $this->compile();
168
169 // match
170 $result = preg_match($this->matchUri, $uriPath, $matches);
171
172 if ($result)
173 {
174 // set parameters to the request
175 $requestParameters = [];
176 $parametersList = array_keys($this->parameters);
177
178 foreach ($parametersList as $parameter)
179 {
180 if ($matches[$parameter] === '' && $this->options && $this->options->hasDefault($parameter))
181 {
182 // set default value if optional parameter is empty
183 $requestParameters[$parameter] = $this->options->getDefault($parameter);
184 }
185 else
186 {
187 $requestParameters[$parameter] = $matches[$parameter];
188 }
189 }
190
191 // set default values if parameter with the same name wasn't set in request
192 // e.g. "RULE" => "download=1&objectId=\$1"
193 if (!empty($defaultValues = $this->options->getDefault()))
194 {
195 foreach ($defaultValues as $parameter => $defaultValue)
196 {
197 if (!in_array($parameter, $parametersList))
198 {
199 $requestParameters[$parameter] = $defaultValue;
200 }
201 }
202 }
203
204 return $requestParameters;
205 }
206 }
207 else
208 {
209 if ($uriPath === $this->getUri())
210 {
211 $requestParameters = [];
212
213 // set default values if parameter with the same name wasn't set in request
214 // e.g. "RULE" => "download=1&objectId=\$1"
215 if (!empty($defaultValues = $this->options->getDefault()))
216 {
217 foreach ($defaultValues as $parameter => $defaultValue)
218 {
219 $requestParameters[$parameter] = $defaultValue;
220 }
221 }
222
223 return $requestParameters ?: true;
224 }
225 }
226
227 return false;
228 }
229
230 function getUri()
231 {
232 if ($this->fullUri === null)
233 {
234 $this->fullUri = $this->uri;
235
236 // concat with option prefix and cache
237 if ($this->options && $this->options->hasPrefix())
238 {
239 $this->fullUri = $this->options->getFullPrefix().'/'.$this->uri;
240 }
241 }
242
243 return $this->fullUri;
244 }
245}
compileFromCache($cacheData)
Definition route.php:146
__construct($uri, $controller)
Definition route.php:40