Total Complexity | 90 |
Total Lines | 502 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like TUrlMappingPattern often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TUrlMappingPattern, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
117 | class TUrlMappingPattern extends \Prado\TComponent |
||
118 | { |
||
119 | /** |
||
120 | * @var string service parameter such as Page class name. |
||
121 | */ |
||
122 | private $_serviceParameter; |
||
123 | /** |
||
124 | * @var string service ID, default is 'page'. |
||
125 | */ |
||
126 | private $_serviceID = 'page'; |
||
127 | /** |
||
128 | * @var string url pattern to match. |
||
129 | */ |
||
130 | private $_pattern; |
||
131 | /** |
||
132 | * @var TAttributeCollection parameter regular expressions. |
||
133 | */ |
||
134 | private $_parameters; |
||
135 | /** |
||
136 | * @var TAttributeCollection of constant parameters. |
||
137 | */ |
||
138 | protected $_constants; |
||
139 | /** |
||
140 | * @var string regular expression pattern. |
||
141 | */ |
||
142 | private $_regexp = ''; |
||
143 | |||
144 | private $_customUrl = true; |
||
145 | |||
146 | private $_manager; |
||
147 | |||
148 | private $_caseSensitive = true; |
||
149 | |||
150 | private $_isWildCardPattern = false; |
||
151 | |||
152 | private $_urlFormat = THttpRequestUrlFormat::Get; |
||
153 | |||
154 | private $_separator = '/'; |
||
155 | |||
156 | /** |
||
157 | * @var TUrlMappingPatternSecureConnection |
||
158 | * @since 3.2 |
||
159 | */ |
||
160 | private $_secureConnection = TUrlMappingPatternSecureConnection::Automatic; |
||
161 | |||
162 | /** |
||
163 | * Constructor. |
||
164 | * @param TUrlManager $manager the URL manager instance |
||
165 | */ |
||
166 | public function __construct(TUrlManager $manager) |
||
167 | { |
||
168 | $this->_manager = $manager; |
||
169 | parent::__construct(); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @return TUrlManager the URL manager instance |
||
174 | */ |
||
175 | public function getManager() |
||
176 | { |
||
177 | return $this->_manager; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Initializes the pattern. |
||
182 | * @param \Prado\Xml\TXmlElement $config configuration for this module. |
||
183 | * @throws TConfigurationException if service parameter is not specified |
||
184 | */ |
||
185 | public function init($config) |
||
|
|||
186 | { |
||
187 | if ($this->_serviceParameter === null) { |
||
188 | throw new TConfigurationException('urlmappingpattern_serviceparameter_required', $this->getPattern()); |
||
189 | } |
||
190 | if (strpos($this->_serviceParameter, '*') !== false) { |
||
191 | $this->_isWildCardPattern = true; |
||
192 | } |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * Substitute the parameter key value pairs as named groupings |
||
197 | * in the regular expression matching pattern. |
||
198 | * @return string regular expression pattern with parameter subsitution |
||
199 | */ |
||
200 | protected function getParameterizedPattern() |
||
201 | { |
||
202 | $params = []; |
||
203 | $values = []; |
||
204 | if ($this->_parameters) { |
||
205 | foreach ($this->_parameters as $key => $value) { |
||
206 | $params[] = '{' . $key . '}'; |
||
207 | $values[] = '(?P<' . $key . '>' . $value . ')'; |
||
208 | } |
||
209 | } |
||
210 | if ($this->getIsWildCardPattern()) { |
||
211 | $params[] = '{*}'; |
||
212 | // service parameter must not contain '=' and '/' |
||
213 | $values[] = '(?P<' . $this->getServiceID() . '>[^=/]+)'; |
||
214 | } |
||
215 | $params[] = '/'; |
||
216 | $values[] = '\\/'; |
||
217 | $regexp = str_replace($params, $values, trim($this->getPattern(), '/') . '/'); |
||
218 | if ($this->_urlFormat === THttpRequestUrlFormat::Get) { |
||
219 | $regexp = '/^' . $regexp . '$/u'; |
||
220 | } else { |
||
221 | $regexp = '/^' . $regexp . '(?P<urlparams>.*)$/u'; |
||
222 | } |
||
223 | |||
224 | if (!$this->getCaseSensitive()) { |
||
225 | $regexp .= 'i'; |
||
226 | } |
||
227 | return $regexp; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return string full regular expression mapping pattern |
||
232 | */ |
||
233 | public function getRegularExpression() |
||
234 | { |
||
235 | return $this->_regexp; |
||
236 | } |
||
237 | |||
238 | /** |
||
239 | * @param string $value full regular expression mapping pattern. |
||
240 | */ |
||
241 | public function setRegularExpression($value) |
||
242 | { |
||
243 | $this->_regexp = $value; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @return bool whether the {@see getPattern Pattern} should be treated as case sensititve. Defaults to true. |
||
248 | */ |
||
249 | public function getCaseSensitive() |
||
250 | { |
||
251 | return $this->_caseSensitive; |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param bool $value whether the {@see getPattern Pattern} should be treated as case sensititve. |
||
256 | */ |
||
257 | public function setCaseSensitive($value) |
||
258 | { |
||
259 | $this->_caseSensitive = TPropertyValue::ensureBoolean($value); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * @param string $value service parameter, such as page class name. |
||
264 | */ |
||
265 | public function setServiceParameter($value) |
||
266 | { |
||
267 | $this->_serviceParameter = $value; |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * @return string service parameter, such as page class name. |
||
272 | */ |
||
273 | public function getServiceParameter() |
||
274 | { |
||
275 | return $this->_serviceParameter; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * @param string $value service id to handle. |
||
280 | */ |
||
281 | public function setServiceID($value) |
||
282 | { |
||
283 | $this->_serviceID = $value; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * @return string service id. |
||
288 | */ |
||
289 | public function getServiceID() |
||
290 | { |
||
291 | return $this->_serviceID; |
||
292 | } |
||
293 | |||
294 | /** |
||
295 | * @return string url pattern to match. Defaults to ''. |
||
296 | */ |
||
297 | public function getPattern() |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * @param string $value url pattern to match. |
||
304 | */ |
||
305 | public function setPattern($value) |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * @return TAttributeCollection parameter key value pairs. |
||
312 | */ |
||
313 | public function getParameters() |
||
314 | { |
||
315 | if (!$this->_parameters) { |
||
316 | $this->_parameters = new TAttributeCollection(); |
||
317 | $this->_parameters->setCaseSensitive(true); |
||
318 | } |
||
319 | return $this->_parameters; |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * @param TAttributeCollection $value new parameter key value pairs. |
||
324 | */ |
||
325 | public function setParameters($value) |
||
326 | { |
||
327 | $this->_parameters = $value; |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @return TAttributeCollection constanst parameter key value pairs. |
||
332 | * @since 3.2.2 |
||
333 | */ |
||
334 | public function getConstants() |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Uses URL pattern (or full regular expression if available) to |
||
345 | * match the given url path. |
||
346 | * @param THttpRequest $request the request module |
||
347 | * @return array matched parameters, empty if no matches. |
||
348 | */ |
||
349 | public function getPatternMatches($request) |
||
350 | { |
||
351 | $matches = []; |
||
352 | if (($pattern = $this->getRegularExpression()) !== '') { |
||
353 | preg_match($pattern, $request->getPathInfo(), $matches); |
||
354 | } else { |
||
355 | preg_match($this->getParameterizedPattern(), trim($request->getPathInfo(), '/') . '/', $matches); |
||
356 | } |
||
357 | |||
358 | if ($this->getIsWildCardPattern() && isset($matches[$this->_serviceID])) { |
||
359 | $matches[$this->_serviceID] = str_replace('*', $matches[$this->_serviceID], $this->_serviceParameter); |
||
360 | } |
||
361 | |||
362 | if (isset($matches['urlparams'])) { |
||
363 | $params = explode('/', $matches['urlparams']); |
||
364 | if ($this->_separator === '/') { |
||
365 | while ($key = array_shift($params)) { |
||
366 | $matches[$key] = ($value = array_shift($params)) ? $value : ''; |
||
367 | } |
||
368 | } else { |
||
369 | array_pop($params); |
||
370 | foreach ($params as $param) { |
||
371 | [$key, $value] = explode($this->_separator, $param, 2); |
||
372 | $matches[$key] = $value; |
||
373 | } |
||
374 | } |
||
375 | unset($matches['urlparams']); |
||
376 | } |
||
377 | |||
378 | if (count($matches) > 0 && $this->_constants) { |
||
379 | foreach ($this->_constants->toArray() as $key => $value) { |
||
380 | $matches[$key] = $value; |
||
381 | } |
||
382 | } |
||
383 | |||
384 | return $matches; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Returns a value indicating whether to use this pattern to construct URL. |
||
389 | * @return bool whether to enable custom constructUrl. Defaults to true. |
||
390 | * @since 3.1.1 |
||
391 | */ |
||
392 | public function getEnableCustomUrl() |
||
393 | { |
||
394 | return $this->_customUrl; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Sets a value indicating whether to enable custom constructUrl using this pattern |
||
399 | * @param bool $value whether to enable custom constructUrl. |
||
400 | */ |
||
401 | public function setEnableCustomUrl($value) |
||
402 | { |
||
403 | $this->_customUrl = TPropertyValue::ensureBoolean($value); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * @return bool whether this pattern is a wildcard pattern |
||
408 | * @since 3.1.4 |
||
409 | */ |
||
410 | public function getIsWildCardPattern() |
||
411 | { |
||
412 | return $this->_isWildCardPattern; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * @return THttpRequestUrlFormat the format of URLs. Defaults to THttpRequestUrlFormat::Get. |
||
417 | */ |
||
418 | public function getUrlFormat() |
||
419 | { |
||
420 | return $this->_urlFormat; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Sets the format of URLs constructed and interpreted by this pattern. |
||
425 | * A Get URL format is like index.php?name1=value1&name2=value2 |
||
426 | * while a Path URL format is like index.php/name1/value1/name2/value. |
||
427 | * The separating character between name and value can be configured with |
||
428 | * {@see setUrlParamSeparator} and defaults to '/'. |
||
429 | * Changing the UrlFormat will affect {@see constructUrl} and how GET variables |
||
430 | * are parsed. |
||
431 | * @param THttpRequestUrlFormat $value the format of URLs. |
||
432 | * @since 3.1.4 |
||
433 | */ |
||
434 | public function setUrlFormat($value) |
||
435 | { |
||
436 | $this->_urlFormat = TPropertyValue::ensureEnum($value, THttpRequestUrlFormat::class); |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @return string separator used to separate GET variable name and value when URL format is Path. Defaults to slash '/'. |
||
441 | */ |
||
442 | public function getUrlParamSeparator() |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * @param string $value separator used to separate GET variable name and value when URL format is Path. |
||
449 | * @throws TInvalidDataValueException if the separator is not a single character |
||
450 | */ |
||
451 | public function setUrlParamSeparator($value) |
||
452 | { |
||
453 | if (strlen($value) === 1) { |
||
454 | $this->_separator = $value; |
||
455 | } else { |
||
456 | throw new TInvalidDataValueException('httprequest_separator_invalid'); |
||
457 | } |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @return TUrlMappingPatternSecureConnection the SecureConnection behavior. Defaults to {@see \Prado\Web\TUrlMappingPatternSecureConnection::Automatic Automatic} |
||
462 | * @since 3.2 |
||
463 | */ |
||
464 | public function getSecureConnection() |
||
465 | { |
||
466 | return $this->_secureConnection; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * @param TUrlMappingPatternSecureConnection $value the SecureConnection behavior. |
||
471 | * @since 3.2 |
||
472 | */ |
||
473 | public function setSecureConnection($value) |
||
474 | { |
||
475 | $this->_secureConnection = TPropertyValue::ensureEnum($value, TUrlMappingPatternSecureConnection::class); |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * @param array $getItems list of GET items to be put in the constructed URL |
||
480 | * @return bool whether this pattern IS the one for constructing the URL with the specified GET items. |
||
481 | * @since 3.1.1 |
||
482 | */ |
||
483 | public function supportCustomUrl($getItems) |
||
484 | { |
||
485 | if (!$this->_customUrl || $this->getPattern() === null) { |
||
486 | return false; |
||
487 | } |
||
488 | if ($this->_parameters) { |
||
489 | foreach ($this->_parameters as $key => $value) { |
||
490 | if (!isset($getItems[$key])) { |
||
491 | return false; |
||
492 | } |
||
493 | } |
||
494 | } |
||
495 | |||
496 | if ($this->_constants) { |
||
497 | foreach ($this->_constants->toArray() as $key => $value) { |
||
498 | if (!isset($getItems[$key])) { |
||
499 | return false; |
||
500 | } |
||
501 | if ($getItems[$key] != $value) { |
||
502 | return false; |
||
503 | } |
||
504 | } |
||
505 | } |
||
506 | return true; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Constructs a URL using this pattern. |
||
511 | * @param array $getItems list of GET variables |
||
512 | * @param bool $encodeAmpersand whether the ampersand should be encoded in the constructed URL |
||
513 | * @param bool $encodeGetItems whether the GET variables should be encoded in the constructed URL |
||
514 | * @return string the constructed URL |
||
515 | * @since 3.1.1 |
||
516 | */ |
||
517 | public function constructUrl($getItems, $encodeAmpersand, $encodeGetItems) |
||
518 | { |
||
519 | if ($this->_constants) { |
||
520 | foreach ($this->_constants->toArray() as $key => $value) { |
||
521 | unset($getItems[$key]); |
||
522 | } |
||
523 | } |
||
524 | |||
525 | $extra = []; |
||
526 | $replace = []; |
||
527 | // for the GET variables matching the pattern, put them in the URL path |
||
528 | foreach ($getItems as $key => $value) { |
||
529 | if (($this->_parameters && $this->_parameters->contains($key)) || ($key === '*' && $this->getIsWildCardPattern())) { |
||
530 | $replace['{' . $key . '}'] = $encodeGetItems ? rawurlencode($value) : $value; |
||
531 | } else { |
||
532 | $extra[$key] = $value; |
||
533 | } |
||
534 | } |
||
535 | |||
536 | $url = $this->_manager->getUrlPrefix() . '/' . ltrim(strtr($this->getPattern(), $replace), '/'); |
||
537 | |||
538 | // for the rest of the GET variables, put them in the query string |
||
539 | if (count($extra) > 0) { |
||
540 | if ($this->_urlFormat === THttpRequestUrlFormat::Path && $this->getIsWildCardPattern()) { |
||
541 | foreach ($extra as $name => $value) { |
||
542 | $url .= '/' . $name . $this->_separator . ($encodeGetItems ? rawurlencode($value) : $value); |
||
543 | } |
||
544 | return $url; |
||
545 | } |
||
546 | |||
547 | $url2 = ''; |
||
548 | $amp = $encodeAmpersand ? '&' : '&'; |
||
549 | if ($encodeGetItems) { |
||
550 | foreach ($extra as $name => $value) { |
||
551 | if (is_array($value)) { |
||
552 | $name = rawurlencode($name . '[]'); |
||
553 | foreach ($value as $v) { |
||
554 | $url2 .= $amp . $name . '=' . rawurlencode($v); |
||
555 | } |
||
556 | } else { |
||
557 | $url2 .= $amp . rawurlencode($name) . '=' . rawurlencode($value); |
||
558 | } |
||
559 | } |
||
560 | } else { |
||
561 | foreach ($extra as $name => $value) { |
||
562 | if (is_array($value)) { |
||
563 | foreach ($value as $v) { |
||
564 | $url2 .= $amp . $name . '[]=' . $v; |
||
565 | } |
||
566 | } else { |
||
567 | $url2 .= $amp . $name . '=' . $value; |
||
568 | } |
||
569 | } |
||
570 | } |
||
571 | $url = $url . '?' . substr($url2, strlen($amp)); |
||
572 | } |
||
573 | return $this -> applySecureConnectionPrefix($url); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Apply behavior of {@see SecureConnection} property by conditionaly prefixing |
||
578 | * URL with {@see \Prado\Web\THttpRequest::getBaseUrl()} |
||
579 | * |
||
580 | * @param string $url |
||
581 | * @return string |
||
582 | * @since 3.2 |
||
583 | */ |
||
584 | protected function applySecureConnectionPrefix($url) |
||
619 | } |
||
620 | } |
||
621 | } |
||
622 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.