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 AbstractKeekoCommand 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
|
|
|
} |
27
|
|
|
|
28
|
|
|
/* (non-PHPdoc) |
29
|
|
|
* @see \Symfony\Component\Console\Command\Command::initialize() |
30
|
|
|
*/ |
31
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) { |
32
|
|
|
// io |
33
|
|
|
$io = new IOHelper(); |
34
|
|
|
$io->setInput($input); |
35
|
|
|
$io->setOutput($output); |
36
|
|
|
$this->getHelperSet()->set($io); |
37
|
|
|
|
38
|
|
|
// logger |
39
|
|
|
$logger = new ConsoleLogger($output, [ |
40
|
|
|
LogLevel::NOTICE => OutputInterface::VERBOSITY_VERBOSE, |
41
|
|
|
LogLevel::INFO => OutputInterface::VERBOSITY_VERBOSE, |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
// services |
45
|
|
|
$service = new CommandService($this, $logger); |
46
|
|
|
$this->loadServices($service); |
47
|
|
|
$this->package = $service->getPackageService()->getPackage(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return CommandService |
52
|
|
|
*/ |
53
|
|
|
public function getService() { |
54
|
|
|
// this is BULLSHIT!! |
55
|
|
|
// this should never be public |
56
|
|
|
// this is only because of the UI classes, some need access to the model service |
57
|
|
|
// see https://github.com/keeko/tools/issues/5 how this could be made more useful |
58
|
|
|
return $this->service; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
protected function configure() { |
63
|
|
|
$this->configureGlobalOptions(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function configureGenerateOptions() { |
67
|
|
|
$this |
68
|
|
|
->addOption( |
69
|
|
|
'schema', |
70
|
|
|
's', |
71
|
|
|
InputOption::VALUE_OPTIONAL, |
72
|
|
|
'Path to the database schema (if ommited, database/schema.xml is used)', |
73
|
|
|
null |
74
|
|
|
) |
75
|
|
|
->addOption( |
76
|
|
|
'force', |
77
|
|
|
'f', |
78
|
|
|
InputOption::VALUE_NONE, |
79
|
|
|
'Forces to owerwrite' |
80
|
|
|
) |
81
|
|
|
; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function configureGlobalOptions() { |
85
|
|
|
$this |
86
|
|
|
->addOption( |
87
|
|
|
'workdir', |
88
|
|
|
'w', |
89
|
|
|
InputOption::VALUE_OPTIONAL, |
90
|
|
|
'Specify the working directory (if ommited, current working directory is used)', |
91
|
|
|
null |
92
|
|
|
) |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function runCommand($name, array $input = []) { |
97
|
|
|
// return whether command has already executed |
98
|
|
|
$app = $this->getApplication(); |
99
|
|
|
$cmd = $app->find($name); |
100
|
|
|
|
101
|
|
|
$input = new ArrayInput($this->sanitizeInput($input)); |
102
|
|
|
$input->setInteractive(false); |
103
|
|
|
|
104
|
|
|
$cmd->run($input, $this->io->getOutput()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Prepares input as used for running a command |
109
|
|
|
* |
110
|
|
|
* @param array $input |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
private function sanitizeInput(array $input) { |
114
|
|
|
// check if at least one argument is present and if not add a blank one |
115
|
|
|
$hasArgs = false; |
116
|
|
|
foreach (array_keys($input) as $key) { |
117
|
|
|
if (!Text::create($key)->startsWith('--')) { |
118
|
|
|
$hasArgs = true; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
if (!$hasArgs) { |
122
|
|
|
$input[] = ''; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $input; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |