1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Packagist Mirror. |
7
|
|
|
* |
8
|
|
|
* For the full license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webs\Mirror\Command; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Webs\Mirror\ShortName; |
19
|
|
|
use Webs\Mirror\IProgressBar; |
20
|
|
|
use Webs\Mirror\Filesystem; |
21
|
|
|
use Webs\Mirror\Http; |
22
|
|
|
use Webs\Mirror\Provider; |
23
|
|
|
use Webs\Mirror\Package; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Base command. |
27
|
|
|
* |
28
|
|
|
* @author Webysther Nunes <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Base extends Command |
31
|
|
|
{ |
32
|
|
|
use ShortName; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $initialized = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var InputInterface |
41
|
|
|
*/ |
42
|
|
|
protected $input; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var OutputInterface |
46
|
|
|
*/ |
47
|
|
|
protected $output; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var IProgressBar |
51
|
|
|
*/ |
52
|
|
|
protected $progressBar; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Filesystem |
56
|
|
|
*/ |
57
|
|
|
protected $filesystem; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Http |
61
|
|
|
*/ |
62
|
|
|
protected $http; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Provider |
66
|
|
|
*/ |
67
|
|
|
protected $provider; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var Package |
71
|
|
|
*/ |
72
|
|
|
protected $package; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var int |
76
|
|
|
*/ |
77
|
|
|
protected $exitCode; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var bool |
81
|
|
|
*/ |
82
|
|
|
protected $verbose = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
protected $verboseVerbose = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var bool |
91
|
|
|
*/ |
92
|
|
|
protected $verboseDebug = false; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var int |
96
|
|
|
*/ |
97
|
|
|
const V = OutputInterface::VERBOSITY_VERBOSE; |
98
|
|
|
const VV = OutputInterface::VERBOSITY_VERY_VERBOSE; |
99
|
|
|
const VVV = OutputInterface::VERBOSITY_DEBUG; |
100
|
1 |
|
|
101
|
|
|
/** |
102
|
1 |
|
* Main files. |
103
|
1 |
|
*/ |
104
|
1 |
|
const MAIN = 'packages.json'; |
105
|
1 |
|
const DOT = '.packages.json'; |
106
|
1 |
|
const INIT = '.init'; |
107
|
|
|
const TO = 'p'; |
108
|
1 |
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
protected function configure() |
113
|
1 |
|
{ |
114
|
|
|
$this->addOption( |
115
|
1 |
|
'no-progress', |
116
|
1 |
|
null, |
117
|
1 |
|
InputOption::VALUE_NONE, |
118
|
1 |
|
"Don't show progress bar" |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
126
|
1 |
|
{ |
127
|
|
|
$this->input = $input; |
128
|
1 |
|
$this->output = $output; |
129
|
|
|
$this->verbose = $this->output->getVerbosity() == self::V; |
130
|
|
|
$this->verboseVerbose = $this->output->getVerbosity() == self::VV; |
131
|
|
|
$this->verboseDebug = $this->output->getVerbosity() == self::VVV; |
132
|
|
|
} |
133
|
1 |
|
|
134
|
|
|
/** |
135
|
1 |
|
* Inicialize with custom |
136
|
|
|
* |
137
|
|
|
* @param InputInterface $input |
138
|
|
|
* @param OutputInterface $output |
139
|
|
|
*/ |
140
|
|
|
public function init(InputInterface $input, OutputInterface $output) |
141
|
1 |
|
{ |
142
|
|
|
if (isset($this->input) && isset($this->output)) { |
143
|
1 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
// Only when direct call by tests |
147
|
|
|
$this->initialize($input, $output); |
148
|
|
|
|
149
|
|
|
return $this; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
1 |
|
* @return bool |
154
|
|
|
*/ |
155
|
1 |
|
public function isVerbose():bool |
156
|
|
|
{ |
157
|
1 |
|
return $this->verbose || $this->isVeryVerbose(); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function isVeryVerbose():bool |
164
|
|
|
{ |
165
|
|
|
return $this->verboseVerbose || $this->isDebug(); |
166
|
|
|
} |
167
|
1 |
|
|
168
|
|
|
/** |
169
|
1 |
|
* @return bool |
170
|
|
|
*/ |
171
|
1 |
|
public function isDebug():bool |
172
|
|
|
{ |
173
|
|
|
return $this->verboseDebug; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Add a progress bar. |
178
|
|
|
* |
179
|
|
|
* @param IProgressBar $progressBar |
180
|
|
|
* |
181
|
1 |
|
* @return Base |
182
|
|
|
*/ |
183
|
1 |
|
public function setProgressBar(IProgressBar $progressBar):Base |
184
|
|
|
{ |
185
|
1 |
|
$this->progressBar = $progressBar; |
186
|
|
|
|
187
|
|
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Add a fileSystem. |
192
|
|
|
* |
193
|
|
|
* @param Filesystem $fileSystem |
194
|
|
|
* |
195
|
1 |
|
* @return Base |
196
|
|
|
*/ |
197
|
1 |
|
public function setFilesystem(Filesystem $filesystem):Base |
198
|
|
|
{ |
199
|
1 |
|
$this->filesystem = $filesystem; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Add a http. |
206
|
|
|
* |
207
|
|
|
* @param Http $http |
208
|
|
|
* |
209
|
1 |
|
* @return Base |
210
|
|
|
*/ |
211
|
1 |
|
public function setHttp(Http $http):Base |
212
|
|
|
{ |
213
|
1 |
|
$this->http = $http; |
214
|
|
|
|
215
|
|
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Add a provider. |
220
|
|
|
* |
221
|
|
|
* @param Provider $provider |
222
|
|
|
* |
223
|
|
|
* @return Base |
224
|
|
|
*/ |
225
|
|
|
public function setProvider(Provider $provider):Base |
226
|
|
|
{ |
227
|
|
|
$this->provider = $provider; |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Add a packages. |
234
|
|
|
* |
235
|
|
|
* @param Package $package |
236
|
|
|
* |
237
|
|
|
* @return Base |
238
|
|
|
*/ |
239
|
1 |
|
public function setPackage(Package $package):Base |
240
|
|
|
{ |
241
|
1 |
|
$this->package = $package; |
242
|
|
|
|
243
|
|
|
return $this; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* @param int $exit |
248
|
|
|
* |
249
|
|
|
* @return Base |
250
|
|
|
*/ |
251
|
|
|
protected function setExitCode(int $exit):Base |
252
|
|
|
{ |
253
|
|
|
$this->exitCode = $exit; |
254
|
|
|
|
255
|
|
|
return $this; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return int |
260
|
|
|
*/ |
261
|
|
|
protected function getExitCode():int |
262
|
|
|
{ |
263
|
|
|
return $this->stop() ? $this->exitCode : 0; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return bool |
268
|
|
|
*/ |
269
|
|
|
protected function stop():bool |
270
|
|
|
{ |
271
|
|
|
return isset($this->exitCode); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|