1 | <?php |
||
2 | |||
3 | namespace Sfneal\Dependencies\Utils; |
||
4 | |||
5 | use Illuminate\Support\Collection; |
||
6 | use Sfneal\Helpers\Strings\StringHelpers; |
||
7 | |||
8 | class ComposerRequirements |
||
9 | { |
||
10 | /** |
||
11 | * @var bool |
||
12 | */ |
||
13 | private bool $dev; |
||
14 | |||
15 | /** |
||
16 | * ComposerDependencies constructor. |
||
17 | * |
||
18 | * @param bool $dev |
||
19 | */ |
||
20 | public function __construct(bool $dev = false) |
||
21 | { |
||
22 | $this->dev = $dev; |
||
23 | } |
||
24 | |||
25 | /** |
||
26 | * Retrieve an array of composer package dependencies from the composer.json. |
||
27 | * |
||
28 | * @return Collection |
||
29 | */ |
||
30 | public function get(): Collection |
||
31 | { |
||
32 | // If development packages are included, merge require & require-dev |
||
33 | $dependencies = $this->dev ? array_merge(self::require(), self::requireDev()) : self::require(); |
||
34 | |||
35 | // Retrieve 'require' array from composer.json with only package names (the keys |
||
36 | // todo: remove keys? |
||
37 | return collect(array_keys($dependencies)) |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
38 | |||
39 | // Remove 'php' & php extensions from the packages array |
||
40 | ->filter(function (string $dep) { |
||
41 | return $dep != 'php' && ! (new StringHelpers($dep))->inString('ext'); |
||
42 | }); |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Return the file contents of a composer.json file decoded as json array. |
||
47 | * |
||
48 | * @return array |
||
49 | */ |
||
50 | private static function composerJson(): array |
||
51 | { |
||
52 | return json_decode(file_get_contents(config('dependencies.composer_json_path')), true); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Retrieve an array of the composer package dependencies. |
||
57 | * |
||
58 | * @return array |
||
59 | */ |
||
60 | private static function require(): array |
||
61 | { |
||
62 | return self::composerJson()['require']; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Retrieve an array of the composer package development dependencies. |
||
67 | * |
||
68 | * @return array |
||
69 | */ |
||
70 | private static function requireDev(): array |
||
71 | { |
||
72 | return self::composerJson()['require-dev']; |
||
73 | } |
||
74 | } |
||
75 |