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 $verboseVerbose = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool |
86
|
|
|
*/ |
87
|
|
|
protected $verboseDebug = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var int |
91
|
|
|
*/ |
92
|
|
|
const VV = OutputInterface::VERBOSITY_VERBOSE; |
93
|
|
|
const VVV = OutputInterface::VERBOSITY_DEBUG; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Main files. |
97
|
|
|
*/ |
98
|
|
|
const MAIN = 'packages.json'; |
99
|
|
|
const DOT = '.packages.json'; |
100
|
1 |
|
const INIT = '.init'; |
101
|
|
|
const TO = 'p'; |
102
|
1 |
|
|
103
|
1 |
|
/** |
104
|
1 |
|
* {@inheritdoc} |
105
|
1 |
|
*/ |
106
|
1 |
|
protected function configure() |
107
|
|
|
{ |
108
|
1 |
|
$this->addOption( |
109
|
|
|
'no-progress', |
110
|
|
|
null, |
111
|
|
|
InputOption::VALUE_NONE, |
112
|
|
|
"Don't show progress bar" |
113
|
1 |
|
); |
114
|
|
|
} |
115
|
1 |
|
|
116
|
1 |
|
/** |
117
|
1 |
|
* {@inheritdoc} |
118
|
1 |
|
*/ |
119
|
|
|
protected function initialize(InputInterface $input, OutputInterface $output) |
120
|
|
|
{ |
121
|
|
|
$this->input = $input; |
122
|
|
|
$this->output = $output; |
123
|
|
|
$this->verboseVerbose = $this->output->getVerbosity() == self::VV; |
124
|
|
|
$this->verboseDebug = $this->output->getVerbosity() == self::VVV; |
125
|
|
|
} |
126
|
1 |
|
|
127
|
|
|
/** |
128
|
1 |
|
* Inicialize with custom |
129
|
|
|
* |
130
|
|
|
* @param InputInterface $input |
131
|
|
|
* @param OutputInterface $output |
132
|
|
|
*/ |
133
|
1 |
|
public function init(InputInterface $input, OutputInterface $output) |
134
|
|
|
{ |
135
|
1 |
|
if (isset($this->input) && isset($this->output)) { |
136
|
|
|
return $this; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
// Only when direct call by tests |
140
|
|
|
$this->initialize($input, $output); |
141
|
1 |
|
|
142
|
|
|
return $this; |
143
|
1 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function isVerbose():bool |
149
|
|
|
{ |
150
|
|
|
return $this->verboseVerbose; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
/** |
154
|
|
|
* @return bool |
155
|
1 |
|
*/ |
156
|
|
|
public function isDebug():bool |
157
|
1 |
|
{ |
158
|
|
|
return $this->verboseDebug; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Add a progress bar. |
163
|
|
|
* |
164
|
|
|
* @param IProgressBar $progressBar |
165
|
|
|
* |
166
|
|
|
* @return Base |
167
|
1 |
|
*/ |
168
|
|
|
public function setProgressBar(IProgressBar $progressBar):Base |
169
|
1 |
|
{ |
170
|
|
|
$this->progressBar = $progressBar; |
171
|
1 |
|
|
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Add a fileSystem. |
177
|
|
|
* |
178
|
|
|
* @param Filesystem $fileSystem |
179
|
|
|
* |
180
|
|
|
* @return Base |
181
|
1 |
|
*/ |
182
|
|
|
public function setFilesystem(Filesystem $filesystem):Base |
183
|
1 |
|
{ |
184
|
|
|
$this->filesystem = $filesystem; |
185
|
1 |
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Add a http. |
191
|
|
|
* |
192
|
|
|
* @param Http $http |
193
|
|
|
* |
194
|
|
|
* @return Base |
195
|
1 |
|
*/ |
196
|
|
|
public function setHttp(Http $http):Base |
197
|
1 |
|
{ |
198
|
|
|
$this->http = $http; |
199
|
1 |
|
|
200
|
|
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Add a provider. |
205
|
|
|
* |
206
|
|
|
* @param Provider $provider |
207
|
|
|
* |
208
|
|
|
* @return Base |
209
|
1 |
|
*/ |
210
|
|
|
public function setProvider(Provider $provider):Base |
211
|
1 |
|
{ |
212
|
|
|
$this->provider = $provider; |
213
|
1 |
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Add a packages. |
219
|
|
|
* |
220
|
|
|
* @param Package $package |
221
|
|
|
* |
222
|
|
|
* @return Base |
223
|
|
|
*/ |
224
|
|
|
public function setPackage(Package $package):Base |
225
|
|
|
{ |
226
|
|
|
$this->package = $package; |
227
|
|
|
|
228
|
|
|
return $this; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param int $exit |
233
|
|
|
* |
234
|
|
|
* @return Base |
235
|
|
|
*/ |
236
|
|
|
protected function setExitCode(int $exit):Base |
237
|
|
|
{ |
238
|
|
|
$this->exitCode = $exit; |
239
|
1 |
|
|
240
|
|
|
return $this; |
241
|
1 |
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return int |
245
|
|
|
*/ |
246
|
|
|
protected function getExitCode():int |
247
|
|
|
{ |
248
|
|
|
return $this->stop() ? $this->exitCode : 0; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @return bool |
253
|
|
|
*/ |
254
|
|
|
protected function stop():bool |
255
|
|
|
{ |
256
|
|
|
return isset($this->exitCode); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|