1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the box project. |
||
7 | * |
||
8 | * (c) Kevin Herrera <[email protected]> |
||
9 | * Théo Fidry <[email protected]> |
||
10 | * |
||
11 | * This source file is subject to the MIT license that is bundled |
||
12 | * with this source code in the file LICENSE. |
||
13 | */ |
||
14 | |||
15 | namespace KevinGH\Box\Composer\Artifact; |
||
16 | |||
17 | use KevinGH\Box\Composer\Package\Extensions; |
||
18 | use KevinGH\Box\Composer\Package\PackageInfo; |
||
19 | use KevinGH\Box\Composer\Package\RequiredItem; |
||
20 | use function array_key_exists; |
||
21 | use function array_keys; |
||
22 | use function current; |
||
23 | use function trim; |
||
24 | |||
25 | /** |
||
26 | * @private |
||
27 | */ |
||
28 | final readonly class ComposerJson |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
29 | { |
||
30 | /** |
||
31 | * @param array $decodedContents Decoded JSON contents of the `composer.json` file |
||
32 | */ |
||
33 | public function __construct( |
||
34 | public string $path, |
||
35 | public array $decodedContents, |
||
36 | ) { |
||
37 | } |
||
38 | |||
39 | public function getRequiredPhpVersion(): ?string |
||
40 | { |
||
41 | return $this->decodedContents['require']['php'] ?? null; |
||
42 | } |
||
43 | |||
44 | public function hasRequiredPhpVersion(): bool |
||
45 | { |
||
46 | return null !== $this->getRequiredPhpVersion(); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * @return RequiredItem[] |
||
51 | */ |
||
52 | public function getRequiredItems(): array |
||
53 | { |
||
54 | $require = $this->decodedContents['require'] ?? []; |
||
55 | |||
56 | return array_map( |
||
57 | static fn (string $packageName) => new RequiredItem([$packageName => $require[$packageName]]), |
||
58 | array_keys($require), |
||
59 | ); |
||
60 | } |
||
61 | |||
62 | public function getConflictingExtensions(): Extensions |
||
63 | { |
||
64 | return PackageInfo::parseExtensions( |
||
65 | $this->decodedContents['conflict'] ?? [], |
||
66 | ); |
||
67 | } |
||
68 | |||
69 | public function getVendorDir(): ?string |
||
70 | { |
||
71 | return $this->decodedContents['config']['vendor-dir'] ?? null; |
||
72 | } |
||
73 | |||
74 | public function getFirstBin(): ?string |
||
75 | { |
||
76 | $firstBin = current((array) ($this->decodedContents['bin'] ?? [])); |
||
77 | |||
78 | return false === $firstBin ? null : $firstBin; |
||
79 | } |
||
80 | |||
81 | public function hasAutoload(): bool |
||
82 | { |
||
83 | return array_key_exists('autoload', $this->decodedContents); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return string[] |
||
88 | */ |
||
89 | public function getAutoloadPaths(): array |
||
90 | { |
||
91 | $autoload = $this->decodedContents['autoload'] ?? []; |
||
92 | $paths = []; |
||
93 | |||
94 | if (array_key_exists('psr-4', $autoload)) { |
||
95 | foreach ($autoload['psr-4'] as $path) { |
||
96 | /** @var string|string[] $path */ |
||
97 | $composerPaths = (array) $path; |
||
98 | |||
99 | foreach ($composerPaths as $composerPath) { |
||
100 | $paths[] = trim($composerPath); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | |||
105 | if (array_key_exists('psr-0', $autoload)) { |
||
106 | foreach ($autoload['psr-0'] as $path) { |
||
107 | /** @var string|string[] $path */ |
||
108 | $composerPaths = (array) $path; |
||
109 | |||
110 | foreach ($composerPaths as $composerPath) { |
||
111 | $paths[] = trim($composerPath); |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | |||
116 | if (array_key_exists('classmap', $autoload)) { |
||
117 | foreach ($autoload['classmap'] as $path) { |
||
118 | // @var string $path |
||
119 | $paths[] = $path; |
||
120 | } |
||
121 | } |
||
122 | |||
123 | return $paths; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * @return string[] |
||
128 | */ |
||
129 | public function getAutoloadFiles(): array |
||
130 | { |
||
131 | return $this->decodedContents['autoload']['files'] ?? []; |
||
132 | } |
||
133 | } |
||
134 |