59 lines
1.9 KiB
PHP
59 lines
1.9 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\Repository\LogFileRepository;
|
|
use App\Service\LogService;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
final readonly class SymfonyLogProvider implements ProviderInterface
|
|
{
|
|
public function __construct(
|
|
private EnvironmentRepository $environmentRepository,
|
|
private LogFileRepository $logFileRepository,
|
|
private LogService $logService,
|
|
private RequestStack $requestStack,
|
|
) {}
|
|
|
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): LogOutput
|
|
{
|
|
$envId = $uriVariables['id'] ?? null;
|
|
$logFileId = $uriVariables['logFileId'] ?? null;
|
|
|
|
$environment = $envId ? $this->environmentRepository->find($envId) : null;
|
|
if (null === $environment) {
|
|
throw new NotFoundHttpException(sprintf('Environment "%s" not found.', $envId));
|
|
}
|
|
|
|
$logFile = $logFileId ? $this->logFileRepository->find($logFileId) : null;
|
|
if (null === $logFile || $logFile->getEnvironment()?->getId() !== $environment->getId()) {
|
|
throw new NotFoundHttpException(sprintf('Log file "%s" not found.', $logFileId));
|
|
}
|
|
|
|
$request = $this->requestStack->getCurrentRequest();
|
|
$lines = (int) ($request?->query->get('lines', '100') ?? 100);
|
|
$level = $request?->query->get('level');
|
|
|
|
$content = $this->logService->getSymfonyLog(
|
|
$environment->getContainerName(),
|
|
$logFile->getPath(),
|
|
$lines,
|
|
$level,
|
|
);
|
|
|
|
$dto = new LogOutput();
|
|
$dto->content = $content;
|
|
$dto->lines = $lines;
|
|
$dto->source = sprintf('symfony:%s', $logFile->getLabel());
|
|
|
|
return $dto;
|
|
}
|
|
}
|