1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Resta\Router; |
4
|
|
|
|
5
|
|
|
use Resta\Contracts\ApplicationContracts; |
6
|
|
|
use Resta\Foundation\ApplicationProvider; |
7
|
|
|
|
8
|
|
|
class RouteMatching extends ApplicationProvider |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var null|object |
12
|
|
|
*/ |
13
|
|
|
protected $route; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* RouteMatching constructor. |
17
|
|
|
* |
18
|
|
|
* @param ApplicationContracts $app |
19
|
|
|
* @param null|object $route |
20
|
|
|
*/ |
21
|
|
|
public function __construct(ApplicationContracts $app, $route=null) |
22
|
|
|
{ |
23
|
|
|
parent::__construct($app); |
24
|
|
|
|
25
|
|
|
$this->route = $route; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* get route pattern resolve |
30
|
|
|
* |
31
|
|
|
* @return array|int|string |
32
|
|
|
*/ |
33
|
|
|
public function getPatternResolve() |
34
|
|
|
{ |
35
|
|
|
$routes = $this->route->getRoutes(); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if(!isset($routes['pattern'])){ |
38
|
|
|
return []; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$patterns = $routes['pattern']; |
42
|
|
|
$urlRoute = array_filter(route(),'strlen'); |
43
|
|
|
|
44
|
|
|
$patternList = []; |
45
|
|
|
|
46
|
|
|
foreach($routes['data'] as $patternKey=>$routeData){ |
47
|
|
|
if($routeData['http']==httpMethod()){ |
48
|
|
|
$patternList[$patternKey]=$patterns[$patternKey]; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$scoredList = []; |
53
|
|
|
|
54
|
|
|
foreach ($patternList as $key=>$pattern){ |
55
|
|
|
|
56
|
|
|
$patternCount = $this->getPatternRealCount($pattern); |
57
|
|
|
|
58
|
|
|
if(count($urlRoute)==0 && count($urlRoute)==0) return array_key_first($patternList); |
59
|
|
|
|
60
|
|
|
if(isset($patternCount['optional'])){ |
61
|
|
|
$optionalCount = count($patternCount['default']) + count($patternCount['optional']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if(count($urlRoute) == count($patternCount['default']) || |
65
|
|
|
(isset($optionalCount) && count($urlRoute)>count($patternCount['default']) && $optionalCount>=count($urlRoute)) |
66
|
|
|
){ |
67
|
|
|
|
68
|
|
|
foreach ($pattern as $pkey=>$item){ |
69
|
|
|
|
70
|
|
|
if($this->route->isMatchVaribleRegexPattern($item)===false){ |
71
|
|
|
if(isset($urlRoute[$pkey]) && $urlRoute[$pkey]==$item){ |
72
|
|
|
$scoredList[$key][] = 3; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if($this->route->isMatchVaribleRegexPattern($item) && !$this->route->isOptionalVaribleRegexPattern($item)){ |
77
|
|
|
if(isset($urlRoute[$pkey])){ |
78
|
|
|
$scoredList[$key][] = 2; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if($this->route->isMatchVaribleRegexPattern($item) && $this->route->isOptionalVaribleRegexPattern($item)){ |
83
|
|
|
if(isset($urlRoute[$pkey])){ |
84
|
|
|
$scoredList[$key][] = 1; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->showKeyAccordingToScoredList($scoredList); |
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* get pattern real count |
98
|
|
|
* |
99
|
|
|
* @param $pattern |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
private function getPatternRealCount($pattern) |
103
|
|
|
{ |
104
|
|
|
$list = []; |
105
|
|
|
$list['default'] = []; |
106
|
|
|
|
107
|
|
|
foreach ($pattern as $key=>$value){ |
108
|
|
|
if(($this->route->isMatchVaribleRegexPattern($value)===false) || ($this->route->isMatchVaribleRegexPattern($value) |
109
|
|
|
&& !$this->route->isOptionalVaribleRegexPattern($value))){ |
110
|
|
|
$list['default'][$key] = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if(($this->route->isMatchVaribleRegexPattern($value) |
114
|
|
|
&& $this->route->isOptionalVaribleRegexPattern($value))){ |
115
|
|
|
$list['optional'][] = true; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $list; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* show key according tp scored list |
124
|
|
|
* |
125
|
|
|
* @param array $scoredList |
126
|
|
|
* @return false|int|string |
127
|
|
|
*/ |
128
|
|
|
private function showKeyAccordingToScoredList($scoredList=array()) |
129
|
|
|
{ |
130
|
|
|
$scored = []; |
131
|
|
|
|
132
|
|
|
foreach($scoredList as $key=>$item){ |
133
|
|
|
$scored[$key] = array_sum($item); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if(count($scored)){ |
137
|
|
|
$arrayCountValues = array_count_values($scored); |
138
|
|
|
|
139
|
|
|
if($arrayCountValues[max($scored)]==1){ |
140
|
|
|
return array_search(max($scored),$scored); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return null; |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* is same on static strings |
150
|
|
|
* |
151
|
|
|
* @param $pattern |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
private function isSameOnStaticStrings($pattern) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
$route = route(); |
157
|
|
|
|
158
|
|
|
foreach ($pattern as $key=>$item) { |
159
|
|
|
if($this->route->isMatchVaribleRegexPattern($item)===false){ |
160
|
|
|
if(isset($route[$key]) && $item!==$route[$key]){ |
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return true; |
167
|
|
|
} |
168
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.