1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ComposerRequireChecker\FileLocator; |
4
|
|
|
|
5
|
|
|
use ComposerRequireChecker\Exception\DependenciesNotInstalledException; |
6
|
|
|
use ComposerRequireChecker\Exception\NotReadableException; |
7
|
|
|
use ComposerRequireChecker\JsonLoader; |
8
|
|
|
use Generator; |
9
|
|
|
use function array_key_exists; |
10
|
|
|
use function file_get_contents; |
11
|
|
|
use function json_decode; |
12
|
|
|
|
13
|
|
|
final class LocateComposerPackageDirectDependenciesSourceFiles |
14
|
|
|
{ |
15
|
13 |
|
public function __invoke(string $composerJsonPath): Generator |
16
|
|
|
{ |
17
|
13 |
|
$packageDir = dirname($composerJsonPath); |
18
|
|
|
|
19
|
13 |
|
$composerJson = json_decode(file_get_contents($composerJsonPath), true); |
20
|
13 |
|
$configVendorDir = $composerJson['config']['vendor-dir'] ?? 'vendor'; |
21
|
13 |
|
$vendorDirs = []; |
22
|
13 |
|
foreach ($composerJson['require'] ?? [] as $vendorName => $vendorRequiredVersion) { |
23
|
9 |
|
$vendorDirs[$vendorName] = $packageDir . '/' . $configVendorDir . '/' . $vendorName; |
24
|
|
|
}; |
25
|
|
|
|
26
|
13 |
|
$installedPackages = $this->getInstalledPackages($packageDir . '/' . $configVendorDir); |
27
|
|
|
|
28
|
12 |
|
foreach ($vendorDirs as $vendorName => $vendorDir) { |
29
|
9 |
|
if (!array_key_exists($vendorName, $installedPackages)) { |
30
|
5 |
|
continue; |
31
|
|
|
} |
32
|
|
|
|
33
|
8 |
|
yield from (new LocateComposerPackageSourceFiles())->__invoke($installedPackages[$vendorName], $vendorDir); |
34
|
|
|
} |
35
|
12 |
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Lookup each vendor package's composer.json info from installed.json |
40
|
|
|
* |
41
|
|
|
* @param string $vendorDir |
42
|
|
|
* |
43
|
|
|
* @return array Keys are the package name and value is the composer.json as an array |
44
|
|
|
* @throws DependenciesNotInstalledException When composer install/update has not been run |
45
|
|
|
*/ |
46
|
13 |
|
private function getInstalledPackages(string $vendorDir): array |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
13 |
|
$installedData = (new JsonLoader($vendorDir . '/composer/installed.json'))->getData(); |
50
|
1 |
|
} catch (NotReadableException $e) { |
51
|
1 |
|
$message = 'The composer dependencies have not been installed, run composer install/update first'; |
52
|
1 |
|
throw new DependenciesNotInstalledException($message); |
53
|
|
|
} |
54
|
|
|
|
55
|
12 |
|
$installedPackages = []; |
56
|
|
|
|
57
|
12 |
|
$packages = $installedData['packages'] ?? $installedData; |
58
|
12 |
|
foreach ($packages as $vendorJson) { |
59
|
8 |
|
$vendorName = $vendorJson['name']; |
60
|
8 |
|
$installedPackages[$vendorName] = $vendorJson; |
61
|
|
|
} |
62
|
|
|
|
63
|
12 |
|
return $installedPackages; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|