Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExecuteCommandEndpoint
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
156
0.00% covered (danger)
0.00%
0 / 1
 handle
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
156
1<?php
2
3namespace Olz\Apps\Commands\Endpoints;
4
5use Doctrine\DBAL\Exception\DriverException;
6use Olz\Api\OlzTypedEndpoint;
7use PhpTypeScriptApi\HttpError;
8use Symfony\Component\Console\Input\ArgvInput;
9use Symfony\Component\Console\Output\BufferedOutput;
10
11/**
12 * @extends OlzTypedEndpoint<
13 *   array{command: non-empty-string, argv?: ?non-empty-string},
14 *   array{error: bool, output: non-empty-string}
15 * >
16 */
17class ExecuteCommandEndpoint extends OlzTypedEndpoint {
18    protected function handle(mixed $input): mixed {
19        $command_name = $input['command'];
20        try {
21            $has_access = $this->authUtils()->hasPermission('commands');
22            $has_command_access = $this->authUtils()->hasPermission("command_{$command_name}");
23            if (!$has_access && !$has_command_access) {
24                throw new HttpError(403, "Kein Zugriff!");
25            }
26        } catch (DriverException $exc) {
27            // Could be a migration issue
28            // => if the command is a cache operation or a db migration/reset,
29            // continue nevertheless!
30            $should_continue = (
31                $command_name === 'cache:clear'
32                || $command_name === 'cache:warmup'
33                || $command_name === 'olz:db-migrate'
34                || $command_name === 'olz:db-reset'
35            );
36            if (!$should_continue) {
37                throw $exc;
38            }
39        }
40
41        set_time_limit(4000);
42        ignore_user_abort(true);
43
44        $argv_arg = $input['argv'] ?? null;
45        $argv = ($argv_arg ? preg_split('/\s+/', $argv_arg) : []) ?: [];
46        $command_input = new ArgvInput(['bin/console', $command_name, ...$argv]);
47        $command_input->setInteractive(false);
48        $command_output = new BufferedOutput();
49        try {
50            $this->symfonyUtils()->callCommand($command_name, $command_input, $command_output);
51            $output = $command_output->fetch();
52            $this->log()->info("Command {$command_name} successfully executed via endpoint.");
53            return [
54                'error' => false,
55                'output' => $output ? $output : '(no output)',
56            ];
57        } catch (\Throwable $th) {
58            $this->log()->notice("Failed to execute command {$command_name} via endpoint.");
59            $output = $command_output->fetch();
60            return [
61                'error' => true,
62                'output' => $output."\n".$th->getMessage(),
63            ];
64        }
65    }
66}