|
1
|
|
|
<?php namespace FreedomCore\TrinityCore\Console\Abstracts; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class BaseCommand |
|
5
|
|
|
* @package FreedomCore\TrinityCore\Console\Abstracts |
|
6
|
|
|
*/ |
|
7
|
|
|
abstract class BaseCommand |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Client Instance Object |
|
12
|
|
|
* @var null|\SoapClient |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $clientInstance = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Name of the command |
|
18
|
|
|
* @var null|string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $command = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Available methods |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $methods = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Commands which do not need to be prefixed with BASE command |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $doNotPrefix = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Commands which methods should be concatenated |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $concatenate = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* BaseCommand constructor. |
|
42
|
|
|
* @param \SoapClient $client |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct(\SoapClient $client) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->clientInstance = $client; |
|
47
|
|
|
$this->prepareCommand(); |
|
48
|
|
|
$this->prepareMethods(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Get Command Name |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getCommandName() : string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->command; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get Command Methods |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getCommandMethods() : array |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->methods; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Execute Help Command |
|
71
|
|
|
* @param string $methodName |
|
72
|
|
|
* @return array |
|
73
|
|
|
* @codeCoverageIgnore |
|
74
|
|
|
*/ |
|
75
|
|
|
public function help(string $methodName = '') |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->processOutput($this->clientInstance->executeCommand(new \SoapParam(trim(sprintf('help %s %s', $this->command, $methodName)), 'command')), true); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Add quotes to string |
|
82
|
|
|
* @param string $string |
|
83
|
|
|
* @return string |
|
84
|
|
|
*/ |
|
85
|
|
|
public function inQuotes(string $string) : string |
|
86
|
|
|
{ |
|
87
|
|
|
return '"' . $string . '"'; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Prepare Command Name Variable |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function prepareCommand() |
|
94
|
|
|
{ |
|
95
|
|
|
$this->command = strtolower((new \ReflectionClass(get_called_class()))->getShortName()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Prepare Available Methods For Specified Command |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function prepareMethods() |
|
102
|
|
|
{ |
|
103
|
|
|
$globalMethods = get_class_methods(get_called_class()); |
|
104
|
|
|
$classMethods = array_diff($globalMethods, [ |
|
105
|
|
|
'__construct', |
|
106
|
|
|
'prepareCommand', |
|
107
|
|
|
'prepareMethods', |
|
108
|
|
|
'generateCommand', |
|
109
|
|
|
'generateQueryString', |
|
110
|
|
|
'executeCommand', |
|
111
|
|
|
'processOutput', |
|
112
|
|
|
'help', |
|
113
|
|
|
'inQuotes', |
|
114
|
|
|
'parseCommand', |
|
115
|
|
|
'getCommandName', |
|
116
|
|
|
'getCommandMethods' |
|
117
|
|
|
]); |
|
118
|
|
|
foreach ($classMethods as $method) { |
|
119
|
|
|
$command = $this->generateCommand($method); |
|
120
|
|
|
$this->methods[$method] = [ |
|
121
|
|
|
'command' => $command, |
|
122
|
|
|
'query' => $this->generateQueryString(get_called_class(), $method) |
|
123
|
|
|
]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Generate Command String |
|
129
|
|
|
* @param string $method |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function generateCommand(string $method) : string |
|
133
|
|
|
{ |
|
134
|
|
|
preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $matches); |
|
135
|
|
|
$elements = array_map('strtolower', $matches[0]); |
|
136
|
|
|
$command = (!in_array($method, $this->doNotPrefix)) ? implode(' ', array_merge([$this->command], $elements)) : implode(' ', $elements); |
|
137
|
|
|
if (in_array($method, $this->concatenate)) { |
|
138
|
|
|
$command = $this->command . $method; |
|
139
|
|
|
} |
|
140
|
|
|
return trim($this->parseCommand($command)); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Generate Command Query String |
|
145
|
|
|
* @param string $class |
|
146
|
|
|
* @param string $method |
|
147
|
|
|
* @return string |
|
148
|
|
|
*/ |
|
149
|
|
|
protected function generateQueryString(string $class, string $method) : string |
|
150
|
|
|
{ |
|
151
|
|
|
$reflection = new \ReflectionMethod($class, $method); |
|
152
|
|
|
return implode(' ', array_map(function ($item) { |
|
153
|
|
|
return '%' . $item->getName() . '%'; |
|
154
|
|
|
}, $reflection->getParameters())); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Execute Command |
|
159
|
|
|
* @param string $methodName |
|
160
|
|
|
* @param array $parameters |
|
161
|
|
|
* @return array|string |
|
162
|
|
|
* @codeCoverageIgnore |
|
163
|
|
|
*/ |
|
164
|
|
|
protected function executeCommand(string $methodName, array $parameters) |
|
165
|
|
|
{ |
|
166
|
|
|
$structure = [ |
|
167
|
|
|
'class' => get_called_class(), |
|
168
|
|
|
'method' => $methodName, |
|
169
|
|
|
'parameters' => $parameters, |
|
170
|
|
|
'query' => $this->methods[$methodName] |
|
171
|
|
|
]; |
|
172
|
|
|
$structure['query']['prepared'] = str_replace(' ', ' ', trim(implode(' ', [ |
|
173
|
|
|
$structure['query']['command'], |
|
174
|
|
|
str_replace(explode(' ', $structure['query']['query']), $structure['parameters'], $structure['query']['query']) |
|
175
|
|
|
]))); |
|
176
|
|
|
try { |
|
177
|
|
|
return $this->processOutput( |
|
178
|
|
|
$this->clientInstance->executeCommand(new \SoapParam(trim($structure['query']['prepared']), 'command')) |
|
179
|
|
|
); |
|
180
|
|
|
} catch (\SoapFault $exception) { |
|
181
|
|
|
return [ |
|
182
|
|
|
'status' => 'failed', |
|
183
|
|
|
'code' => $exception->getCode(), |
|
184
|
|
|
'message' => trim($exception->getMessage()), |
|
185
|
|
|
'query' => trim($structure['query']['prepared']) |
|
186
|
|
|
]; |
|
187
|
|
|
} |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Process Response |
|
192
|
|
|
* @param string $commandOutput |
|
193
|
|
|
* @param bool $helpFunction |
|
194
|
|
|
* @return array |
|
195
|
|
|
* @codeCoverageIgnore |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function processOutput(string $commandOutput, bool $helpFunction = false) |
|
|
|
|
|
|
198
|
|
|
{ |
|
199
|
|
|
return array_filter(explode(PHP_EOL, $commandOutput)); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Parse and Process Command |
|
204
|
|
|
* @param string $command |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function parseCommand(string $command) : string |
|
208
|
|
|
{ |
|
209
|
|
|
$replacements = [ |
|
210
|
|
|
'gm level' => 'gmlevel', |
|
211
|
|
|
'game account create' => 'gameaccountcreate', |
|
212
|
|
|
'list game accounts' => 'listgameaccounts', |
|
213
|
|
|
'diff time' => 'difftime', |
|
214
|
|
|
'log level' => 'loglevel', |
|
215
|
|
|
'save all' => 'saveall', |
|
216
|
|
|
'name announce' => 'nameannounce', |
|
217
|
|
|
'change faction' => 'changefaction', |
|
218
|
|
|
'change race' => 'changerace' |
|
219
|
|
|
]; |
|
220
|
|
|
foreach ($replacements as $key => $value) { |
|
221
|
|
|
if (strstr($command, $key)) { |
|
222
|
|
|
$command = str_replace($key, $value, $command); |
|
223
|
|
|
break; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
return $command; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.