1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Route; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A container for routes. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class Route |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Properties |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
private $name; // A name for this route |
17
|
|
|
private $rule; // The rule for this route |
18
|
|
|
private $action; // The controller action to handle this route |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Set values for route. |
24
|
|
|
* |
25
|
|
|
* @param string $rule for this route |
26
|
|
|
* @param callable $action callable to implement a controller for the route |
27
|
|
|
* |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
9 |
|
public function set($rule, $action) |
31
|
|
|
{ |
32
|
9 |
|
$this->rule = $rule; |
33
|
9 |
|
$this->action = $action; |
34
|
|
|
|
35
|
9 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if the route matches a query |
42
|
|
|
* |
43
|
|
|
* @param string $query to match against |
44
|
|
|
* |
45
|
|
|
* @return boolean true if query matches the route |
46
|
|
|
*/ |
47
|
9 |
|
public function match($query) |
48
|
|
|
{ |
49
|
9 |
|
$ruleParts = explode('/', $this->rule); |
50
|
9 |
|
$queryParts = explode('/', $query); |
51
|
9 |
|
$ruleCount = max(count($ruleParts), count($queryParts)); |
52
|
|
|
|
53
|
|
|
// If default route, match anything |
54
|
9 |
|
if ($this->rule == "*") { |
55
|
2 |
|
return true; |
56
|
|
|
} |
57
|
|
|
|
58
|
7 |
|
$match = false; |
|
|
|
|
59
|
7 |
|
for ($i = 0; $i < $ruleCount; $i++) { |
60
|
7 |
|
$rulePart = isset($ruleParts[$i]) ? $ruleParts[$i] : null; |
61
|
7 |
|
$queryPart = isset($queryParts[$i]) ? $queryParts[$i] : null; |
62
|
|
|
|
63
|
|
|
// Support various rules for matching the parts |
64
|
7 |
|
$first = isset($rulePart[0]) ? $rulePart[0] : ''; |
65
|
|
|
switch ($first) { |
66
|
7 |
|
case '*': |
67
|
1 |
|
$match = true; |
68
|
1 |
|
break; |
69
|
|
|
|
70
|
7 |
|
default: |
71
|
7 |
|
$match = ($rulePart == $queryPart); |
72
|
7 |
|
break; |
73
|
7 |
|
} |
74
|
|
|
|
75
|
|
|
// Continue as long as each part matches |
76
|
7 |
|
if (!$match) { |
77
|
4 |
|
return false; |
78
|
|
|
} |
79
|
7 |
|
} |
80
|
|
|
|
81
|
7 |
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Handle the action for the route. |
88
|
|
|
* |
89
|
|
|
* @return void |
90
|
|
|
*/ |
91
|
5 |
|
public function handle() |
92
|
|
|
{ |
93
|
5 |
|
return call_user_func($this->action); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the name of the route. |
100
|
|
|
* |
101
|
|
|
* @param string $name set a name for the route |
102
|
|
|
* |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setName($name) |
106
|
|
|
{ |
107
|
|
|
$this->name = $name; |
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the rule for the route. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getRule() |
119
|
|
|
{ |
120
|
|
|
return $this->rule; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.