0 follower

Class Yiisoft\Yii\Debug\Api\Inspector\Controller\CommandController

InheritanceYiisoft\Yii\Debug\Api\Inspector\Controller\CommandController

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( \Yiisoft\DataResponse\DataResponseFactoryInterface $responseFactory )
$responseFactory \Yiisoft\DataResponse\DataResponseFactoryInterface

                public function __construct(
    private DataResponseFactoryInterface $responseFactory,
) {
}

            
index() public method

public \Psr\Http\Message\ResponseInterface index ( \Yiisoft\Config\ConfigInterface $config, \Yiisoft\Aliases\Aliases $aliases )
$config \Yiisoft\Config\ConfigInterface
$aliases \Yiisoft\Aliases\Aliases

                public function index(ConfigInterface $config, Aliases $aliases): ResponseInterface
{
    $params = $config->get('params');
    $configCommandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? [];
    $result = [];
    foreach ($configCommandMap as $groupName => $commands) {
        foreach ($commands as $name => $command) {
            if (!is_subclass_of($command, CommandInterface::class)) {
                continue;
            }
            $result[] = [
                'name' => $name,
                'title' => $command::getTitle(),
                'group' => $groupName,
                'description' => $command::getDescription(),
            ];
        }
    }
    $composerScripts = $this->getComposerScripts($aliases);
    foreach ($composerScripts as $scriptName => $commands) {
        $commandName = "composer/{$scriptName}";
        $result[] = [
            'name' => $commandName,
            'title' => $scriptName,
            'group' => 'composer',
            'description' => implode("\n", $commands),
        ];
    }
    return $this->responseFactory->createResponse($result);
}

            
run() public method

public \Psr\Http\Message\ResponseInterface run ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Container\ContainerInterface $container, \Yiisoft\Config\ConfigInterface $config, \Yiisoft\Aliases\Aliases $aliases )
$request \Psr\Http\Message\ServerRequestInterface
$container \Psr\Container\ContainerInterface
$config \Yiisoft\Config\ConfigInterface
$aliases \Yiisoft\Aliases\Aliases

                public function run(
    ServerRequestInterface $request,
    ContainerInterface $container,
    ConfigInterface $config,
    Aliases $aliases,
): ResponseInterface {
    $params = $config->get('params');
    $commandMap = $params['yiisoft/yii-debug-api']['inspector']['commandMap'] ?? [];
    $commandList = [];
    foreach ($commandMap as $commands) {
        foreach ($commands as $name => $command) {
            if (!is_subclass_of($command, CommandInterface::class)) {
                continue;
            }
            $commandList[$name] = $command;
        }
    }
    $composerScripts = $this->getComposerScripts($aliases);
    foreach ($composerScripts as $scriptName => $commands) {
        $commandName = "composer/{$scriptName}";
        $commandList[$commandName] = ['composer', $scriptName];
    }
    $request = $request->getQueryParams();
    $commandName = $request['command'] ?? null;
    if ($commandName === null) {
        throw new InvalidArgumentException(
            sprintf(
                'Command must not be null. Available commands: "%s".',
                implode('", "', array_keys($commandList))
            )
        );
    }
    if (!array_key_exists($commandName, $commandList)) {
        throw new InvalidArgumentException(
            sprintf(
                'Unknown command "%s". Available commands: "%s".',
                $commandName,
                implode('", "', array_keys($commandList))
            )
        );
    }
    $commandClass = $commandList[$commandName];
    if (is_string($commandClass) && $container->has($commandClass)) {
        $command = $container->get($commandClass);
    } else {
        $command = new BashCommand($aliases, (array)$commandClass);
    }
    $result = $command->run();
    return $this->responseFactory->createResponse([
        'status' => $result->getStatus(),
        'result' => $result->getResult(),
        'error' => $result->getErrors(),
    ]);
}