|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Yiisoft\Aliases\Aliases; |
|
12
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
|
13
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\Command\BashCommand; |
|
14
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\CommandResponse; |
|
15
|
|
|
|
|
16
|
|
|
final class ComposerController |
|
17
|
|
|
{ |
|
18
|
|
|
public function __construct( |
|
19
|
|
|
private DataResponseFactoryInterface $responseFactory, |
|
20
|
|
|
) { |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function index(Aliases $aliases): ResponseInterface |
|
24
|
|
|
{ |
|
25
|
|
|
$composerJsonPath = $aliases->get('@root/composer.json'); |
|
26
|
|
|
$composerLockPath = $aliases->get('@root/composer.lock'); |
|
27
|
|
|
if (!file_exists($composerJsonPath)) { |
|
28
|
|
|
throw new Exception( |
|
29
|
|
|
sprintf( |
|
30
|
|
|
'Could not find composer.json by the path "%s".', |
|
31
|
|
|
$composerJsonPath, |
|
32
|
|
|
) |
|
33
|
|
|
); |
|
34
|
|
|
} |
|
35
|
|
|
$result = [ |
|
36
|
|
|
'json' => json_decode(file_get_contents($composerJsonPath), true, 512, JSON_THROW_ON_ERROR), |
|
37
|
|
|
'lock' => file_exists($composerLockPath) |
|
38
|
|
|
? json_decode(file_get_contents($composerLockPath), true, 512, JSON_THROW_ON_ERROR) |
|
39
|
|
|
: null, |
|
40
|
|
|
]; |
|
41
|
|
|
|
|
42
|
|
|
return $this->responseFactory->createResponse($result); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function inspect(ServerRequestInterface $request, Aliases $aliases): ResponseInterface |
|
46
|
|
|
{ |
|
47
|
|
|
$package = $request->getQueryParams()['package'] ?? null; |
|
48
|
|
|
if ($package === null) { |
|
49
|
|
|
throw new InvalidArgumentException( |
|
50
|
|
|
sprintf( |
|
51
|
|
|
'Query parameter "package" should not be empty.' |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
$command = new BashCommand($aliases, ['composer', 'show', $package, '--all', '--format=json']); |
|
56
|
|
|
$result = $command->run(); |
|
57
|
|
|
|
|
58
|
|
|
return $this->responseFactory->createResponse([ |
|
59
|
|
|
'status' => $result->getStatus(), |
|
60
|
|
|
'result' => $result->getStatus() === CommandResponse::STATUS_OK |
|
61
|
|
|
? json_decode($result->getResult(), true, 512, JSON_THROW_ON_ERROR) |
|
62
|
|
|
: null, |
|
63
|
|
|
'errors' => $result->getErrors(), |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function require(ServerRequestInterface $request, Aliases $aliases): ResponseInterface |
|
68
|
|
|
{ |
|
69
|
|
|
$package = $request->getParsedBody()['package'] ?? null; |
|
70
|
|
|
$version = $request->getParsedBody()['version'] ?? null; |
|
71
|
|
|
$isDev = $request->getParsedBody()['isDev'] ?? false; |
|
72
|
|
|
if ($package === null) { |
|
73
|
|
|
throw new InvalidArgumentException( |
|
74
|
|
|
sprintf( |
|
75
|
|
|
'Query parameter "package" should not be empty.' |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
$packageWithVersion = sprintf('%s:%s', $package, $version ?? '*'); |
|
80
|
|
|
$command = new BashCommand($aliases, [ |
|
81
|
|
|
'composer', |
|
82
|
|
|
'require', |
|
83
|
|
|
$packageWithVersion, |
|
84
|
|
|
'-n', |
|
85
|
|
|
...$isDev ? ['--dev'] : [], |
|
86
|
|
|
]); |
|
87
|
|
|
$result = $command->run(); |
|
88
|
|
|
|
|
89
|
|
|
return $this->responseFactory->createResponse([ |
|
90
|
|
|
'status' => $result->getStatus(), |
|
91
|
|
|
'result' => $result->getStatus() === CommandResponse::STATUS_OK |
|
92
|
|
|
? json_decode($result->getResult(), true, 512, JSON_THROW_ON_ERROR) |
|
93
|
|
|
: null, |
|
94
|
|
|
'errors' => $result->getErrors(), |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|