1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System Framework package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @author Steeve Andrian Salim |
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
13
|
|
|
|
14
|
|
|
namespace O2System\Kernel\Http\Router\DataStructures; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Kernel\Http\Message\Uri\Segments; |
19
|
|
|
use O2System\Spl\Info\SplClassInfo; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Controller |
23
|
|
|
* |
24
|
|
|
* @package O2System\DataStructures |
25
|
|
|
*/ |
26
|
|
|
class Controller extends SplClassInfo |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Controller::$requestSegments |
30
|
|
|
* |
31
|
|
|
* Request Segments |
32
|
|
|
* |
33
|
|
|
* @var Segments |
34
|
|
|
*/ |
35
|
|
|
private $requestSegments; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Controller::$requestMethod |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
private $requestMethod = null; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Controller::$requestMethodArgs |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $requestMethodArgs = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Controller::$properties |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $properties = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Controller::$instance |
60
|
|
|
* |
61
|
|
|
* @var \O2System\Kernel\Http\Controller |
62
|
|
|
*/ |
63
|
|
|
private $instance; |
64
|
|
|
|
65
|
|
|
// ------------------------------------------------------------------------ |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Controller::__construct |
69
|
|
|
* |
70
|
|
|
* @param string $filePath |
71
|
|
|
*/ |
72
|
|
|
public function __construct($filePath) |
73
|
|
|
{ |
74
|
|
|
if (is_object($filePath)) { |
|
|
|
|
75
|
|
|
if ($filePath instanceof \O2System\Kernel\Http\Controller) { |
76
|
|
|
parent::__construct($filePath); |
77
|
|
|
$this->instance = $filePath; |
78
|
|
|
} |
79
|
|
|
} elseif (is_string($filePath) && is_file($filePath)) { |
80
|
|
|
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME)); |
81
|
|
|
@list($namespaceDirectory, $subNamespace) = explode('Controllers', dirname($filePath)); |
82
|
|
|
|
83
|
|
|
$classNamespace = loader()->getDirNamespace( |
|
|
|
|
84
|
|
|
$namespaceDirectory |
85
|
|
|
) . 'Controllers' . (empty($subNamespace) ? null : str_replace('/', '\\', $subNamespace)) . '\\'; |
86
|
|
|
$className = $classNamespace . $className; |
87
|
|
|
|
88
|
|
|
if (class_exists($className)) { |
89
|
|
|
parent::__construct($className); |
90
|
|
|
} elseif (class_exists('\O2System\Kernel\Http\\' . $className)) { |
91
|
|
|
parent::__construct('\O2System\Kernel\Http\\' . $className); |
92
|
|
|
} |
93
|
|
|
} elseif (class_exists($filePath)) { |
94
|
|
|
parent::__construct($filePath); |
95
|
|
|
} elseif (class_exists('\O2System\Kernel\Http\\' . $filePath)) { |
96
|
|
|
parent::__construct('\O2System\Kernel\Http\\' . $filePath); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// ------------------------------------------------------------------------ |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Controller::setProperties |
104
|
|
|
* |
105
|
|
|
* @param array $properties |
106
|
|
|
*/ |
107
|
|
|
public function setProperties(array $properties) |
108
|
|
|
{ |
109
|
|
|
$this->properties = $properties; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
// ------------------------------------------------------------------------ |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Controller::getInstance |
116
|
|
|
* |
117
|
|
|
* @return \O2System\Kernel\Http\Controller|string |
118
|
|
|
*/ |
119
|
|
|
public function &getInstance() |
120
|
|
|
{ |
121
|
|
|
if (empty($this->instance)) { |
122
|
|
|
$className = $this->name; |
123
|
|
|
$this->instance = new $className(); |
124
|
|
|
|
125
|
|
|
if (count($this->properties)) { |
126
|
|
|
foreach ($this->properties as $key => $value) { |
127
|
|
|
$setterMethodName = camelcase('set_' . $key); |
128
|
|
|
|
129
|
|
|
if (method_exists($this->instance, $setterMethodName)) { |
130
|
|
|
$this->instance->{$setterMethodName}($value); |
131
|
|
|
} else { |
132
|
|
|
$this->instance->{$key} = $value; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->instance; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// ------------------------------------------------------------------------ |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Controller::getRequestSegments |
145
|
|
|
* |
146
|
|
|
* @return \O2System\Kernel\Http\Message\Uri\Segments |
147
|
|
|
*/ |
148
|
|
|
public function getRequestSegments() |
149
|
|
|
{ |
150
|
|
|
if (empty($this->requestSegments)) { |
151
|
|
|
$segments[] = $this->getParameter(); |
|
|
|
|
152
|
|
|
|
153
|
|
|
if ( ! in_array($this->getRequestMethod(), ['index', 'route'])) { |
154
|
|
|
array_push($segments, $this->getRequestMethod()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
$this->setRequestSegments($segments); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return $this->requestSegments; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// ------------------------------------------------------------------------ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Controller::setRequestSegments |
167
|
|
|
* |
168
|
|
|
* @param array $segments |
169
|
|
|
* |
170
|
|
|
* @return static |
171
|
|
|
*/ |
172
|
|
|
public function setRequestSegments(array $segments) |
173
|
|
|
{ |
174
|
|
|
$this->requestSegments = new Segments($segments); |
|
|
|
|
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// ------------------------------------------------------------------------ |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Controller::getParameter |
183
|
|
|
* |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function getParameter() |
187
|
|
|
{ |
188
|
|
|
return dash(get_class_name($this->name)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// ------------------------------------------------------------------------ |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Controller::getRequestMethod |
195
|
|
|
* |
196
|
|
|
* @return string|null |
197
|
|
|
*/ |
198
|
|
|
public function getRequestMethod() |
199
|
|
|
{ |
200
|
|
|
return $this->requestMethod; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// ------------------------------------------------------------------------ |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Controller::setRequestMethod |
207
|
|
|
* |
208
|
|
|
* @param string $method |
209
|
|
|
* |
210
|
|
|
* @return static |
211
|
|
|
*/ |
212
|
|
|
public function setRequestMethod($method) |
213
|
|
|
{ |
214
|
|
|
$this->requestMethod = $method; |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// ------------------------------------------------------------------------ |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Controller::getRequestMethodArgs |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public function getRequestMethodArgs() |
227
|
|
|
{ |
228
|
|
|
return $this->requestMethodArgs; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
// ------------------------------------------------------------------------ |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Controller::setRequestMethodArgs |
235
|
|
|
* |
236
|
|
|
* @param array $arguments |
237
|
|
|
* |
238
|
|
|
* @return static |
239
|
|
|
*/ |
240
|
|
|
public function setRequestMethodArgs(array $arguments) |
241
|
|
|
{ |
242
|
|
|
$arguments = array_values($arguments); |
243
|
|
|
array_unshift($arguments, null); |
244
|
|
|
unset($arguments[ 0 ]); |
245
|
|
|
|
246
|
|
|
$this->requestMethodArgs = $arguments; |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// ------------------------------------------------------------------------ |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* Controller::isValid |
255
|
|
|
* |
256
|
|
|
* @return bool |
257
|
|
|
*/ |
258
|
|
|
public function isValid() |
259
|
|
|
{ |
260
|
|
|
if ( ! empty($this->name) and $this->hasMethod('__call') and $this->isSubclassOf('\O2System\Kernel\Http\Controller')) { |
261
|
|
|
return true; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return false; |
265
|
|
|
} |
266
|
|
|
} |