Some checks failed
Auto Tag Develop / tag (push) Has been cancelled
Reviewed-on: #3 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\State;
|
|
|
|
use ApiPlatform\Metadata\Operation;
|
|
use ApiPlatform\State\ProviderInterface;
|
|
use App\ApiResource\LogOutput;
|
|
use App\Repository\EnvironmentRepository;
|
|
use App\Service\LogService;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
final readonly class DockerLogProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private EnvironmentRepository $environmentRepository,
|
|
private LogService $logService,
|
|
private RequestStack $requestStack,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): LogOutput
|
|
{
|
|
$id = $uriVariables['id'] ?? null;
|
|
$environment = $id ? $this->environmentRepository->find($id) : null;
|
|
|
|
if (null === $environment) {
|
|
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $id));
|
|
}
|
|
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
$lines = (int) ($request?->query->get('lines', '100') ?? 100);
|
|
$since = $request?->query->get('since');
|
|
|
|
$content = $this->logService->getDockerLogs(
|
|
$environment->getContainerName(),
|
|
$lines,
|
|
$since,
|
|
);
|
|
|
|
$dto = new LogOutput();
|
|
$dto->content = $content;
|
|
$dto->lines = $lines;
|
|
$dto->source = sprintf('docker:%s', $environment->getContainerName());
|
|
|
|
return $dto;
|
|
}
|
|
}
|