-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCollection.php
155 lines (120 loc) · 4.91 KB
/
Collection.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
<?php
/**
* JBZoo Toolbox - Composer-Graph.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Composer-Graph
*/
declare(strict_types=1);
namespace JBZoo\ComposerGraph;
use JBZoo\Data\JSON;
use function JBZoo\Data\json;
class Collection
{
private JSON $composerFile;
private JSON $lockFile;
/** @var Package[] */
private array $collection = [];
private ?string $vendorDir;
public function __construct(JSON $composerFile, JSON $lockFile, ?string $vendorDir = null)
{
$this->composerFile = $composerFile;
$this->lockFile = $lockFile;
$this->vendorDir = $vendorDir;
$this->buildCollection();
}
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function buildCollection(): void
{
/** @phpstan-ignore-next-line */
$istTest = \defined('\IS_PHPUNIT_TEST') && IS_PHPUNIT_TEST;
$this->add('php', [
'version' => $istTest ? null : \PHP_VERSION,
'tags' => [Package::PHP, Package::HAS_META],
]);
$this->add((string)$this->composerFile->get('name'), [
'version' => $istTest ? null : Helper::getGitVersion(),
'require' => $this->composerFile->get('require'),
'require-dev' => $this->composerFile->get('require-dev'),
'suggest' => $this->composerFile->get('suggest'),
'tags' => [Package::MAIN, Package::HAS_META],
]);
$mainRequire = \array_keys((array)$this->composerFile->get('require'));
foreach ($mainRequire as $package) {
$this->add((string)$package, ['tags' => [Package::DIRECT]]);
}
$mainRequireDev = \array_keys((array)$this->composerFile->get('require-dev'));
foreach ($mainRequireDev as $packageDev) {
$this->add((string)$packageDev, ['tags' => [Package::DIRECT]]);
}
$mainSuggest = \array_keys((array)$this->composerFile->get('suggest'));
foreach ($mainSuggest as $suggest) {
$this->add((string)$suggest, ['tags' => [Package::DIRECT, Package::SUGGEST]]);
}
// Lock file
$scopes = [
Package::REQUIRED => (array)$this->lockFile->get('packages'),
Package::REQUIRED_DEV => (array)$this->lockFile->get('packages-dev'),
];
foreach ($scopes as $scopeType => $scope) {
foreach ($scope as $package) {
$package = json($package);
$require = (array)$package->get('require');
$suggest = (array)$package->get('suggest');
$version = $package->getString('version');
$version = $package->findString("extra.branch-alias.{$version}", $version);
$this->add((string)$package->get('name'), [
'version' => $version,
'require' => $require,
'suggest' => $suggest,
'tags' => [$scopeType, Package::HAS_META],
]);
foreach (\array_keys($require) as $innerRequired) {
$this->add((string)$innerRequired, ['tags' => [$scopeType]]);
}
foreach (\array_keys($suggest) as $innerSuggested) {
$this->add((string)$innerSuggested, ['tags' => [$scopeType, Package::SUGGEST]]);
}
}
}
}
public function getMain(): Package
{
foreach ($this->collection as $package) {
if ($package->isMain()) {
return $package;
}
}
throw new Exception('Main package not found');
}
public function getByName(string $packageName): Package
{
$packageAlias = Package::alias($packageName);
if (\array_key_exists($packageAlias, $this->collection)) {
return $this->collection[$packageAlias];
}
throw new Exception("Package \"{$packageName} ({$packageAlias})\" not found in collection");
}
private function add(string $packageName, array $packageMeta): void
{
$current = json($packageMeta);
$packageAlias = Package::alias($packageName);
/** @var Package $package */
$package = $this->collection[$packageAlias] ?? new Package($packageName, $this->vendorDir);
$package
->setVersion((string)$current->get('version'))
->addRequire((array)$current->get('require'))
->addRequireDev((array)$current->get('require-dev'))
->addSuggest((array)$current->get('suggest'))
->addTags((array)$current->get('tags'));
$this->collection[$packageAlias] = $package;
}
}