1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\Route; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A container for routes. |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
class CRouteBasic |
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
|
1 |
|
public function set($rule, $action) |
31
|
|
|
{ |
32
|
1 |
|
$this->rule = $rule; |
33
|
1 |
|
$this->action = $action; |
34
|
|
|
|
35
|
1 |
|
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
|
1 |
|
public function match($query) |
48
|
|
|
{ |
49
|
1 |
|
$ruleParts = explode('/', $this->rule); |
50
|
1 |
|
$queryParts = explode('/', $query); |
51
|
|
|
|
52
|
1 |
|
$ruleCount = count($ruleParts); |
53
|
|
|
//$querCount = count($queryParts); |
54
|
|
|
|
55
|
1 |
|
$match = false; |
|
|
|
|
56
|
1 |
|
for ($i = 0; $i < $ruleCount; $i++) { |
57
|
1 |
|
$rp = $ruleParts[$i]; |
58
|
1 |
|
$qp = isset($queryParts[$i]) ? $queryParts[$i] : null; |
59
|
|
|
|
60
|
1 |
|
$first = isset($rp[0]) ? $rp[0] : ''; |
61
|
|
|
switch ($first) { |
62
|
1 |
|
case '*': |
63
|
1 |
|
$match = true; |
64
|
1 |
|
break; |
65
|
|
|
|
66
|
1 |
|
default: |
67
|
1 |
|
$match = ($rp == $qp); |
68
|
1 |
|
break; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
1 |
|
if (!$match) { |
72
|
1 |
|
return false; |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
|
76
|
|
|
//echo "$this->rule == $query\n"; |
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Handle the action for the route. |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function handle() |
88
|
|
|
{ |
89
|
|
|
return call_user_func($this->action); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set the name of the route. |
96
|
|
|
* |
97
|
|
|
* @param string $name set a name for the route |
98
|
|
|
* |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
|
|
public function setName($name) |
102
|
|
|
{ |
103
|
|
|
$this->name = $name; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Get the rule for the route. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getRule() |
115
|
|
|
{ |
116
|
|
|
return $this->rule; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
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.