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