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\Repositories; |
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
|
|
|
$repositories = $this->getRepositories(); |
24
|
|
|
|
25
|
|
|
return new CollectResponse($repositories); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function getRepositories(): array |
29
|
|
|
{ |
30
|
|
|
$jsonFile = ComposerParser::parseComposerJson(base_path('composer.json')); |
31
|
|
|
|
32
|
|
|
$repositories = $jsonFile->getRepositories(); |
33
|
|
|
$output = []; |
34
|
|
|
|
35
|
|
|
foreach ($repositories as $key => $repository) { |
36
|
|
|
$package = $repository->getPackage(); |
37
|
|
|
|
38
|
|
|
$output[$key] = [ |
39
|
|
|
'type' => $repository->getType(), |
40
|
|
|
'url' => $repository->getUrl(), |
41
|
|
|
'options' => $repository->getOptions(), |
42
|
|
|
'package' => is_null($package) ? null : $package->toArray(), |
43
|
|
|
]; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$output = $this->addDefaultRepository($output); |
47
|
|
|
|
48
|
|
|
return $output; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// TODO: Remove after fix: https://github.com/MCStreetguy/ComposerParser/issues/4 |
52
|
|
|
private function addDefaultRepository(array $repositories): array |
53
|
|
|
{ |
54
|
|
|
$isPackagistOrgDisabled = false; |
55
|
|
|
foreach ($repositories as $key => $repository) { |
56
|
|
|
// This hacky temporary solution the only way to determine `{"packagist.org": false}` repository object |
57
|
|
|
// But in fact it will display that packagist ignored even with any empty object |
58
|
|
|
if ($repository['type'] === '' && $repository['url'] === '' && $repository['options'] === [] && is_null($repository['package'])) { |
59
|
|
|
$isPackagistOrgDisabled = true; |
60
|
|
|
unset($repositories[$key]); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($isPackagistOrgDisabled) { |
65
|
|
|
return $repositories; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$defaultRepository = [ |
69
|
|
|
'type' => 'composer', |
70
|
|
|
'url' => 'https?://repo.packagist.org', |
71
|
|
|
'allow_ssl_downgrade' => true, |
72
|
|
|
]; |
73
|
|
|
|
74
|
|
|
if (!isset($repositories['packagist.org'])) { |
75
|
|
|
$repositories['packagist.org'] = []; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$repositories['packagist.org'] = array_merge($defaultRepository, $repositories['packagist.org']); |
79
|
|
|
|
80
|
|
|
return $repositories; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|