1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-21 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
namespace Chapi\Commands; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use Chapi\Component\Command\CommandUtils; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\Console\Command\Command; |
17
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
18
|
|
|
use Symfony\Component\Console\Input\InputOption; |
19
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
20
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
21
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
22
|
|
|
|
23
|
|
|
abstract class AbstractCommand extends Command |
24
|
|
|
{ |
25
|
|
|
const FOLDER_RESOURCES = '/../../app/Resources/config/'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var InputInterface |
29
|
|
|
*/ |
30
|
|
|
protected $oInput; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var OutputInterface |
34
|
|
|
*/ |
35
|
|
|
protected $oOutput; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var ContainerBuilder |
39
|
|
|
*/ |
40
|
|
|
private $oContainer; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private static $sHomeDir = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
25 |
|
public function __construct($name = null) |
51
|
|
|
{ |
52
|
25 |
|
parent::__construct($name); |
53
|
|
|
|
54
|
|
|
// setup default --profile option for all commands |
55
|
25 |
|
$this->addOption('profile', null, InputOption::VALUE_OPTIONAL, 'Use a specific profile from your config file.'); |
56
|
25 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritdoc |
60
|
|
|
*/ |
61
|
20 |
|
protected function initialize(InputInterface $oInput, OutputInterface $oOutput) |
62
|
|
|
{ |
63
|
20 |
|
$this->oInput = $oInput; |
|
|
|
|
64
|
20 |
|
$this->oOutput = $oOutput; |
65
|
|
|
|
66
|
20 |
|
parent::initialize($oInput, $oOutput); |
67
|
20 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Executes the current command. |
71
|
|
|
* |
72
|
|
|
* This method is not abstract because you can use this class |
73
|
|
|
* as a concrete class. In this case, instead of defining the |
74
|
|
|
* execute() method, you set the code to execute by passing |
75
|
|
|
* a Closure to the setCode() method. |
76
|
|
|
* |
77
|
|
|
* @param InputInterface $oInput An InputInterface instance |
78
|
|
|
* @param OutputInterface $oOutput An OutputInterface instance |
79
|
|
|
* |
80
|
|
|
* @return integer null or 0 if everything went fine, or an error code |
81
|
|
|
* |
82
|
|
|
* @throws \LogicException When this abstract method is not implemented |
83
|
|
|
* |
84
|
|
|
* @see setCode() |
85
|
|
|
*/ |
86
|
14 |
|
protected function execute(InputInterface $oInput, OutputInterface $oOutput) |
87
|
|
|
{ |
88
|
14 |
|
if (!$this->isAppRunable()) |
89
|
|
|
{ |
90
|
|
|
return 1; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// set output for verbosity handling |
94
|
|
|
/** @var \Symfony\Bridge\Monolog\Handler\ConsoleHandler $_oConsoleHandler */ |
95
|
14 |
|
$_oConsoleHandler = $this->getContainer()->get('ConsoleHandler'); |
96
|
14 |
|
$_oConsoleHandler->setOutput($this->oOutput); |
97
|
|
|
|
98
|
14 |
|
return $this->process(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
abstract protected function process(); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return ContainerBuilder |
108
|
|
|
*/ |
109
|
|
|
protected function getContainer() |
110
|
|
|
{ |
111
|
|
|
if (is_null($this->oContainer)) |
112
|
|
|
{ |
113
|
|
|
$_oContainer = new ContainerBuilder(); |
114
|
|
|
|
115
|
|
|
// load local parameters |
116
|
|
|
$this->loadParameterConfig($this->getHomeDir(), $this->getParameterFileName(), $_oContainer); |
117
|
|
|
|
118
|
|
|
// load optional parameter in the current working directory |
119
|
|
|
$this->loadParameterConfig($this->getWorkingDir(), '.chapiconfig', $_oContainer); |
120
|
|
|
|
121
|
|
|
// load services |
122
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator(__DIR__ . self::FOLDER_RESOURCES)); |
123
|
|
|
$_oLoader->load('services.yml'); |
124
|
|
|
|
125
|
|
|
$this->oContainer = $_oContainer; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $this->oContainer; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
6 |
|
protected function getParameterFileName() |
135
|
|
|
{ |
136
|
6 |
|
$_sProfile = $this->oInput->getOption('profile'); |
137
|
6 |
|
return (is_string($_sProfile)) |
138
|
1 |
|
? sprintf('parameters_%s.yml', $_sProfile) |
139
|
6 |
|
: 'parameters.yml'; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
3 |
|
protected function isAppRunable() |
146
|
|
|
{ |
147
|
|
|
// one file have to exist |
148
|
|
|
if ( |
149
|
3 |
|
!file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml') |
150
|
3 |
|
&& !file_exists($this->getWorkingDir() . DIRECTORY_SEPARATOR . '.chapiconfig') |
151
|
|
|
) |
152
|
|
|
{ |
153
|
|
|
$this->oOutput->writeln(sprintf( |
154
|
|
|
'<error>%s</error>', |
155
|
|
|
'No parameter file found. Please run "configure" command for initial setup or add a local `.chapiconfig` to your working directory.' |
156
|
|
|
)); |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
3 |
|
return true; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return string |
165
|
|
|
*/ |
166
|
1 |
|
protected function getHomeDir() |
167
|
|
|
{ |
168
|
1 |
|
if (!empty(self::$sHomeDir)) |
169
|
|
|
{ |
170
|
1 |
|
return self::$sHomeDir; |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
$_sHomeDir = getenv('CHAPI_HOME'); |
174
|
1 |
|
if (!$_sHomeDir) |
175
|
|
|
{ |
176
|
1 |
|
$_sHomeDir = CommandUtils::getOsHomeDir() . DIRECTORY_SEPARATOR . '.chapi'; |
177
|
|
|
} |
178
|
|
|
|
179
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sHomeDir); |
180
|
|
|
|
181
|
1 |
|
return self::$sHomeDir = $_sHomeDir; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
1 |
|
protected function getCacheDir() |
188
|
|
|
{ |
189
|
1 |
|
$_sCacheDir = $this->getHomeDir() . DIRECTORY_SEPARATOR . 'cache'; |
190
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sCacheDir); |
191
|
|
|
|
192
|
1 |
|
return $_sCacheDir; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @return string |
197
|
|
|
*/ |
198
|
|
|
protected function getWorkingDir() |
199
|
|
|
{ |
200
|
|
|
return getcwd(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $sPath |
205
|
|
|
* @param string $sFile |
206
|
|
|
* @param ContainerBuilder $oContainer |
207
|
|
|
*/ |
208
|
|
|
private function loadParameterConfig($sPath, $sFile, $oContainer) |
209
|
|
|
{ |
210
|
|
|
// load local parameters |
211
|
|
|
if (file_exists($sPath . DIRECTORY_SEPARATOR . $sFile)) |
212
|
|
|
{ |
213
|
|
|
$_oLoader = new YamlFileLoader($oContainer, new FileLocator($sPath)); |
214
|
|
|
$_oLoader->load($sFile); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.