feat: integrate with easy admin bundle

This commit is contained in:
Björn Fromme
2026-03-16 12:02:27 +01:00
parent bdcd54687d
commit 136e2d68a2
10 changed files with 256 additions and 115 deletions
-107
View File
@@ -1,107 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Command;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
#[AsCommand(
name: 'app:bpn:read-dump',
description: 'Read XML request or response dump for a given request ID'
)]
class ReadXmlDumpCommand extends Command
{
public function __construct(
private readonly FilesystemOperator $xmlDump,
) {
parent::__construct();
}
protected function configure(): void
{
$this
->addArgument('requestId', InputArgument::REQUIRED, 'The request ID to look up')
->addOption('type', 't', InputOption::VALUE_REQUIRED, 'Dump type: request or response', 'request');
}
/**
* @see \Symfony\Component\Console\Command\Command::execute()
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
$requestId = $input->getArgument('requestId');
$type = $input->getOption('type');
if (false === in_array($type, ['request', 'response'], true)) {
$io->error('Invalid type. Must be "request" or "response".');
return Command::FAILURE;
}
try {
$files = $this->findMatchingFiles($requestId, $type);
if (0 === count($files)) {
$io->warning(sprintf('No dump files found for request ID "%s". They may have been cleaned up.', $requestId));
return Command::FAILURE;
}
foreach ($files as $filename) {
if (count($files) > 1) {
$io->section($filename);
}
$content = $this->xmlDump->read($filename);
$output->writeln($content);
}
return Command::SUCCESS;
} catch (FilesystemException $e) {
$io->error(sprintf('Failed to read dump file: %s', $e->getMessage()));
return Command::FAILURE;
}
}
/**
* Finds dump files matching the given request ID and type.
*
* Supports both exact matches (e.g., "abc123_1") and base ID matches (e.g., "abc123")
* which will return all files with counter suffixes.
*
* @return string[]
*
* @throws FilesystemException
*/
private function findMatchingFiles(string $requestId, string $type): array
{
$exactMatch = $requestId.'_'.$type.'.xml';
if ($this->xmlDump->fileExists($exactMatch)) {
return [$exactMatch];
}
$pattern = '/^'.preg_quote($requestId, '/').'_\d+_'.$type.'\.xml$/';
$matches = [];
foreach ($this->xmlDump->listContents('.') as $item) {
if ($item->isFile() && 1 === preg_match($pattern, $item->path())) {
$matches[] = $item->path();
}
}
sort($matches);
return $matches;
}
}
@@ -45,7 +45,7 @@ class BookingEditDraftCrudController extends AbstractCrudController
public function configureFields(string $pageName): iterable
{
return [
IntegerField::new('bookingId', 'Buchungs ID'),
IntegerField::new('bookingNumber', 'Vorgang'),
AssociationField::new('user', 'Kundenaccount')->formatValue(fn (User $user) => $user->getEmail()),
DateTimeField::new('createdAt', 'erstellt am'),
DateTimeField::new('updatedAt', 'aktualisiert am'),
+11 -1
View File
@@ -8,8 +8,10 @@ use App\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Attribute\AdminDashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
#[AdminDashboard(routePath: '/admin', routeName: 'admin')]
class DashboardController extends AbstractDashboardController
@@ -28,11 +30,19 @@ class DashboardController extends AbstractDashboardController
;
}
public function configureUserMenu(UserInterface $user): UserMenu
{
return parent::configureUserMenu($user)
->addMenuItems([
MenuItem::linkToRoute('MyE&P', 'fa fa-user', 'app_account')
]);
}
public function configureMenuItems(): iterable
{
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Log', 'fa fa-list', LogEntry::class);
yield MenuItem::linkToCrud('Buchungsentwürfe', 'fa fa-pen-to-square', BookingEditDraft::class);
yield MenuItem::linkToCrud('Kundenaccounts', 'fa fa-users', User::class);
yield MenuItem::linkToCrud('Benutzeraccounts', 'fa fa-users', User::class);
}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Entity\LogEntry;
@@ -7,10 +9,7 @@ use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class LogEntryCrudController extends AbstractCrudController
@@ -22,8 +21,14 @@ class LogEntryCrudController extends AbstractCrudController
public function configureActions(Actions $actions): Actions
{
$viewDumps = Action::new('viewDumps', 'XML Dumps', 'fa fa-file-code')
->linkToRoute('admin_xml_dump_list', static fn (LogEntry $entity): array => ['id' => $entity->getId()])
->displayIf(static fn (LogEntry $entity): bool => 'bpn' === $entity->getChannel());
return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL)
->add(Crud::PAGE_INDEX, $viewDumps)
->add(Crud::PAGE_DETAIL, $viewDumps)
->remove(Crud::PAGE_INDEX, Action::NEW)
->remove(Crud::PAGE_INDEX, Action::EDIT)
->remove(Crud::PAGE_INDEX, Action::DELETE)
@@ -45,8 +50,9 @@ class LogEntryCrudController extends AbstractCrudController
return [
DateTimeField::new('createdAt', 'Zeitstempel'),
TextField::new('username', 'Benutzer'),
TextField::new('requestId', 'Request ID'),
TextField::new('message', 'Aktion'),
TextField::new('requestId', 'Request ID'),
TextField::new('uri', 'URI'),
];
}
}
+2 -2
View File
@@ -31,8 +31,8 @@ class UserCrudController extends AbstractCrudController
public function configureCrud(Crud $crud): Crud
{
return $crud
->setEntityLabelInSingular('Kundenaccount')
->setEntityLabelInPlural('Kundenaccounts')
->setEntityLabelInSingular('Benutzeraccount')
->setEntityLabelInPlural('Benutzeraccounts')
->setDefaultSort(['lastLoginAt' => 'DESC']);
}
@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Repository\LogEntryRepository;
use App\Service\XmlDumpService;
use League\Flysystem\FilesystemException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
#[Route('/admin/xml-dump')]
#[IsGranted('ROLE_ADMIN')]
class XmlDumpController extends AbstractController
{
public function __construct(
private readonly XmlDumpService $xmlDumpService,
private readonly LogEntryRepository $logEntryRepository,
) {
}
#[Route('/{id}', name: 'admin_xml_dump_list', methods: ['GET'])]
public function list(int $id): Response
{
$logEntry = $this->logEntryRepository->find($id);
if (null === $logEntry) {
throw new NotFoundHttpException('Log entry not found.');
}
if ('bpn' !== $logEntry->getChannel()) {
throw new NotFoundHttpException('XML dumps are only available for BPN channel entries.');
}
$requestId = $logEntry->getRequestId();
$dumps = [];
try {
$dumps = $this->xmlDumpService->findDumpsForRequestId($requestId);
} catch (FilesystemException) {
// Dumps directory may not exist or be inaccessible
}
return $this->render('admin/xml_dump/list.html.twig', [
'logEntry' => $logEntry,
'requestId' => $requestId,
'dumps' => $dumps,
]);
}
#[Route('/download/{filename}', name: 'admin_xml_dump_download', methods: ['GET'], requirements: ['filename' => '.+'])]
public function download(string $filename): Response
{
try {
if (false === $this->xmlDumpService->fileExists($filename)) {
throw new NotFoundHttpException('Dump file not found. It may have been cleaned up.');
}
$content = $this->xmlDumpService->getContent($filename);
} catch (FilesystemException $e) {
throw new NotFoundHttpException('Failed to read dump file: '.$e->getMessage());
}
$response = new Response($content);
$response->headers->set('Content-Type', 'application/xml');
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
basename($filename)
);
$response->headers->set('Content-Disposition', $disposition);
return $response;
}
}
+5
View File
@@ -111,4 +111,9 @@ class LogEntry
{
return $this->getExtra()['request_id'] ?? '-';
}
public function getUri(): string
{
return $this->getExtra()['uri'] ?? '';
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php
declare(strict_types=1);
namespace App\Service;
use League\Flysystem\FilesystemException;
use League\Flysystem\FilesystemOperator;
/**
* Provides access to XML dumps of BusProNet API communication.
*
* XML dumps are stored in the filesystem and named with a pattern that includes
* the request ID, sequence number, and type (request/response). This service
* allows finding and reading dumps by request ID.
*/
class XmlDumpService
{
public function __construct(
private readonly FilesystemOperator $xmlDump,
) {
}
/**
* Finds all XML dump files matching the given request ID.
*
* Supports both exact matches (e.g., "abc123_1") and base ID matches (e.g., "abc123")
* which will return all files with counter suffixes for both request and response types.
*
* @return array<int, array{filename: string, type: string, size: int}>
*
* @throws FilesystemException
*/
public function findDumpsForRequestId(string $requestId): array
{
if ('' === $requestId || '-' === $requestId) {
return [];
}
$pattern = '/^'.preg_quote($requestId, '/').'(_\d+)?_(request|response)\.xml$/';
$matches = [];
foreach ($this->xmlDump->listContents('.') as $item) {
if ($item->isFile() && 1 === preg_match($pattern, $item->path(), $typeMatch)) {
$matches[] = [
'filename' => $item->path(),
'type' => $typeMatch[2],
'size' => $this->xmlDump->fileSize($item->path()),
];
}
}
usort($matches, static fn (array $a, array $b): int => strcmp($a['filename'], $b['filename']));
return $matches;
}
/**
* Reads the content of a dump file.
*
* @throws FilesystemException
*/
public function getContent(string $filename): string
{
return $this->xmlDump->read($filename);
}
/**
* Checks if a dump file exists.
*
* @throws FilesystemException
*/
public function fileExists(string $filename): bool
{
return $this->xmlDump->fileExists($filename);
}
}