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\Cli\Router\DataStructures; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Spl\Info\SplClassInfo; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Commander |
22
|
|
|
* |
23
|
|
|
* @package O2System\DataStructures |
24
|
|
|
*/ |
25
|
|
|
class Commander extends SplClassInfo |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Commander::$requestMethod |
29
|
|
|
* |
30
|
|
|
* @var string|null |
31
|
|
|
*/ |
32
|
|
|
private $requestMethod = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Commander::$requestMethodArgs |
36
|
|
|
* |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $requestMethodArgs = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Commander::$properties |
43
|
|
|
* |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $properties = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Commander::$instance |
50
|
|
|
* |
51
|
|
|
* @var \O2System\Kernel\Cli\Commander |
52
|
|
|
*/ |
53
|
|
|
private $instance; |
54
|
|
|
|
55
|
|
|
// ------------------------------------------------------------------------ |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Commander::__construct |
59
|
|
|
* |
60
|
|
|
* @param string $filePath |
61
|
|
|
*/ |
62
|
|
|
public function __construct($filePath) |
63
|
|
|
{ |
64
|
|
|
if (is_object($filePath)) { |
|
|
|
|
65
|
|
|
if ($filePath instanceof \O2System\Kernel\Cli\Commander) { |
66
|
|
|
parent::__construct($filePath); |
67
|
|
|
$this->instance = $filePath; |
68
|
|
|
} |
69
|
|
|
} elseif (is_string($filePath) && is_file($filePath)) { |
70
|
|
|
$className = prepare_class_name(pathinfo($filePath, PATHINFO_FILENAME)); |
71
|
|
|
@list($namespaceDirectory, $subNamespace) = explode('Commanders', dirname($filePath)); |
72
|
|
|
$classNamespace = loader()->getDirNamespace( |
|
|
|
|
73
|
|
|
$namespaceDirectory |
74
|
|
|
) . 'Commanders' . (empty($subNamespace) ? null : str_replace('/', '\\', $subNamespace)) . '\\'; |
75
|
|
|
$className = $classNamespace . $className; |
76
|
|
|
|
77
|
|
|
if (class_exists('\O2System\Kernel\Cli\\' . $className)) { |
78
|
|
|
parent::__construct('\O2System\Kernel\Cli\\' . $className); |
79
|
|
|
} elseif (class_exists('\O2System\Framework\Cli\\' . $className)) { |
80
|
|
|
parent::__construct('\O2System\Framework\Cli\\' . $className); |
81
|
|
|
} elseif (class_exists('\O2System\Reactor\Cli\\' . $className)) { |
82
|
|
|
parent::__construct('\O2System\Reactor\Cli\\' . $className); |
83
|
|
|
} elseif (class_exists('\App\Cli\\' . $className)) { |
84
|
|
|
parent::__construct('\App\Cli\\' . $className); |
85
|
|
|
} elseif (class_exists('\App\\' . $className)) { |
86
|
|
|
parent::__construct('\App\\' . $className); |
87
|
|
|
} elseif (class_exists($className)) { |
88
|
|
|
parent::__construct($className); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// ------------------------------------------------------------------------ |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Commander::setProperties |
97
|
|
|
* |
98
|
|
|
* @param array $properties |
99
|
|
|
*/ |
100
|
|
|
public function setProperties(array $properties) |
101
|
|
|
{ |
102
|
|
|
$this->properties = $properties; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// ------------------------------------------------------------------------ |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Commander::getParameter |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getParameter() |
113
|
|
|
{ |
114
|
|
|
return strtolower(get_class_name($this->name)); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// ------------------------------------------------------------------------ |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Commander::getInstance |
121
|
|
|
* |
122
|
|
|
* @return \O2System\Kernel\Cli\Commander|string |
123
|
|
|
*/ |
124
|
|
|
public function &getInstance() |
125
|
|
|
{ |
126
|
|
|
if (empty($this->instance)) { |
127
|
|
|
$className = $this->name; |
128
|
|
|
$this->instance = new $className(); |
129
|
|
|
|
130
|
|
|
if (count($this->properties)) { |
131
|
|
|
foreach ($this->properties as $key => $value) { |
132
|
|
|
$setterMethodName = camelcase('set_' . $key); |
133
|
|
|
|
134
|
|
|
if (method_exists($this->instance, $setterMethodName)) { |
135
|
|
|
$this->instance->{$setterMethodName}($value); |
136
|
|
|
} else { |
137
|
|
|
$this->instance->{$key} = $value; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return $this->instance; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// ------------------------------------------------------------------------ |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Commander::getRequestMethod |
150
|
|
|
* |
151
|
|
|
* @return string|null |
152
|
|
|
*/ |
153
|
|
|
public function getRequestMethod() |
154
|
|
|
{ |
155
|
|
|
return $this->requestMethod; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
// ------------------------------------------------------------------------ |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Commander::setRequestMethod |
162
|
|
|
* |
163
|
|
|
* @param string $method |
164
|
|
|
* |
165
|
|
|
* @return static |
166
|
|
|
*/ |
167
|
|
|
public function setRequestMethod($method) |
168
|
|
|
{ |
169
|
|
|
$this->requestMethod = $method; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
// ------------------------------------------------------------------------ |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Commander::getRequestMethodArgs |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function getRequestMethodArgs() |
182
|
|
|
{ |
183
|
|
|
return $this->requestMethodArgs; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// ------------------------------------------------------------------------ |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Commander::setRequestMethodArgs |
190
|
|
|
* |
191
|
|
|
* @param array $arguments |
192
|
|
|
* |
193
|
|
|
* @return static |
194
|
|
|
*/ |
195
|
|
|
public function setRequestMethodArgs(array $arguments) |
196
|
|
|
{ |
197
|
|
|
$arguments = array_values($arguments); |
198
|
|
|
array_unshift($arguments, null); |
199
|
|
|
unset($arguments[ 0 ]); |
200
|
|
|
|
201
|
|
|
$this->requestMethodArgs = $arguments; |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
} |