1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace drupol\ComposerPackages\Exporter; |
6
|
|
|
|
7
|
|
|
use Composer\Package\Loader\ArrayLoader; |
8
|
|
|
use Composer\Package\PackageInterface; |
9
|
|
|
|
10
|
|
|
class Dependencies extends Exporter |
11
|
|
|
{ |
12
|
|
|
public function exportToArray(): array |
13
|
|
|
{ |
14
|
|
|
$data = $this->getEvent()->getComposer()->getLocker()->getLockData(); |
15
|
2 |
|
|
16
|
|
|
$packagesData = array_merge( |
17
|
2 |
|
$data['packages'], |
18
|
|
|
$data['packages-dev'] |
19
|
2 |
|
); |
20
|
2 |
|
|
21
|
2 |
|
$packageDeps = []; |
22
|
|
|
|
23
|
|
|
foreach ($packagesData as $package) { |
24
|
2 |
|
$package = (new ArrayLoader())->load($package); |
25
|
|
|
$packageName = $package->getName(); |
26
|
2 |
|
$packageDeps += [$packageName => []]; |
27
|
2 |
|
$this->getDependenciesOf($packageDeps[$package->getName()], $package); |
28
|
2 |
|
|
29
|
2 |
|
$packageDeps[$packageName] = array_values($packageDeps[$packageName]); |
30
|
2 |
|
} |
31
|
|
|
|
32
|
2 |
|
$regex = $this->buildRegex($packageDeps); |
33
|
|
|
|
34
|
|
|
return compact('packageDeps', 'regex'); |
35
|
2 |
|
} |
36
|
|
|
|
37
|
|
|
protected function getDependenciesOf(array &$carry, PackageInterface $package): void |
38
|
|
|
{ |
39
|
|
|
foreach (array_keys($package->getRequires()) as $key) { |
40
|
|
|
if ('php' === $key) { |
41
|
|
|
continue; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
2 |
|
if (0 === mb_strpos((string) $key, 'ext-')) { |
45
|
2 |
|
continue; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
$carry += [$key => $key]; |
49
|
2 |
|
} |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
private function buildRegex($packages): array |
53
|
2 |
|
{ |
54
|
|
|
$groups = []; |
55
|
2 |
|
|
56
|
|
|
foreach ($packages as $package => $dependencies) { |
57
|
|
|
[$prefix, $bundle] = explode('/', $package); |
58
|
|
|
$groups[sprintf('(?i:%s)(?|', $prefix)][] = sprintf('/?(?i:%s) (*MARK:%s)|', str_replace('-', '-?', $bundle), $package); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return $groups; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|