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