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; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Kernel\Cli\Router\DataStructures\Commander; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Router |
22
|
|
|
* |
23
|
|
|
* @package O2System\Framework\Cli |
24
|
|
|
*/ |
25
|
|
|
class Router |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Router::$string |
29
|
|
|
* |
30
|
|
|
* Router request string. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $string; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Router::$commands |
38
|
|
|
* |
39
|
|
|
* Router request commands. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $commands = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Router::$commander |
47
|
|
|
* |
48
|
|
|
* Router request commander. |
49
|
|
|
* |
50
|
|
|
* @var Commander |
51
|
|
|
*/ |
52
|
|
|
protected $commander; |
53
|
|
|
|
54
|
|
|
// ----------------------------------------------------------------------- |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Router::handle |
58
|
|
|
* |
59
|
|
|
* Parse server argv to determine requested commander. |
60
|
|
|
* |
61
|
|
|
* @return void |
62
|
|
|
* @throws \ReflectionException |
63
|
|
|
*/ |
64
|
|
|
public function handle() |
65
|
|
|
{ |
66
|
|
|
$argv = $_SERVER[ 'argv' ]; |
67
|
|
|
|
68
|
|
|
if ($_SERVER[ 'SCRIPT_NAME' ] === $_SERVER[ 'argv' ][ 0 ]) { |
69
|
|
|
array_shift($argv); |
70
|
|
|
|
71
|
|
|
if (empty($argv)) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->string = str_replace(['/', '\\', ':'], '/', $argv[ 0 ]); |
77
|
|
|
$this->commands = explode('/', $this->string); |
78
|
|
|
|
79
|
|
|
if (strpos($this->commands[ 0 ], '--') !== false |
80
|
|
|
|| strpos($this->commands[ 0 ], '-') !== false |
81
|
|
|
) { |
82
|
|
|
$options = $this->commands; |
83
|
|
|
$this->commands = []; |
84
|
|
|
} else { |
85
|
|
|
$options = array_slice($argv, 1); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
foreach ($options as $option) { |
89
|
|
|
if (strpos($option, '--') !== false |
90
|
|
|
|| strpos($option, '-') !== false |
91
|
|
|
) { |
92
|
|
|
$option = str_replace(['-', '--'], '', $option); |
93
|
|
|
$option = str_replace(':', '=', $option); |
94
|
|
|
$option = str_replace('"', '', $option); |
95
|
|
|
$value = null; |
96
|
|
|
|
97
|
|
|
if (strpos($option, '=') !== false) { |
98
|
|
|
$optionParts = explode('=', $option); |
99
|
|
|
$option = $optionParts[ 0 ]; |
100
|
|
|
$value = $optionParts[ 1 ]; |
101
|
|
|
} else { |
102
|
|
|
$value = current($options); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($value === 'true') { |
106
|
|
|
$value = true; |
107
|
|
|
} elseif ($value === 'false') { |
108
|
|
|
$value = false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if (strpos($value, '--') === false |
112
|
|
|
|| strpos($value, '-') === false |
113
|
|
|
) { |
114
|
|
|
$_GET[ $option ] = $value; |
115
|
|
|
} else { |
116
|
|
|
$_GET[ $option ] = null; |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
|
|
$keys = array_keys($_GET); |
120
|
|
|
if (count($keys)) { |
121
|
|
|
$key = end($keys); |
122
|
|
|
$_GET[ $key ] = $option; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (array_key_exists('verbose', $_GET) or array_key_exists('v', $_GET)) { |
128
|
|
|
$_ENV[ 'VERBOSE' ] = true; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if( $this->parseCommands($this->commands) === false ){ |
132
|
|
|
output()->sendError(404); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// ------------------------------------------------------------------------ |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Router::parseSegments |
140
|
|
|
* |
141
|
|
|
* Parse and validate requested commands. |
142
|
|
|
* |
143
|
|
|
* @param array $commands |
144
|
|
|
* |
145
|
|
|
* @throws \ReflectionException |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
protected function parseCommands(array $commands) |
150
|
|
|
{ |
151
|
|
|
$numCommands = count($commands); |
152
|
|
|
$commanderRegistry = null; |
153
|
|
|
$commandersDirectories = [ |
154
|
|
|
defined('PATH_REACTOR') ? PATH_REACTOR . 'Cli' . DIRECTORY_SEPARATOR . 'Commanders' . DIRECTORY_SEPARATOR : PATH_FRAMEWORK . 'Cli' . DIRECTORY_SEPARATOR . 'Commanders' . DIRECTORY_SEPARATOR, |
|
|
|
|
155
|
|
|
PATH_APP . 'Commanders' . DIRECTORY_SEPARATOR |
|
|
|
|
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
if (function_exists('modules')) { |
159
|
|
|
$commandersDirectories = modules()->getDirs('Commanders'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
for ($i = 0; $i <= $numCommands; $i++) { |
163
|
|
|
$routedCommands = array_slice($commands, 0, ($numCommands - $i)); |
164
|
|
|
|
165
|
|
|
$commanderFilename = implode(DIRECTORY_SEPARATOR, $routedCommands); |
166
|
|
|
$commanderFilename = prepare_filename($commanderFilename) . '.php'; |
167
|
|
|
|
168
|
|
|
foreach ($commandersDirectories as $commanderDirectory) { |
169
|
|
|
if (is_file($commanderFilePath = $commanderDirectory . $commanderFilename)) { |
170
|
|
|
$routedCommands = array_diff($commands, $routedCommands); |
171
|
|
|
$commanderRegistry = new Router\DataStructures\Commander($commanderFilePath); |
172
|
|
|
break; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
if ($commanderRegistry instanceof Router\DataStructures\Commander) { |
177
|
|
|
$this->setCommander($commanderRegistry, $routedCommands); |
178
|
|
|
return true; |
179
|
|
|
break; |
|
|
|
|
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return false; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
// ------------------------------------------------------------------------ |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Router::getCommander |
190
|
|
|
* |
191
|
|
|
* Gets requested commander. |
192
|
|
|
* |
193
|
|
|
* @return \O2System\Kernel\Cli\Router\DataStructures\Commander |
194
|
|
|
*/ |
195
|
|
|
public function getCommander() |
196
|
|
|
{ |
197
|
|
|
return $this->commander; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// ------------------------------------------------------------------------ |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Router::setCommander |
204
|
|
|
* |
205
|
|
|
* Sets requested commander. |
206
|
|
|
* |
207
|
|
|
* @param \O2System\Kernel\Cli\Router\DataStructures\Commander $commander |
208
|
|
|
* @param array $commands |
209
|
|
|
*/ |
210
|
|
|
final protected function setCommander(Router\DataStructures\Commander $commander, array $commands = []) |
211
|
|
|
{ |
212
|
|
|
// Add Commander PSR4 Namespace |
213
|
|
|
loader()->addNamespace($commander->getNamespaceName(), $commander->getFileInfo()->getPath()); |
|
|
|
|
214
|
|
|
|
215
|
|
|
$commanderMethod = 'execute'; |
216
|
|
|
if(count($commands)) { |
217
|
|
|
$commanderMethod = camelcase(reset($commands)); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
if($commander->hasMethod('route')) { |
221
|
|
|
$commander |
222
|
|
|
->setRequestMethod('route') |
223
|
|
|
->setRequestMethodArgs([$commanderMethod]); |
224
|
|
|
} elseif($commander->hasMethod($commanderMethod)) { |
225
|
|
|
$commander->setRequestMethod($commanderMethod); |
226
|
|
|
} elseif($commander->hasMethod('execute')) { |
227
|
|
|
$commander->setRequestMethod('execute'); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// Set Router Commander |
231
|
|
|
$this->commander = $commander; |
232
|
|
|
} |
233
|
|
|
} |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: