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
|
|
|
protected $routeCollector; |
15
|
|
|
|
16
|
|
|
protected $data; |
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $path; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $explodedPath; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var |
29
|
|
|
*/ |
30
|
|
|
protected $httpMethod; |
31
|
|
|
|
32
|
3 |
|
public function __construct(RouteCollector $routeCollector, $data = null) |
33
|
|
|
{ |
34
|
3 |
|
$this->routeCollector = $routeCollector; |
35
|
3 |
|
$this->data = $data; |
36
|
3 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $httpMethod |
40
|
|
|
* @param $path |
41
|
|
|
*/ |
42
|
3 |
|
public function dispatch($httpMethod, $path) |
43
|
|
|
{ |
44
|
3 |
|
$this->setPath($path); |
45
|
3 |
|
$this->httpMethod = $httpMethod; |
46
|
|
|
|
47
|
3 |
|
$result[0] = Status::NOT_FOUND; |
|
|
|
|
48
|
|
|
|
49
|
3 |
|
$data = is_null($this->data)?$this->routeCollector->getData():$this->data; |
50
|
3 |
|
$countData = count($data); |
51
|
3 |
|
$countExplodePath = count($this->explodedPath); |
52
|
|
|
|
53
|
3 |
|
$i = 0; |
54
|
3 |
|
while ($result[0] == Status::NOT_FOUND && $countData > $i) { |
55
|
|
|
/** @var Route $route */ |
56
|
3 |
|
$route = $data[$i]; |
57
|
|
|
|
58
|
3 |
|
if ($this->httpMethod == $route->method && $countExplodePath == count($route->explodePath)) { |
59
|
3 |
|
$result[0] = Status::FOUND; |
60
|
3 |
|
if ($this->path != $route->path) { |
61
|
3 |
|
for ($j = 0; $j < $countExplodePath && $result[0] == Status::FOUND; $j++) { |
62
|
3 |
|
$countMatches = count($route->ruleMatches[$j]); |
63
|
|
|
|
64
|
3 |
|
if ($countMatches == 0 || $countMatches == 2 |
65
|
3 |
|
&& $this->explodedPath[$j] != $route->explodePath[$j]) { |
66
|
3 |
|
$result[0] = Status::NOT_FOUND; |
67
|
3 |
View Code Duplication |
} elseif ($countMatches == 3) { |
|
|
|
|
68
|
3 |
|
$result[2][$route->ruleMatches[$j][2]] = $this->explodedPath[$j]; |
69
|
3 |
|
} elseif ($countMatches == 5) { |
70
|
3 |
|
if (!preg_match("/^".$route->ruleMatches[$j][4]."$/", $this->explodedPath[$j])) { |
71
|
|
|
$result[0] = Status::NOT_FOUND; |
72
|
|
View Code Duplication |
} else { |
|
|
|
|
73
|
3 |
|
$result[2][$route->ruleMatches[$j][2]] = $this->explodedPath[$j]; |
74
|
|
|
} |
75
|
3 |
|
} elseif ($countMatches == 0) { |
76
|
|
|
$result[0] = Status::NOT_FOUND; |
77
|
|
|
} |
78
|
3 |
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
3 |
|
if ($result[0] == Status::FOUND) { |
83
|
3 |
|
$result[1] = $route->handle; |
84
|
3 |
|
} |
85
|
3 |
|
} |
86
|
|
|
|
87
|
3 |
|
$i++; |
88
|
3 |
|
} |
89
|
|
|
|
90
|
3 |
|
return $result; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param mixed $path |
95
|
|
|
*/ |
96
|
3 |
|
public function setPath($path) |
97
|
|
|
{ |
98
|
3 |
|
$this->path = static::normalizePath($path); |
99
|
|
|
|
100
|
3 |
|
$this->explodedPath = static::explodePath($this->path); |
101
|
3 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $path |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
3 |
|
public static function normalizePath($path) |
108
|
|
|
{ |
109
|
3 |
|
$path = trim($path, '/'); |
110
|
3 |
|
return $path == ""?'/':$path; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param $path |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
3 |
|
public static function explodePath($path) |
118
|
|
|
{ |
119
|
3 |
|
return (array) explode("/", $path); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.