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