1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Inspector\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
|
|
|
'Query parameter "package" should not be empty.' |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
$command = new BashCommand($aliases, ['composer', 'show', $package, '--all', '--format=json']); |
54
|
|
|
$result = $command->run(); |
55
|
|
|
|
56
|
|
|
return $this->responseFactory->createResponse([ |
57
|
|
|
'status' => $result->getStatus(), |
58
|
|
|
'result' => $result->getStatus() === CommandResponse::STATUS_OK |
59
|
|
|
? json_decode($result->getResult(), true, 512, JSON_THROW_ON_ERROR) |
60
|
|
|
: null, |
61
|
|
|
'errors' => $result->getErrors(), |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function require(ServerRequestInterface $request, Aliases $aliases): ResponseInterface |
66
|
|
|
{ |
67
|
|
|
// Request factory may be unable to parse JSON so don't rely on getParsedBody(). |
68
|
|
|
$parsedBody = \json_decode($request->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); |
69
|
|
|
$package = $parsedBody['package'] ?? null; |
70
|
|
|
$version = $parsedBody['version'] ?? null; |
71
|
|
|
$isDev = $parsedBody['isDev'] ?? false; |
72
|
|
|
if ($package === null) { |
73
|
|
|
throw new InvalidArgumentException( |
74
|
|
|
'Query parameter "package" should not be empty.' |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
$packageWithVersion = sprintf('%s:%s', $package, $version ?? '*'); |
78
|
|
|
$command = new BashCommand($aliases, [ |
79
|
|
|
'composer', |
80
|
|
|
'require', |
81
|
|
|
$packageWithVersion, |
82
|
|
|
'-n', |
83
|
|
|
...$isDev ? ['--dev'] : [], |
84
|
|
|
]); |
85
|
|
|
$result = $command->run(); |
86
|
|
|
|
87
|
|
|
return $this->responseFactory->createResponse([ |
88
|
|
|
'status' => $result->getStatus(), |
89
|
|
|
'result' => !is_string($result->getResult()) |
90
|
|
|
? null |
91
|
|
|
: ( |
92
|
|
|
$result->getStatus() === CommandResponse::STATUS_OK |
93
|
|
|
? json_decode($result->getResult(), true, 512, JSON_THROW_ON_ERROR) |
94
|
|
|
: $result->getResult() |
95
|
|
|
), |
96
|
|
|
'errors' => $result->getErrors(), |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|