1 | <?php namespace FreedomCore\TrinityCore\Console\Abstracts; |
||
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 | 24 | public function __construct(\SoapClient $client) |
|
45 | { |
||
46 | 24 | $this->clientInstance = $client; |
|
47 | 24 | $this->prepareCommand(); |
|
48 | 24 | $this->prepareMethods(); |
|
49 | 24 | } |
|
50 | |||
51 | /** |
||
52 | * Get Command Name |
||
53 | * @return string |
||
54 | */ |
||
55 | 2 | public function getCommandName() : string |
|
56 | { |
||
57 | 2 | return $this->command; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Get Command Methods |
||
62 | * @return array |
||
63 | */ |
||
64 | 2 | public function getCommandMethods() : array |
|
65 | { |
||
66 | 2 | 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 | 2 | public function inQuotes(string $string) : string |
|
86 | { |
||
87 | 2 | return '"' . $string . '"'; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Prepare Command Name Variable |
||
92 | */ |
||
93 | 24 | protected function prepareCommand() |
|
94 | { |
||
95 | 24 | $this->command = strtolower((new \ReflectionClass(get_called_class()))->getShortName()); |
|
96 | 24 | } |
|
97 | |||
98 | /** |
||
99 | * Prepare Available Methods For Specified Command |
||
100 | */ |
||
101 | 24 | protected function prepareMethods() |
|
102 | { |
||
103 | 24 | $globalMethods = get_class_methods(get_called_class()); |
|
104 | 24 | $classMethods = array_diff($globalMethods, [ |
|
105 | 24 | '__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 | 24 | foreach ($classMethods as $method) { |
|
119 | 24 | $command = $this->generateCommand($method); |
|
120 | 24 | $this->methods[$method] = [ |
|
121 | 24 | 'command' => $command, |
|
122 | 24 | 'query' => $this->generateQueryString(get_called_class(), $method) |
|
123 | ]; |
||
124 | } |
||
125 | 24 | } |
|
126 | |||
127 | /** |
||
128 | * Generate Command String |
||
129 | * @param string $method |
||
130 | * @return string |
||
131 | */ |
||
132 | 24 | protected function generateCommand(string $method) : string |
|
133 | { |
||
134 | 24 | preg_match_all('/((?:^|[A-Z])[a-z]+)/', $method, $matches); |
|
135 | 24 | $elements = array_map('strtolower', $matches[0]); |
|
136 | 24 | $command = (!in_array($method, $this->doNotPrefix)) ? implode(' ', array_merge([$this->command], $elements)) : implode(' ', $elements); |
|
137 | 24 | if (in_array($method, $this->concatenate)) { |
|
138 | 8 | $command = $this->command . $method; |
|
139 | } |
||
140 | 24 | 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 | 24 | protected function generateQueryString(string $class, string $method) : string |
|
150 | { |
||
151 | 24 | $reflection = new \ReflectionMethod($class, $method); |
|
152 | 24 | return implode(' ', array_map(function ($item) { |
|
153 | 24 | return '%' . $item->getName() . '%'; |
|
154 | 24 | }, $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) |
||
201 | |||
202 | /** |
||
203 | * Parse and Process Command |
||
204 | * @param string $command |
||
205 | * @return string |
||
206 | */ |
||
207 | 24 | protected function parseCommand(string $command) : string |
|
208 | { |
||
209 | $replacements = [ |
||
210 | 24 | 'gm level' => 'gmlevel', |
|
211 | 'game account create' => 'gameaccountcreate', |
||
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.