|
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\Output\OutputInterface; |
|
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
20
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
|
21
|
|
|
|
|
22
|
|
|
abstract class AbstractCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
const FOLDER_RESOURCES = '/../../app/Resources/config/'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var InputInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $oInput; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var OutputInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $oOutput; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ContainerBuilder |
|
38
|
|
|
*/ |
|
39
|
|
|
private static $oContainer; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $sHomeDir = ''; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Executes the current command. |
|
48
|
|
|
* |
|
49
|
|
|
* This method is not abstract because you can use this class |
|
50
|
|
|
* as a concrete class. In this case, instead of defining the |
|
51
|
|
|
* execute() method, you set the code to execute by passing |
|
52
|
|
|
* a Closure to the setCode() method. |
|
53
|
|
|
* |
|
54
|
|
|
* @param InputInterface $oInput An InputInterface instance |
|
55
|
|
|
* @param OutputInterface $oOutput An OutputInterface instance |
|
56
|
|
|
* |
|
57
|
|
|
* @return integer null or 0 if everything went fine, or an error code |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \LogicException When this abstract method is not implemented |
|
60
|
|
|
* |
|
61
|
|
|
* @see setCode() |
|
62
|
|
|
*/ |
|
63
|
10 |
|
protected function execute(InputInterface $oInput, OutputInterface $oOutput) |
|
64
|
|
|
{ |
|
65
|
10 |
|
$this->oInput = $oInput; |
|
66
|
10 |
|
$this->oOutput = $oOutput; |
|
67
|
|
|
|
|
68
|
10 |
|
if (!$this->isAppRunable()) |
|
69
|
10 |
|
{ |
|
70
|
|
|
return 1; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// set output for verbosity handling |
|
74
|
|
|
/** @var \Symfony\Bridge\Monolog\Handler\ConsoleHandler $_oConsoleHandler */ |
|
75
|
10 |
|
$_oConsoleHandler = $this->getContainer()->get('ConsoleHandler'); |
|
76
|
10 |
|
$_oConsoleHandler->setOutput($this->oOutput); |
|
77
|
|
|
|
|
78
|
10 |
|
return $this->process(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return int |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract protected function process(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return ContainerBuilder |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getContainer() |
|
90
|
|
|
{ |
|
91
|
|
|
if (is_null(self::$oContainer)) |
|
92
|
|
|
{ |
|
93
|
|
|
$_oContainer = new ContainerBuilder(); |
|
94
|
|
|
|
|
95
|
|
|
// load local parameters |
|
96
|
|
View Code Duplication |
if (file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml')) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator($this->getHomeDir())); |
|
99
|
|
|
$_oLoader->load('parameters.yml'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// load optional parameter in the current working directory |
|
103
|
|
View Code Duplication |
if (file_exists($this->getWorkingDir() . DIRECTORY_SEPARATOR . '.chapiconfig')) |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator($this->getWorkingDir())); |
|
106
|
|
|
$_oLoader->load('.chapiconfig'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// load services |
|
110
|
|
|
$_oLoader = new YamlFileLoader($_oContainer, new FileLocator(__DIR__ . self::FOLDER_RESOURCES)); |
|
111
|
|
|
$_oLoader->load('services.yml'); |
|
112
|
|
|
|
|
113
|
|
|
self::$oContainer = $_oContainer; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return self::$oContainer; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
3 |
|
protected function isAppRunable() |
|
123
|
|
|
{ |
|
124
|
|
|
if ( |
|
125
|
3 |
|
!file_exists($this->getHomeDir() . DIRECTORY_SEPARATOR . 'parameters.yml') |
|
126
|
3 |
|
&& !file_exists($this->getWorkingDir() . DIRECTORY_SEPARATOR . '.chapiconfig') |
|
127
|
1 |
|
) // one file have to exist |
|
128
|
3 |
|
{ |
|
129
|
|
|
$this->oOutput->writeln(sprintf( |
|
130
|
|
|
'<error>%s</error>', |
|
131
|
|
|
'No parameter file found. Please run "configure" command for initial setup or add a local `.chapiconfig` to your working directory.' |
|
132
|
|
|
)); |
|
133
|
|
|
return false; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
3 |
|
return true; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
1 |
|
protected function getHomeDir() |
|
143
|
|
|
{ |
|
144
|
1 |
|
if (!empty(self::$sHomeDir)) |
|
145
|
1 |
|
{ |
|
146
|
1 |
|
return self::$sHomeDir; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
1 |
|
$_sHomeDir = getenv('CHAPI_HOME'); |
|
150
|
1 |
|
if (!$_sHomeDir) |
|
151
|
1 |
|
{ |
|
152
|
1 |
|
$_sHomeDir = CommandUtils::getOsHomeDir() . DIRECTORY_SEPARATOR . '.chapi'; |
|
153
|
1 |
|
} |
|
154
|
|
|
|
|
155
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sHomeDir); |
|
156
|
|
|
|
|
157
|
1 |
|
return self::$sHomeDir = $_sHomeDir; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
1 |
|
protected function getCacheDir() |
|
164
|
|
|
{ |
|
165
|
1 |
|
$_sCacheDir = $this->getHomeDir() . DIRECTORY_SEPARATOR . 'cache'; |
|
166
|
1 |
|
CommandUtils::hasCreateDirectoryIfNotExists($_sCacheDir); |
|
167
|
|
|
|
|
168
|
1 |
|
return $_sCacheDir; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
protected function getWorkingDir() |
|
175
|
|
|
{ |
|
176
|
|
|
return getcwd(); |
|
177
|
|
|
} |
|
178
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.