-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathComposerFileParser.php
138 lines (115 loc) · 3.9 KB
/
ComposerFileParser.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
<?php
declare(strict_types=1);
namespace PhpAT\Parser;
class ComposerFileParser
{
private array $composerFile;
private array $lockFile;
private ?array $lockedPackages = null;
/**
* @throws \Exception
*/
public function parse(string $composerFilePath, string $lockFilePath): self
{
$this->composerFile = json_decode(file_get_contents($composerFilePath), true);
$this->lockFile = json_decode(file_get_contents($lockFilePath), true);
$this->lockedPackages = $this->getPackagesFromLockFile();
return $this;
}
/**
* Returns an array of all namespaces declared by the current composer file.
*
* @return array<string>
*/
public function getNamespaces(bool $dev = false): array
{
return $this->extractNamespaces($this->composerFile, $dev);
}
/**
* Returns an array of all required namespaces including deep dependencies (dependencies of dependencies)
*
* @return array<string>
*/
public function getDeepRequirementNamespaces(bool $dev): array
{
$required = $this->getDirectDependencies($dev);
$required = $this->flattenDependencies($required);
return $this->autoloadableNamespacesForRequirements($required);
}
/**
* Returns an array of directly required package names.
*
* @return array<string>
*/
public function getDirectDependencies(bool $dev): array
{
$required = [];
$key = $dev ? 'require-dev' : 'require';
foreach (array_keys($this->composerFile[$key] ?? []) as $packageName) {
$required[] = (string) $packageName;
}
return $required;
}
/**
* Resolves an array of package names to an array of namespaces declared by those packages.
*
* @param array<string> $requirements
* @return array<string>
*/
public function autoloadableNamespacesForRequirements(array $requirements)
{
foreach ($requirements as $package) {
$n = $this->extractNamespaces($this->lockedPackages[$package], false);
foreach ($n as $k => $v) {
if (empty($v)) {
unset($n[$k]);
}
}
$namespaces = array_merge($namespaces ?? [], $n);
}
return $namespaces ?? [];
}
private function flattenDependencies(array $topLevelRequirements): array
{
$required = [];
$toCheck = $topLevelRequirements;
while ($toCheck !== []) {
$packageName = array_pop($toCheck);
$package = $this->lockedPackages[ $packageName ] ?? null;
if ($package === null) {
continue;
}
$required[] = $packageName;
$deepRequirements = array_keys($package['require'] ?? []);
foreach ($deepRequirements as $name) {
if (!\in_array($name, $required, true)) {
$toCheck[] = $name;
}
}
}
return array_unique($required);
}
private function getPackagesFromLockFile(): array
{
$lockedPackages = [];
foreach ($this->lockFile['packages'] ?? [] as $package) {
$lockedPackages[ $package['name'] ] = $package;
}
foreach ($this->lockFile['packages-dev'] ?? [] as $package) {
$lockedPackages[ $package['name'] ] = $package;
}
return $lockedPackages;
}
private function extractNamespaces(array $package, bool $dev): array
{
$key = $dev ? 'autoload-dev' : 'autoload';
$namespaces = [];
foreach (array_keys($package[$key]['psr-0'] ?? []) as $namespace) {
$namespaces[] = (string) $namespace;
}
foreach (array_keys($package[$key]['psr-4'] ?? []) as $namespace) {
$namespaces[] = (string) $namespace;
}
return $namespaces;
}
}