|
1
|
|
|
<?php |
|
2
|
|
|
namespace keeko\tools\command; |
|
3
|
|
|
|
|
4
|
|
|
use keeko\framework\schema\PackageSchema; |
|
5
|
|
|
use keeko\tools\helpers\IOHelper; |
|
6
|
|
|
use keeko\tools\helpers\ServiceLoaderTrait; |
|
7
|
|
|
use keeko\tools\services\CommandService; |
|
8
|
|
|
use phootwork\lang\Text; |
|
9
|
|
|
use Psr\Log\LogLevel; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
|
|
17
|
|
|
abstract class AbstractGenerateCommand extends Command { |
|
18
|
|
|
|
|
19
|
|
|
use ServiceLoaderTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** @var PackageSchema */ |
|
22
|
|
|
protected $package; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct($name = null) { |
|
25
|
|
|
parent::__construct($name); |
|
26
|
20 |
|
} |
|
27
|
20 |
|
|
|
28
|
20 |
|
/* (non-PHPdoc) |
|
29
|
|
|
* @see \Symfony\Component\Console\Command\Command::initialize() |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) { |
|
32
|
|
|
// io |
|
33
|
20 |
|
$io = new IOHelper(); |
|
34
|
|
|
$io->setInput($input); |
|
35
|
20 |
|
$io->setOutput($output); |
|
36
|
20 |
|
$this->getHelperSet()->set($io); |
|
37
|
20 |
|
|
|
38
|
20 |
|
// logger |
|
39
|
|
|
$logger = new ConsoleLogger($output, [ |
|
40
|
|
|
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE, |
|
41
|
20 |
|
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, |
|
42
|
20 |
|
]); |
|
43
|
20 |
|
|
|
44
|
20 |
|
// services |
|
45
|
|
|
$service = new CommandService($this, $logger); |
|
46
|
|
|
$this->loadServices($service); |
|
47
|
20 |
|
$this->package = $service->getPackageService()->getPackage(); |
|
48
|
20 |
|
} |
|
49
|
20 |
|
|
|
50
|
20 |
|
/** |
|
51
|
|
|
* @return CommandService |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function getService() { |
|
54
|
|
|
return $this->service; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
protected function configure() { |
|
59
|
|
|
$this->configureGlobalOptions(); |
|
60
|
20 |
|
} |
|
61
|
20 |
|
|
|
62
|
20 |
|
protected function configureGenerateOptions() { |
|
63
|
|
|
$this |
|
64
|
20 |
|
->addOption( |
|
65
|
20 |
|
'schema', |
|
66
|
20 |
|
's', |
|
67
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
68
|
20 |
|
'Path to the database schema (if ommited, database/schema.xml is used)', |
|
69
|
20 |
|
null |
|
70
|
20 |
|
) |
|
71
|
|
|
->addOption( |
|
72
|
20 |
|
'force', |
|
73
|
20 |
|
'f', |
|
74
|
20 |
|
InputOption::VALUE_NONE, |
|
75
|
20 |
|
'Forces to owerwrite' |
|
76
|
20 |
|
) |
|
77
|
20 |
|
; |
|
78
|
|
|
} |
|
79
|
20 |
|
|
|
80
|
|
|
protected function configureGlobalOptions() { |
|
81
|
20 |
|
$this |
|
82
|
|
|
->addOption( |
|
83
|
20 |
|
'workdir', |
|
84
|
20 |
|
'w', |
|
85
|
20 |
|
InputOption::VALUE_OPTIONAL, |
|
86
|
20 |
|
'Specify the working directory (if ommited, current working directory is used)', |
|
87
|
20 |
|
null |
|
88
|
20 |
|
) |
|
89
|
20 |
|
; |
|
90
|
|
|
} |
|
91
|
20 |
|
|
|
92
|
|
|
protected function runCommand($name, array $input = []) { |
|
93
|
20 |
|
// return whether command has already executed |
|
94
|
|
|
$app = $this->getApplication(); |
|
95
|
|
|
$cmd = $app->find($name); |
|
96
|
|
|
|
|
97
|
|
|
$input = new ArrayInput($this->sanitizeInput($input)); |
|
98
|
|
|
$input->setInteractive(false); |
|
99
|
|
|
|
|
100
|
|
|
$cmd->run($input, $this->io->getOutput()); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
private function sanitizeInput($input) { |
|
|
|
|
|
|
104
|
|
|
// check if at least one argument is present and if not add a blank one |
|
105
|
|
|
$hasArgs = false; |
|
106
|
|
|
foreach (array_keys($input) as $key) { |
|
107
|
|
|
if (!Text::create($key)->startsWith('--')) { |
|
108
|
|
|
$hasArgs = true; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
if (!$hasArgs) { |
|
112
|
|
|
$input[] = ''; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $input; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.