1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Laravel Paket. |
5
|
|
|
* |
6
|
|
|
* (c) Anton Komarev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Cog\Laravel\Paket\Http\Controllers\Api\Requirements; |
15
|
|
|
|
16
|
|
|
use Illuminate\Contracts\Support\Responsable as ResponsableContract; |
17
|
|
|
use MCStreetguy\ComposerParser\Factory as ComposerParser; |
18
|
|
|
|
19
|
|
|
final class CollectAction |
20
|
|
|
{ |
21
|
|
|
public function __invoke(): ResponsableContract |
22
|
|
|
{ |
23
|
|
|
$requirements = $this->getInstalledRequirements(); |
24
|
|
|
|
25
|
|
|
return new CollectResponse($requirements); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function getInstalledRequirements(): array |
29
|
|
|
{ |
30
|
|
|
$lockFile = ComposerParser::parseLockfile(base_path('composer.lock')); |
31
|
|
|
$jsonFile = ComposerParser::parseComposerJson(base_path('composer.json')); |
32
|
|
|
|
33
|
|
|
$packages = $lockFile->getPackages()->getData(); |
34
|
|
|
$devPackages = $lockFile->getPackagesDev()->getData(); |
35
|
|
|
$platform = $lockFile->getPlatform()->getData(); |
36
|
|
|
$devPlatform = $lockFile->getPlatformDev()->getData(); |
37
|
|
|
|
38
|
|
|
foreach ($packages as &$package) { |
39
|
|
|
$package['isDevelopment'] = false; |
40
|
|
|
} |
41
|
|
|
foreach ($devPackages as &$package) { |
42
|
|
|
$package['isDevelopment'] = true; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$requires = $jsonFile->getRequire()->getData(); |
46
|
|
|
$devRequires = $jsonFile->getRequireDev()->getData(); |
47
|
|
|
|
48
|
|
|
$roots = [ |
49
|
|
|
'essential' => [], |
50
|
|
|
'dev' => [], |
51
|
|
|
]; |
52
|
|
|
foreach ($platform as $name => $version) { |
53
|
|
|
$roots['essential'][] = [ |
54
|
|
|
'name' => $name, |
55
|
|
|
'version' => $version, |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
foreach ($devPlatform as $name => $version) { |
59
|
|
|
$roots['dev'][] = [ |
60
|
|
|
'name' => $name, |
61
|
|
|
'version' => $version, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
View Code Duplication |
foreach ($packages as $key => $package) { |
|
|
|
|
65
|
|
|
if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) { |
66
|
|
|
$roots['essential'][] = $package; |
67
|
|
|
unset($packages[$key]); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
View Code Duplication |
foreach ($devPackages as $key => $package) { |
|
|
|
|
71
|
|
|
if (isset($requires[$package['name']]) || isset($devRequires[$package['name']])) { |
72
|
|
|
$roots['dev'][] = $package; |
73
|
|
|
unset($devPackages[$key]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
$dependencies = [ |
77
|
|
|
'essential' => [], |
78
|
|
|
'dev' => [], |
79
|
|
|
]; |
80
|
|
|
foreach ($packages as $key => $package) { |
81
|
|
|
$dependencies['essential'][] = $package; |
82
|
|
|
} |
83
|
|
|
foreach ($devPackages as $key => $package) { |
84
|
|
|
$dependencies['dev'][] = $package; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'roots' => $roots, |
89
|
|
|
'dependencies' => $dependencies, |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.