-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUpdate.php
More file actions
344 lines (302 loc) · 8.53 KB
/
Update.php
File metadata and controls
344 lines (302 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
namespace Winter\Packager\Commands;
use Winter\Packager\Composer;
use Winter\Packager\Exceptions\ComposerExceptionHandler;
use Winter\Packager\Parser\InstallOutputParser;
/**
* Update command.
*
* Runs "composer update" within PHP.
*
* @author Ben Thomson
* @since 0.1.0
*/
class Update extends BaseCommand
{
/**
* Package preference constants
*/
const PREFER_NONE = 'none';
const PREFER_DIST = 'dist';
const PREFER_SOURCE = 'source';
/**
* @var array<int, string> Raw output from Composer
*/
protected ?array $rawOutput;
/**
* Was the update successful?
*/
protected ?bool $successful;
/**
* @var array<string,array<string|int, string|array<int, string>>> Array of packages installed, upgraded or removed
*/
protected $packages = [
'installed' => [],
'upgraded' => [],
'removed' => [],
];
/**
* @var array<string,array<string|int, string|array<int, string>>> Array of packages locked, upgraded or removed in
* lock file
*/
protected $lockFile = [
'locked' => [],
'upgraded' => [],
'removed' => [],
];
/**
* @var array<int, string> Array of problems during update.
*/
protected $problems = [];
/**
* Command constructor.
*
* @param boolean $includeDev Include "require-dev" dependencies in the update.
* @param boolean $lockFileOnly Do a lockfile update only, do not install dependencies.
* @param boolean $ignorePlatformReqs Ignore platform reqs when running the update.
* @param string $installPreference Set an install preference - must be one of "none", "dist", "source"
* @param boolean $ignoreScripts Ignores scripts that run after Composer events.
* @param ?string $package Specify a specific package to update.
* @return void
*/
final public function __construct(
Composer $composer,
protected bool $includeDev = true,
protected bool $lockFileOnly = false,
protected bool $ignorePlatformReqs = false,
protected string $installPreference = 'none',
protected bool $ignoreScripts = false,
protected bool $dryRun = false,
protected ?string $package = null
) {
parent::__construct($composer);
if (!in_array($this->installPreference, [self::PREFER_NONE, self::PREFER_DIST, self::PREFER_SOURCE])) {
throw new \InvalidArgumentException(
'installPreference is not an allowed value `' . $this->installPreference . '`. See: "none", "dist", "source"'
);
}
}
/**
* Executes the command with the given options.
*
* @return static
*/
public function execute()
{
$output = $this->runComposerCommand();
if ($output['code'] !== 0) {
if (isset($output['exception'])) {
$exception = ComposerExceptionHandler::handle($output['exception'], $this);
throw new $exception['class'](
$exception['message'],
$exception['code'] ?? 0,
$exception['previous'] ?? null
);
}
}
$this->rawOutput = $output['output'];
$parser = new InstallOutputParser;
$parsed = $parser->parse($this->rawOutput);
$this->successful = !$parsed['conflicts'];
$this->lockFile = $parsed['lockFile'];
$this->packages = $parsed['packages'];
$this->problems = $parsed['problems'];
return $this;
}
/**
* Determines if the update was successful.
*
* @return boolean
*/
public function isSuccessful(): bool
{
return $this->successful === true;
}
/**
* Gets raw output from Composer.
*
* @return array<int, string>
*/
public function getRawOutput(): array
{
return $this->rawOutput;
}
/**
* Returns installed packages.
*
* Packages are returned as an array, with the package name as the key, and the installed version as the value.
*
* @return array<string, string>
*/
public function getInstalled(): array
{
return $this->packages['installed'];
}
/**
* Returns the count of installed packages.
*
* @return int
*/
public function getInstalledCount(): int
{
return count($this->getInstalled());
}
/**
* Returns upgraded packages.
*
* Packages are returned as an array, with the package name as the key. The value is also an array with two values,
* the previously installed version and the version that the package was updated to.
*
* @return array<string, array<int, string>>
*/
public function getUpgraded(): array
{
return $this->packages['upgraded'];
}
/**
* Returns the count of upgraded packages.
*
* @return int
*/
public function getUpgradedCount(): int
{
return count($this->getUpgraded());
}
/**
* Returns removed packages.
*
* Packages are returned as a simple array of package names that have been removed.
*
* @return array<int, string>
*/
public function getRemoved(): array
{
return $this->packages['removed'];
}
/**
* Returns the count of removed packages.
*
* @return int
*/
public function getRemovedCount(): int
{
return count($this->getRemoved());
}
/**
* Returns locked packages in the lock file.
*
* Packages are returned as an array, with the package name as the key, and the installed version as the value.
*
* @return array<string, string>
*/
public function getLockInstalled(): array
{
return $this->lockFile['locked'];
}
/**
* Returns the count of locked packages in the lock file.
*
* @return int
*/
public function getLockInstalledCount(): int
{
return count($this->getLockInstalled());
}
/**
* Returns upgraded packages in the lock file.
*
* Packages are returned as an array, with the package name as the key. The value is also an array with two values,
* the previously installed version and the version that the package was updated to.
*
* @return array<string, array<int, string>>
*/
public function getLockUpgraded(): array
{
return $this->lockFile['upgraded'];
}
/**
* Returns the count of upgraded packages in the lock file.
*
* @return int
*/
public function getLockUpgradedCount(): int
{
return count($this->getLockUpgraded());
}
/**
* Returns removed packages in the lock file.
*
* Packages are returned as a simple array of package names that have been removed.
*
* @return array<int, string>
*/
public function getLockRemoved(): array
{
return $this->lockFile['removed'];
}
/**
* Returns the count of removed packages in the lock file.
*
* @return int
*/
public function getLockRemovedCount(): int
{
return count($this->getLockRemoved());
}
/**
* Returns the problems encountered with the last update.
*
* The problems are returned as a simple array of strings.
*
* @return array<int, string>
*/
public function getProblems(): array
{
return $this->problems;
}
/**
* @inheritDoc
*/
protected function getCommandName(): string
{
return 'update';
}
/**
* @inheritDoc
*/
protected function requiresWorkDir(): bool
{
return true;
}
/**
* @inheritDoc
*/
protected function arguments(): array
{
$arguments = [];
if ($this->dryRun) {
$arguments['--dry-run'] = true;
}
if ($this->includeDev) {
$arguments['--dev'] = true;
} else {
$arguments['--no-dev'] = true;
}
if ($this->lockFileOnly) {
$arguments['--no-install'] = true;
}
if ($this->ignorePlatformReqs) {
$arguments['--ignore-platform-reqs'] = true;
}
if ($this->ignoreScripts) {
$arguments['--no-scripts'] = true;
}
if (in_array($this->installPreference, [self::PREFER_DIST, self::PREFER_SOURCE])) {
$arguments['--prefer-' . $this->installPreference] = true;
}
if ($this->package) {
$arguments['packages'] = [$this->package];
}
return $arguments;
}
}