1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Api\Inspector\Controller; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
use Psr\Http\Message\ResponseInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
11
|
|
|
use Yiisoft\Aliases\Aliases; |
12
|
|
|
use Yiisoft\Config\ConfigInterface; |
13
|
|
|
use Yiisoft\DataResponse\DataResponseFactoryInterface; |
14
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\Command\BashCommand; |
15
|
|
|
use Yiisoft\Yii\Debug\Api\Inspector\CommandInterface; |
16
|
|
|
|
17
|
|
|
use function array_key_exists; |
18
|
|
|
use function is_array; |
19
|
|
|
use function is_string; |
20
|
|
|
|
21
|
|
|
class CommandController |
22
|
|
|
{ |
23
|
|
|
public function __construct( |
24
|
|
|
private DataResponseFactoryInterface $responseFactory, |
25
|
|
|
) { |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function index(ConfigInterface $config, Aliases $aliases): ResponseInterface |
29
|
|
|
{ |
30
|
|
|
$params = $config->get('params'); |
31
|
|
|
$configCommandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? []; |
32
|
|
|
|
33
|
|
|
$result = []; |
34
|
|
|
foreach ($configCommandMap as $groupName => $commands) { |
35
|
|
|
foreach ($commands as $name => $command) { |
36
|
|
|
if (!is_subclass_of($command, CommandInterface::class)) { |
37
|
|
|
continue; |
38
|
|
|
} |
39
|
|
|
$result[] = [ |
40
|
|
|
'name' => $name, |
41
|
|
|
'title' => $command::getTitle(), |
42
|
|
|
'group' => $groupName, |
43
|
|
|
'description' => $command::getDescription(), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$composerScripts = $this->getComposerScripts($aliases); |
49
|
|
|
foreach ($composerScripts as $scriptName => $commands) { |
50
|
|
|
$commandName = "composer/{$scriptName}"; |
51
|
|
|
$result[] = [ |
52
|
|
|
'name' => $commandName, |
53
|
|
|
'title' => $scriptName, |
54
|
|
|
'group' => 'composer', |
55
|
|
|
'description' => implode("\n", $commands), |
56
|
|
|
]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this->responseFactory->createResponse($result); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function run( |
63
|
|
|
ServerRequestInterface $request, |
64
|
|
|
ContainerInterface $container, |
65
|
|
|
ConfigInterface $config, |
66
|
|
|
Aliases $aliases, |
67
|
|
|
): ResponseInterface { |
68
|
|
|
$params = $config->get('params'); |
69
|
|
|
$commandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? []; |
70
|
|
|
|
71
|
|
|
$commandList = []; |
72
|
|
|
foreach ($commandMap as $commands) { |
73
|
|
|
foreach ($commands as $name => $command) { |
74
|
|
|
if (!is_subclass_of($command, CommandInterface::class)) { |
75
|
|
|
continue; |
76
|
|
|
} |
77
|
|
|
$commandList[$name] = $command; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$composerScripts = $this->getComposerScripts($aliases); |
81
|
|
|
foreach ($composerScripts as $scriptName => $commands) { |
82
|
|
|
$commandName = "composer/{$scriptName}"; |
83
|
|
|
$commandList[$commandName] = ['composer', $scriptName]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$request = $request->getQueryParams(); |
87
|
|
|
$commandName = $request['command'] ?? null; |
88
|
|
|
|
89
|
|
|
if ($commandName === null) { |
90
|
|
|
throw new InvalidArgumentException( |
91
|
|
|
sprintf( |
92
|
|
|
'Command must not be null. Available commands: "%s".', |
93
|
|
|
implode('", "', array_keys($commandList)) |
94
|
|
|
) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!array_key_exists($commandName, $commandList)) { |
99
|
|
|
throw new InvalidArgumentException( |
100
|
|
|
sprintf( |
101
|
|
|
'Unknown command "%s". Available commands: "%s".', |
102
|
|
|
$commandName, |
103
|
|
|
implode('", "', array_keys($commandList)) |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$commandClass = $commandList[$commandName]; |
109
|
|
|
if (is_string($commandClass) && $container->has($commandClass)) { |
110
|
|
|
$command = $container->get($commandClass); |
111
|
|
|
} else { |
112
|
|
|
$command = new BashCommand($aliases, (array)$commandClass); |
113
|
|
|
} |
114
|
|
|
$result = $command->run(); |
115
|
|
|
|
116
|
|
|
return $this->responseFactory->createResponse([ |
117
|
|
|
'status' => $result->getStatus(), |
118
|
|
|
'result' => $result->getResult(), |
119
|
|
|
'error' => $result->getErrors(), |
120
|
|
|
]); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function getComposerScripts(Aliases $aliases): array |
124
|
|
|
{ |
125
|
|
|
$result = []; |
126
|
|
|
$composerJsonPath = $aliases->get('@root/composer.json'); |
127
|
|
|
if (file_exists($composerJsonPath)) { |
128
|
|
|
$composerJsonCommands = json_decode(file_get_contents($composerJsonPath), true, 512, JSON_THROW_ON_ERROR); |
129
|
|
|
if (is_array($composerJsonCommands) && isset($composerJsonCommands['scripts'])) { |
130
|
|
|
$scripts = $composerJsonCommands['scripts']; |
131
|
|
|
foreach ($scripts as $name => $script) { |
132
|
|
|
$result[$name] = (array) $script; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|