1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains class YapealSetup. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2016 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @author Michael Cummings <[email protected]> |
32
|
|
|
* @copyright 2016 Michael Cummings |
33
|
|
|
* @license LGPL-3.0+ |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\Cli\Yapeal; |
36
|
|
|
|
37
|
|
|
use Symfony\Component\Console\Command\Command; |
38
|
|
|
use Symfony\Component\Console\Helper\QuestionHelper; |
39
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
40
|
|
|
use Symfony\Component\Console\Input\InputOption; |
41
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
42
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
43
|
|
|
use Yapeal\Cli\ConfigFileTrait; |
44
|
|
|
use Yapeal\Cli\VerbosityToStrategyTrait; |
45
|
|
|
use Yapeal\CommonToolsTrait; |
46
|
|
|
use Yapeal\Container\ContainerInterface; |
47
|
|
|
use Yapeal\Event\EveApiEventEmitterTrait; |
48
|
|
|
use Yapeal\Event\YEMAwareInterface; |
49
|
|
|
use Yapeal\Sql\CommonSqlQueries; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Class YapealSetup. |
53
|
|
|
*/ |
54
|
|
|
class YapealSetup extends Command implements YEMAwareInterface |
55
|
|
|
{ |
56
|
|
|
use CommonToolsTrait, ConfigFileTrait, EveApiEventEmitterTrait, VerbosityToStrategyTrait; |
57
|
|
|
/** |
58
|
|
|
* @param string $name |
59
|
|
|
* @param ContainerInterface $dic |
60
|
|
|
* |
61
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
62
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
63
|
|
|
*/ |
64
|
|
|
public function __construct(string $name, ContainerInterface $dic) |
65
|
|
|
{ |
66
|
|
|
$this->setDescription('Used to setup Yapeal-ng after Composer require or to interactively do so any time'); |
67
|
|
|
$this->setName($name); |
68
|
|
|
$this->setDic($dic); |
69
|
|
|
parent::__construct($name); |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* Configures the current command. |
73
|
|
|
*/ |
74
|
|
|
public function configure() |
75
|
|
|
{ |
76
|
|
|
$help = <<<'HELP' |
77
|
|
|
The <info>%command.name%</info> command is an interactive command that can be used |
78
|
|
|
to setup Yapeal-ng in an application after being add to the application's |
79
|
|
|
`composer.json` file. It will analyze where it is being run from and then ask |
80
|
|
|
interactive questions and use their responses to customize an yapeal.yaml file |
81
|
|
|
with their settings. It can also be used to run some of the additional console |
82
|
|
|
commands if desired like Schema:Creator or Schema:Update etc. |
83
|
|
|
HELP; |
84
|
|
|
$this->addOption('askInteractive', |
85
|
|
|
'a|i', |
86
|
|
|
InputOption::VALUE_NONE, |
87
|
|
|
'Ask interactive questions instead of using multiple choice menus'); |
88
|
|
|
$this->addConfigFileOption(); |
89
|
|
|
$this->setHelp($help); |
90
|
|
|
} |
91
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
92
|
|
|
/** |
93
|
|
|
* Executes the current command. |
94
|
|
|
* |
95
|
|
|
* This method is not abstract because you can use this class |
96
|
|
|
* as a concrete class. In this case, instead of defining the |
97
|
|
|
* execute() method, you set the code to execute by passing |
98
|
|
|
* a Closure to the setCode() method. |
99
|
|
|
* |
100
|
|
|
* @param InputInterface $input An InputInterface instance |
101
|
|
|
* @param OutputInterface $output An OutputInterface instance |
102
|
|
|
* |
103
|
|
|
* @return int 0 if everything went fine, or an error code |
104
|
|
|
* @throws \DomainException |
105
|
|
|
* @throws \InvalidArgumentException |
106
|
|
|
* @throws \LogicException When this abstract method is not implemented |
107
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
108
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
109
|
|
|
* @throws \Symfony\Component\Console\Exception\RuntimeException |
110
|
|
|
* @throws \Yapeal\Exception\YapealException |
111
|
|
|
* |
112
|
|
|
* @see setCode() |
113
|
|
|
*/ |
114
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int |
115
|
|
|
{ |
116
|
|
|
if (!$input->isInteractive()) { |
117
|
|
|
$mess = sprintf('<comment>%1$s can not be used in a non-interactive way be sure not to use' |
118
|
|
|
. ' the -n, --no-interaction option</comment>', |
119
|
|
|
$this->getName()); |
120
|
|
|
$output->writeln($mess); |
121
|
|
|
return 1; |
122
|
|
|
} |
123
|
|
|
$this->setLogThresholdFromVerbosity($output); |
124
|
|
|
$options = $input->getOptions(); |
125
|
|
|
if (!empty($options['configFile'])) { |
126
|
|
|
$this->processConfigFile($options['configFile'], $this->getDic()); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
if (!$options['askInteractive']) { |
129
|
|
|
$this->mainMenu($input, $output); |
130
|
|
|
} else { |
131
|
|
|
$this->startInteractive($input, $output); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
return 0; |
134
|
|
|
} |
135
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection */ |
136
|
|
|
/** |
137
|
|
|
* @param string $option |
138
|
|
|
* @param string $text |
139
|
|
|
*/ |
140
|
|
|
private function addMenuChoice(string $option, string $text) |
141
|
|
|
{ |
142
|
|
|
$this->menuChoices[$option] = $text; |
143
|
|
|
} |
144
|
|
|
/** |
145
|
|
|
* @return string[] |
146
|
|
|
* @throws \LogicException |
147
|
|
|
*/ |
148
|
|
|
private function getMenuChoices(): array |
149
|
|
|
{ |
150
|
|
|
if (null === $this->menuChoices) { |
151
|
|
|
$mess = 'Trying to access menuChoices before it was set'; |
152
|
|
|
throw new \LogicException($mess); |
153
|
|
|
} |
154
|
|
|
return $this->menuChoices; |
155
|
|
|
} |
156
|
|
|
/** |
157
|
|
|
* @return bool |
158
|
|
|
* @throws \LogicException |
159
|
|
|
*/ |
160
|
|
|
private function hasConfigFile(): bool |
161
|
|
|
{ |
162
|
|
|
if (null === $this->configFile) { |
163
|
|
|
$this->configFile = false; |
164
|
|
|
$dic = $this->getDic(); |
165
|
|
|
/** @noinspection PhpParamsInspection */ |
166
|
|
|
if (array_key_exists('Yapeal.vendorParentDir', $dic)) { |
167
|
|
|
$appConfigFile = $dic['Yapeal.vendorParentDir'] . 'config/yapeal.yaml'; |
168
|
|
|
$this->configFile = is_readable($appConfigFile) && is_file($appConfigFile); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
return $this->configFile; |
172
|
|
|
} |
173
|
|
|
/** |
174
|
|
|
* @return bool |
175
|
|
|
* @throws \LogicException |
176
|
|
|
*/ |
177
|
|
|
private function hasSchema(): bool |
178
|
|
|
{ |
179
|
|
|
if (null === $this->schema) { |
180
|
|
|
$this->schema = true; |
181
|
|
|
/** |
182
|
|
|
* @var ContainerInterface|array $dic |
183
|
|
|
* @var \PDO $pdo |
184
|
|
|
* @var CommonSqlQueries $csq |
185
|
|
|
*/ |
186
|
|
|
$dic = $this->getDic(); |
187
|
|
|
$result = false; |
188
|
|
|
$sql = $this->getCsq() |
189
|
|
|
->getSchemaNames(); |
190
|
|
|
try { |
191
|
|
|
$pdo = $dic['Yapeal.Sql.Connection']; |
192
|
|
|
$result = $pdo->query($sql); |
193
|
|
|
} catch (\PDOException $exc) { |
194
|
|
|
$this->schema = false; |
195
|
|
|
} |
196
|
|
|
if (false === $result) { |
197
|
|
|
$this->schema = false; |
198
|
|
|
} |
199
|
|
|
$result = array_values($result->fetchAll(\PDO::FETCH_COLUMN)); |
200
|
|
|
$schemaName = $dic['Yapeal.Sql.Platforms.' . $dic['Yapeal.Sql.platform'] . '.schema']; |
201
|
|
|
if (!in_array($schemaName, $result, false)) { |
202
|
|
|
$this->schema = false; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
return $this->schema; |
206
|
|
|
} |
207
|
|
|
/** |
208
|
|
|
* @param InputInterface $input |
209
|
|
|
* @param OutputInterface $output |
210
|
|
|
* |
211
|
|
|
* @throws \LogicException |
212
|
|
|
* @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
213
|
|
|
* @throws \Symfony\Component\Console\Exception\LogicException |
214
|
|
|
* @throws \Symfony\Component\Console\Exception\RuntimeException |
215
|
|
|
*/ |
216
|
|
|
private function mainMenu(InputInterface $input, OutputInterface $output) |
217
|
|
|
{ |
218
|
|
|
$this->resetMenu(); |
219
|
|
|
$this->addMenuChoice('1', 'Switch from menus to asking interactive questions'); |
220
|
|
|
$text = 'Create/edit application yapeal.yaml file'; |
221
|
|
|
$this->addMenuChoice('2', $text); |
222
|
|
|
$text = 'Create/update Yapeal-ng schema'; |
223
|
|
|
$this->addMenuChoice('3', $text); |
224
|
|
|
$this->addMenuChoice('x', 'Exit command'); |
225
|
|
|
$prompt = 'What do you want to do?(x)'; |
226
|
|
|
/** |
227
|
|
|
* @var QuestionHelper $question |
228
|
|
|
*/ |
229
|
|
|
$question = $this->getHelper('question'); |
230
|
|
|
$choice = new ChoiceQuestion($prompt, $this->getMenuChoices(), 'x'); |
231
|
|
|
$answer = $question->ask($input, $output, $choice); |
232
|
|
|
switch ($answer) { |
233
|
|
|
case 'x': |
234
|
|
|
break; |
235
|
|
|
case '1': // Switch from menus to asking interactive questions |
236
|
|
|
break; |
237
|
|
|
case '2': // Create/update yapeal.yaml file |
238
|
|
|
$this->yamlMenu($input, $output); |
239
|
|
|
break; |
240
|
|
|
case '3': // Create/update schema |
241
|
|
|
break; |
242
|
|
|
default: |
243
|
|
|
break; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
private function resetMenu() |
247
|
|
|
{ |
248
|
|
|
$this->menuChoices = []; |
249
|
|
|
} |
250
|
|
|
/** |
251
|
|
|
* @param InputInterface $input |
252
|
|
|
* @param OutputInterface $output |
253
|
|
|
*/ |
254
|
|
|
private function startInteractive(InputInterface $input, OutputInterface $output) |
|
|
|
|
255
|
|
|
{ |
256
|
|
|
} |
257
|
|
|
/** |
258
|
|
|
* @param InputInterface $input |
259
|
|
|
* @param OutputInterface $output |
260
|
|
|
* |
261
|
|
|
* @throws \LogicException |
262
|
|
|
*/ |
263
|
|
|
private function yamlMenu(InputInterface $input, OutputInterface $output) |
|
|
|
|
264
|
|
|
{ |
265
|
|
|
if ($this->hasConfigFile()) { |
266
|
|
|
$ycf = new YamlConfigFile($this->getDic()['Yapeal.vendorParentDir'] . 'config'); |
267
|
|
|
$ycf->read(); |
268
|
|
|
$this->yamlFile = $ycf->getSettings(); |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
/** |
272
|
|
|
* @var bool $configFile |
273
|
|
|
*/ |
274
|
|
|
private $configFile; |
275
|
|
|
/** |
276
|
|
|
* @var string[] $menuChoices |
277
|
|
|
*/ |
278
|
|
|
private $menuChoices; |
279
|
|
|
/** |
280
|
|
|
* @var bool $schema |
281
|
|
|
*/ |
282
|
|
|
private $schema; |
283
|
|
|
/** |
284
|
|
|
* @var array $yamlFile |
285
|
|
|
*/ |
286
|
|
|
private $yamlFile; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.