-
Notifications
You must be signed in to change notification settings - Fork 513
/
PackageSelection.php
464 lines (395 loc) · 15.7 KB
/
PackageSelection.php
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<?php
/*
* This file is part of composer/satis.
*
* (c) Composer <https://github.com/composer>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Composer\Satis\PackageSelection;
use Composer\Composer;
use Composer\DependencyResolver\Pool;
use Composer\Json\JsonFile;
use Composer\Package\AliasPackage;
use Composer\Package\BasePackage;
use Composer\Package\Link;
use Composer\Package\Loader\ArrayLoader;
use Composer\Package\PackageInterface;
use Composer\Repository\ComposerRepository;
use Composer\Repository\ConfigurableRepositoryInterface;
use Composer\Repository\PlatformRepository;
use Composer\Repository\RepositoryInterface;
use Composer\Semver\Constraint\MultiConstraint;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Builds the Packages list.
*
* @author James Hautot <james@rezo.net>
*/
class PackageSelection
{
/** @var OutputInterface The output Interface. */
protected $output;
/** @var bool Skips Exceptions if true. */
protected $skipErrors;
/** @var string packages.json file name. */
private $filename;
/** @var bool Selects All Packages if true. */
private $requireAll;
/** @var bool Add required dependencies if true. */
private $requireDependencies;
/** @var bool required dev-dependencies if true. */
private $requireDevDependencies;
/** @var string Minimum stability accepted for Packages in the list. */
private $minimumStability;
/** @var array The active package filter to merge. */
private $packagesFilter = array();
/** @var string The active repository filter to merge. */
private $repositoryFilter;
/** @var array The selected packages from config */
private $selected = array();
/** @var array A list of packages marked as abandoned */
private $abandoned = array();
/** @var string The homepage - needed to get the relative paths of the providers */
private $homepage;
/**
* Base Constructor.
*
* @param OutputInterface $output The output Interface
* @param string $outputDir The directory where to build
* @param array $config The parameters from ./satis.json
* @param bool $skipErrors Escapes Exceptions if true
*/
public function __construct(OutputInterface $output, $outputDir, $config, $skipErrors)
{
$this->output = $output;
$this->skipErrors = (bool) $skipErrors;
$this->filename = $outputDir.'/packages.json';
$this->fetchOptions($config);
}
private function fetchOptions($config)
{
$this->requireAll = isset($config['require-all']) && true === $config['require-all'];
$this->requireDependencies = isset($config['require-dependencies']) && true === $config['require-dependencies'];
$this->requireDevDependencies = isset($config['require-dev-dependencies']) && true === $config['require-dev-dependencies'];
if (!$this->requireAll && !isset($config['require'])) {
$this->output->writeln('No explicit requires defined, enabling require-all');
$this->requireAll = true;
}
$this->minimumStability = isset($config['minimum-stability']) ? $config['minimum-stability'] : 'dev';
$this->abandoned = isset($config['abandoned']) ? $config['abandoned'] : array();
$this->homepage = isset($config['homepage']) ? $config['homepage'] : null;
}
/**
* Sets the active repository filter to merge
*
* @param string $repositoryFilter The active repository filter to merge
*/
public function setRepositoryFilter($repositoryFilter)
{
$this->repositoryFilter = $repositoryFilter;
}
/**
* Tells if repository list should be reduced to single repository
*
* @return bool true if repository filter is set
*/
public function hasRepositoryFilter()
{
return $this->repositoryFilter !== null;
}
/**
* Sets the active package filter to merge
*
* @param array $packagesFilter The active package filter to merge
*/
public function setPackagesFilter(array $packagesFilter = array())
{
$this->packagesFilter = $packagesFilter;
}
/**
* Tells if there is at least one package filter.
*
* @return bool true if there is at least one package filter
*/
public function hasFilterForPackages()
{
return count($this->packagesFilter) > 0;
}
/**
* Sets the list of packages to build.
*
* @param Composer $composer The Composer instance
* @param bool $verbose Output infos if true
*
* @throws \InvalidArgumentException
* @throws \Exception
*
* @return PackageInterface[]
*/
public function select(Composer $composer, $verbose)
{
// run over all packages and store matching ones
$this->output->writeln('<info>Scanning packages</info>');
$repos = $composer->getRepositoryManager()->getRepositories();
$pool = new Pool($this->minimumStability);
if ($this->hasRepositoryFilter()) {
$repos = $this->filterRepositories($repos);
}
foreach ($repos as $repo) {
try {
$pool->addRepository($repo);
} catch (\Exception $exception) {
if (!$this->skipErrors) {
throw $exception;
}
$this->output->writeln(sprintf("<error>Skipping Exception '%s'.</error>", $exception->getMessage()));
}
}
if ($this->hasRepositoryFilter()) {
if (count($repos) === 0) {
throw new \InvalidArgumentException(sprintf('Specified repository url "%s" does not exist.', $this->repositoryFilter));
} elseif (count($repos) > 1) {
throw new \InvalidArgumentException(sprintf('Found more than one repository for url "%s".', $this->repositoryFilter));
}
}
$links = $this->requireAll ? $this->getAllLinks($repos, $this->minimumStability, $verbose) : $this->getFilteredLinks($composer);
// process links if any
$depsLinks = array();
$i = 0;
while (isset($links[$i])) {
$link = $links[$i];
++$i;
$name = $link->getTarget();
$matches = $pool->whatProvides($name, $link->getConstraint(), true);
foreach ($matches as $index => $package) {
// skip aliases
if ($package instanceof AliasPackage) {
$package = $package->getAliasOf();
}
// add matching package if not yet selected
if (!isset($this->selected[$package->getUniqueName()])) {
if ($verbose) {
$this->output->writeln('Selected '.$package->getPrettyName().' ('.$package->getPrettyVersion().')');
}
$this->selected[$package->getUniqueName()] = $package;
if (!$this->requireAll) {
$required = $this->getRequired($package);
// append non-platform dependencies
foreach ($required as $dependencyLink) {
$target = $dependencyLink->getTarget();
if (!preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $target)) {
$linkId = $target.' '.$dependencyLink->getConstraint();
// prevent loading multiple times the same link
if (!isset($depsLinks[$linkId])) {
$links[] = $dependencyLink;
$depsLinks[$linkId] = true;
}
}
}
}
}
}
if (!$matches) {
$this->output->writeln('<error>The '.$name.' '.$link->getPrettyConstraint().' requirement did not match any package</error>');
}
}
$this->setSelectedAsAbandoned();
ksort($this->selected, SORT_STRING);
return $this->selected;
}
/**
* Loads previously dumped Packages in order to merge with updates.
*
* @return PackageInterface[]
*/
public function load()
{
$packages = array();
$repoJson = new JsonFile($this->filename);
$dirName = dirname($this->filename);
if ($repoJson->exists()) {
$loader = new ArrayLoader();
$packagesJson = $repoJson->read();
$jsonIncludes = isset($packagesJson['includes']) && is_array($packagesJson['includes'])
? $packagesJson['includes']
: array();
if (isset($packagesJson['providers']) && is_array($packagesJson['providers']) && isset($packagesJson['providers-url'])) {
$baseUrl = $this->homepage ? parse_url(rtrim($this->homepage, '/'), PHP_URL_PATH) . '/' : null;
$baseUrlLength = strlen($baseUrl);
foreach ($packagesJson['providers'] as $packageName => $provider) {
$file = str_replace(array('%package%', '%hash%'), array($packageName, $provider['sha256']), $packagesJson['providers-url']);
if ($baseUrl && substr($file, 0, $baseUrlLength) === $baseUrl) {
$file = substr($file, $baseUrlLength);
}
$jsonIncludes[$file] = $provider;
}
}
foreach ($jsonIncludes as $includeFile => $includeConfig) {
$includeJson = new JsonFile($dirName.'/'.$includeFile);
if (!$includeJson->exists()) {
$this->output->writeln(sprintf(
'<error>File \'%s\' does not exist, defined in "includes" in \'%s\'</error>',
$includeJson->getPath(),
$repoJson->getPath()
));
continue;
}
$jsonPackages = $includeJson->read();
$jsonPackages = isset($jsonPackages['packages']) && is_array($jsonPackages['packages'])
? $jsonPackages['packages']
: array();
foreach ($jsonPackages as $jsonPackage) {
if (is_array($jsonPackage)) {
foreach ($jsonPackage as $jsonVersion) {
if (is_array($jsonVersion)) {
if (isset($jsonVersion['name']) && in_array($jsonVersion['name'], $this->packagesFilter)) {
continue;
}
$package = $loader->load($jsonVersion);
$packages[$package->getUniqueName()] = $package;
}
}
}
}
}
}
return $packages;
}
/**
* Marks selected packages as abandoned by Configuration file
*/
private function setSelectedAsAbandoned()
{
foreach ($this->selected as $name => $package) {
if (array_key_exists($package->getName(), $this->abandoned)) {
$package->setAbandoned($this->abandoned[$package->getName()]);
}
}
}
/**
* Gets a list of filtered Links.
*
* @param Composer $composer The Composer instance
*
* @return Link[]
*/
private function getFilteredLinks(Composer $composer)
{
$links = array_values($composer->getPackage()->getRequires());
// only pick up packages in our filter, if a filter has been set.
if ($this->hasFilterForPackages()) {
$packagesFilter = $this->packagesFilter;
$links = array_filter($links, function (Link $link) use ($packagesFilter) {
return in_array($link->getTarget(), $packagesFilter);
});
}
return array_values($links);
}
/**
* Gets all Links.
*
* This method is called when 'require-all' is set to true.
*
* @param array $repos List of all Repositories configured
* @param string $minimumStability The minimum stability each package must have to be selected
* @param bool $verbose Output infos if true
*
* @return Link[]
*/
private function getAllLinks($repos, $minimumStability, $verbose)
{
$links = array();
foreach ($repos as $repo) {
// collect links for composer repos with providers
if ($repo instanceof ComposerRepository && $repo->hasProviders()) {
foreach ($repo->getProviderNames() as $name) {
$links[] = new Link('__root__', $name, new MultiConstraint(array()), 'requires', '*');
}
} else {
$packages = $this->getPackages($repo);
foreach ($packages as $package) {
// skip aliases
if ($package instanceof AliasPackage) {
continue;
}
if (BasePackage::$stabilities[$package->getStability()] > BasePackage::$stabilities[$minimumStability]) {
if ($verbose) {
$this->output->writeln('Skipped '.$package->getPrettyName().' ('.$package->getStability().')');
}
continue;
}
// add matching package if not yet selected
if (!isset($this->selected[$package->getUniqueName()])) {
if ($verbose) {
$this->output->writeln('Selected '.$package->getPrettyName().' ('.$package->getPrettyVersion().')');
}
$this->selected[$package->getUniqueName()] = $package;
}
}
}
}
return $links;
}
/**
* Gets All or filtered Packages of a Repository.
*
* @param RepositoryInterface $repo a Repository
*
* @return PackageInterface[]
*/
private function getPackages(RepositoryInterface $repo)
{
$packages = array();
if ($this->hasFilterForPackages()) {
// apply package filter if defined
foreach ($this->packagesFilter as $filter) {
$packages += $repo->findPackages($filter);
}
} else {
// process other repos directly
$packages = $repo->getPackages();
}
return $packages;
}
/**
* Gets the required Links if needed.
*
* @param PackageInterface $package A package
*
* @return Link[]
*/
private function getRequired(PackageInterface $package)
{
$required = array();
if ($this->requireDependencies) {
$required = $package->getRequires();
}
if ($this->requireDevDependencies) {
$required = array_merge($required, $package->getDevRequires());
}
return $required;
}
/**
* Filter given repositories.
*
* @param RepositoryInterface[] $repositories
*
* @return RepositoryInterface[]
*/
private function filterRepositories(array $repositories)
{
$url = $this->repositoryFilter;
return array_filter($repositories, function ($repository) use ($url) {
if (!($repository instanceof ConfigurableRepositoryInterface)) {
return false;
}
$config = $repository->getRepoConfig();
if (!isset($config['url']) || $config['url'] !== $url) {
return false;
}
return true;
});
}
}