1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JetFire\Routing; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class Route |
7
|
|
|
* @package JetFire\Routing |
8
|
|
|
* @method getParameters() |
9
|
|
|
* @method getBlock() |
10
|
|
|
* @method getKeys() |
11
|
|
|
* @method getPath() |
12
|
|
|
* @method string getParams() |
13
|
|
|
*/ |
14
|
|
|
class Route |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
private $url; |
21
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
private $name; |
25
|
|
|
/** |
26
|
|
|
* @var |
27
|
|
|
*/ |
28
|
|
|
private $callback; |
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $method; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $target = []; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $detail = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$this->method = (isset($_SERVER['REQUEST_METHOD'])) ? $_SERVER['REQUEST_METHOD'] : 'GET'; |
47
|
|
|
if ($this->method == 'POST' && isset($_SERVER['X-HTTP-METHOD-OVERRIDE'])) { |
48
|
|
|
$this->method = strtoupper($_SERVER['X-HTTP-METHOD-OVERRIDE']); |
49
|
|
|
} |
50
|
|
|
if (isset($_POST['_METHOD']) && in_array($_POST['_METHOD'], array('PUT', 'PATCH', 'HEAD', 'DELETE'))) { |
51
|
|
|
$this->method = $_POST['_METHOD']; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $args |
57
|
|
|
*/ |
58
|
|
|
public function set($args = []) |
59
|
|
|
{ |
60
|
|
|
if (isset($args['name'])) $this->name = $args['name']; |
61
|
|
|
if (isset($args['callback'])) $this->callback = $args['callback']; |
62
|
|
|
if (isset($args['target'])) $this->target = $args['target']; |
63
|
|
|
if (isset($args['detail'])) $this->detail = $args['detail']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return null |
68
|
|
|
*/ |
69
|
|
|
public function getUrl() |
70
|
|
|
{ |
71
|
|
|
return $this->url; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $url |
76
|
|
|
*/ |
77
|
|
|
public function setUrl($url) |
78
|
|
|
{ |
79
|
|
|
$this->url = $url; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return null |
84
|
|
|
*/ |
85
|
|
|
public function getName() |
86
|
|
|
{ |
87
|
|
|
return $this->name; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $name |
92
|
|
|
*/ |
93
|
|
|
public function setName($name) |
94
|
|
|
{ |
95
|
|
|
$this->name = $name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getCallback() |
102
|
|
|
{ |
103
|
|
|
return $this->callback; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $callback |
108
|
|
|
*/ |
109
|
|
|
public function setCallback($callback) |
110
|
|
|
{ |
111
|
|
|
$this->callback = $callback; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getMethod() |
118
|
|
|
{ |
119
|
|
|
return $this->method; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getDetail() |
126
|
|
|
{ |
127
|
|
|
return $this->detail; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param $detail |
132
|
|
|
*/ |
133
|
|
|
public function setDetail($detail) |
134
|
|
|
{ |
135
|
|
|
$this->detail = array_merge($detail, $this->detail); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param $key |
140
|
|
|
* @param $value |
141
|
|
|
*/ |
142
|
|
|
public function addDetail($key, $value) |
143
|
|
|
{ |
144
|
|
|
$this->detail[$key] = $value; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param null $key |
149
|
|
|
* @return array|string |
150
|
|
|
*/ |
151
|
|
|
public function getTarget($key = null) |
152
|
|
|
{ |
153
|
|
|
if (!is_null($key)) |
154
|
|
|
return isset($this->target[$key]) ? $this->target[$key] : ''; |
155
|
|
|
return empty($this->target) ? '' : $this->target; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $target |
160
|
|
|
*/ |
161
|
|
|
public function setTarget($target = []) |
162
|
|
|
{ |
163
|
|
|
$this->target = $target; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $key |
168
|
|
|
* @param $value |
169
|
|
|
*/ |
170
|
|
|
public function addTarget($key, $value) |
171
|
|
|
{ |
172
|
|
|
$this->target[$key] = $value; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param null $key |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
|
|
public function hasTarget($key = null) |
180
|
|
|
{ |
181
|
|
|
if (!is_null($key)) |
182
|
|
|
return isset($this->target[$key]) ? true : false; |
183
|
|
|
return empty($this->target) ? false : true; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
|
|
public function getData() |
190
|
|
|
{ |
191
|
|
|
return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data'])) ? $this->getDetail()['data'] : []; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param $name |
196
|
|
|
* @param $arguments |
197
|
|
|
* @return null |
198
|
|
|
*/ |
199
|
|
|
public function __call($name, $arguments) |
200
|
|
|
{ |
201
|
|
|
if (substr($name, 0, 3) === "get") { |
202
|
|
|
$key = strtolower(str_replace('get', '', $name)); |
203
|
|
|
return isset($this->detail[$key]) ? $this->detail[$key] : ''; |
204
|
|
|
} elseif (substr($name, 0, 3) === "set") { |
205
|
|
|
$key = strtolower(str_replace('set', '', $name)); |
206
|
|
|
$this->detail[$key] = $arguments[0]; |
207
|
|
|
} |
208
|
|
|
return ''; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: