|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
namespace Mezon\Router; |
|
4
|
|
|
|
|
5
|
|
|
use Mezon\Router\Types\BaseType; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait SimpleUrlParser |
|
9
|
|
|
* |
|
10
|
|
|
* @package Router |
|
11
|
|
|
* @author Dodonov A.A. |
|
12
|
|
|
* @version v.1.0 (2021/09/27) |
|
13
|
|
|
* @copyright Copyright (c) 2021, aeon.org |
|
14
|
|
|
*/ |
|
15
|
|
|
trait SimpleUrlParser |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
use UrlParserBase; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Called route |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $calledRoute = ''; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Method compiles route pattern string in regex string. |
|
29
|
|
|
* For example [i:id]/some-str in ([\[0-9\]])/some-str |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $routerPattern |
|
32
|
|
|
* router pattern |
|
33
|
|
|
* @return string regexp pattern |
|
34
|
|
|
*/ |
|
35
|
|
|
private function _getRouteMatcherRegExPattern(string $routerPattern): string |
|
36
|
|
|
{ |
|
37
|
|
|
// parsing routes |
|
38
|
|
|
$compiledRouterPattern = $routerPattern; |
|
39
|
|
|
|
|
40
|
|
|
foreach ($this->types as $typeClass) { |
|
41
|
|
|
$compiledRouterPattern = preg_replace('/' . $typeClass::searchRegExp() . '/', '(' . $typeClass::parserRegExp() . ')', $compiledRouterPattern); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return str_replace('/', '\/', $compiledRouterPattern); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Method returns all parameter names in the route |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $routerPattern |
|
51
|
|
|
* route |
|
52
|
|
|
* @return string[] names |
|
53
|
|
|
*/ |
|
54
|
|
|
private function _getParameterNames(string $routerPattern): array |
|
55
|
|
|
{ |
|
56
|
|
|
$regExPattern = []; |
|
57
|
|
|
|
|
58
|
|
|
foreach (array_keys($this->types) as $typeName) { |
|
59
|
|
|
$regExPattern[] = $typeName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$regExPattern = '\[(' . implode('|', $regExPattern) . '):(' . BaseType::PARAMETER_NAME_REGEXP . ')\]'; |
|
63
|
|
|
|
|
64
|
|
|
$names = []; |
|
65
|
|
|
preg_match_all('/' . str_replace('/', '\\/', $regExPattern) . '/', $routerPattern, $names); |
|
66
|
|
|
|
|
67
|
|
|
$return = []; |
|
68
|
|
|
|
|
69
|
|
|
foreach ($names[2] as $name) { |
|
70
|
|
|
$return[] = $name; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $return; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Method searches dynamic route processor |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $route |
|
80
|
|
|
* route |
|
81
|
|
|
* @param string $requestMethod |
|
82
|
|
|
* request method |
|
83
|
|
|
* @return array{0: string, 1:string}|callable|string|false route's handler or false in case the handler was not found |
|
84
|
|
|
* @psalm-suppress PossiblyUndefinedArrayOffset |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function getDynamicRouteProcessor(string $route, string $requestMethod = '') |
|
87
|
|
|
{ |
|
88
|
|
|
$routes = $this->paramRoutes[$requestMethod == '' ? $this->getRequestMethod() : $requestMethod]; |
|
89
|
|
|
|
|
90
|
|
|
foreach ($routes as $item) { |
|
91
|
|
|
$matches = []; |
|
92
|
|
|
|
|
93
|
|
|
if (preg_match('/^' . $this->_getRouteMatcherRegExPattern($item['pattern']) . '$/', $route, $matches)) { |
|
94
|
|
|
$names = $this->_getParameterNames($item['pattern']); |
|
95
|
|
|
|
|
96
|
|
|
$this->parameters = []; |
|
97
|
|
|
foreach ($names as $i => $name) { |
|
98
|
|
|
$this->parameters[$name] = $matches[(int) $i + 1]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->calledRoute = $item['pattern']; |
|
102
|
|
|
|
|
103
|
|
|
return $item['callback']; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// match was not found |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|