1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Route\Dispatcher; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Route\Collector\RouteCollector; |
5
|
|
|
use JayaCode\Framework\Core\Route\Route; |
6
|
|
|
use JayaCode\Framework\Core\Route\Status; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class RegexDispatcher |
10
|
|
|
* @package JayaCode\Framework\Core\Route\Dispatcher |
11
|
|
|
*/ |
12
|
|
|
class RegexDispatcher implements Dispatcher |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var RouteCollector |
16
|
|
|
*/ |
17
|
|
|
protected $routeCollector; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var null |
21
|
|
|
*/ |
22
|
|
|
protected $data; |
23
|
|
|
/** |
24
|
|
|
* @var |
25
|
|
|
*/ |
26
|
|
|
protected $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $explodedPath; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var |
35
|
|
|
*/ |
36
|
|
|
protected $httpMethod; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param RouteCollector $routeCollector |
40
|
|
|
* @param null $data |
41
|
|
|
*/ |
42
|
3 |
|
public function __construct(RouteCollector $routeCollector, $data = null) |
43
|
|
|
{ |
44
|
3 |
|
$this->routeCollector = $routeCollector; |
45
|
3 |
|
$this->data = $data; |
46
|
3 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param $httpMethod |
50
|
|
|
* @param $path |
51
|
|
|
* @return array|mixed |
52
|
|
|
*/ |
53
|
3 |
|
public function dispatch($httpMethod, $path) |
54
|
|
|
{ |
55
|
3 |
|
$this->setPath($path); |
56
|
3 |
|
$this->httpMethod = $httpMethod; |
57
|
|
|
|
58
|
3 |
|
$data = is_null($this->data)?$this->routeCollector->getData():$this->data; |
59
|
3 |
|
return $this->findDataFromAllRoute($data); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $data |
64
|
|
|
* @return array |
65
|
|
|
*/ |
66
|
3 |
|
protected function findDataFromAllRoute(array $data) |
67
|
|
|
{ |
68
|
|
|
$result = [ |
69
|
|
|
Status::NOT_FOUND |
70
|
3 |
|
]; |
71
|
|
|
|
72
|
3 |
|
$countData = count($data); |
73
|
3 |
|
$countExplodePath = count($this->explodedPath); |
74
|
|
|
|
75
|
3 |
|
for ($i=0; $result[0] == Status::NOT_FOUND && $countData > $i; $i++) { |
76
|
3 |
|
$this->findDataByRouteMethodAndRulePath($data[$i], $countExplodePath, $result); |
77
|
3 |
|
} |
78
|
|
|
|
79
|
3 |
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param Route $route |
84
|
|
|
* @param $countExplodePath |
85
|
|
|
* @param array $result |
86
|
|
|
*/ |
87
|
3 |
|
protected function findDataByRouteMethodAndRulePath(Route $route, $countExplodePath, array &$result) |
88
|
|
|
{ |
89
|
3 |
|
if ($this->httpMethod == $route->method && $countExplodePath == count($route->explodePath)) { |
90
|
3 |
|
$this->findDataByAllMatchesRulePath($route, $countExplodePath, $result); |
91
|
|
|
|
92
|
3 |
|
if ($result[0] == Status::FOUND) { |
93
|
3 |
|
$result[1] = $route->handle; |
94
|
3 |
|
} |
95
|
3 |
|
} |
96
|
3 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Route $route |
100
|
|
|
* @param $countExplodePath |
101
|
|
|
* @param array $result |
102
|
|
|
*/ |
103
|
3 |
|
protected function findDataByAllMatchesRulePath(Route $route, $countExplodePath, array &$result) |
104
|
|
|
{ |
105
|
3 |
|
$result[0] = Status::FOUND; |
106
|
3 |
|
if ($this->path != $route->path) { |
107
|
3 |
|
for ($j = 0; $j < $countExplodePath && $result[0] == Status::FOUND; $j++) { |
108
|
3 |
|
$this->findDataByMatchesRulePath($route, $j, $result); |
109
|
3 |
|
} |
110
|
3 |
|
} |
111
|
3 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Route $route |
115
|
|
|
* @param $index |
116
|
|
|
* @param array $result |
117
|
|
|
*/ |
118
|
3 |
|
protected function findDataByMatchesRulePath(Route $route, $index, array &$result) |
119
|
|
|
{ |
120
|
3 |
|
$countMatches = count($route->ruleMatches[$index]); |
121
|
|
|
|
122
|
3 |
|
if ($countMatches == 0 || |
123
|
3 |
|
$countMatches == 2 && $this->explodedPath[$index] != $route->explodePath[$index] || |
124
|
3 |
|
$countMatches == 5 && |
125
|
3 |
|
!preg_match("/^".$route->ruleMatches[$index][4]."$/", $this->explodedPath[$index])) { |
126
|
3 |
|
$result[0] = Status::NOT_FOUND; |
127
|
3 |
|
} elseif ($countMatches == 3 || $countMatches == 5) { |
128
|
3 |
|
$result[2][$route->ruleMatches[$index][2]] = $this->explodedPath[$index]; |
129
|
3 |
|
} |
130
|
3 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param mixed $path |
134
|
|
|
*/ |
135
|
3 |
|
public function setPath($path) |
136
|
|
|
{ |
137
|
3 |
|
$this->path = static::normalizePath($path); |
138
|
|
|
|
139
|
3 |
|
$this->explodedPath = static::explodePath($this->path); |
140
|
3 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param $path |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
3 |
|
public static function normalizePath($path) |
147
|
|
|
{ |
148
|
3 |
|
$path = trim($path, '/'); |
149
|
3 |
|
return $path == ""?'/':$path; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $path |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
3 |
|
public static function explodePath($path) |
157
|
|
|
{ |
158
|
3 |
|
return (array) explode("/", $path); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|