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 public function configureFields(string $pageName): iterable
{ {
return [ return [
IntegerField::new('bookingId', 'Buchungs ID'), IntegerField::new('bookingNumber', 'Vorgang'),
AssociationField::new('user', 'Kundenaccount')->formatValue(fn (User $user) => $user->getEmail()), AssociationField::new('user', 'Kundenaccount')->formatValue(fn (User $user) => $user->getEmail()),
DateTimeField::new('createdAt', 'erstellt am'), DateTimeField::new('createdAt', 'erstellt am'),
DateTimeField::new('updatedAt', 'aktualisiert 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\Attribute\AdminDashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard; use EasyCorp\Bundle\EasyAdminBundle\Config\Dashboard;
use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem; use EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractDashboardController;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\UserInterface;
#[AdminDashboard(routePath: '/admin', routeName: 'admin')] #[AdminDashboard(routePath: '/admin', routeName: 'admin')]
class DashboardController extends AbstractDashboardController 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 public function configureMenuItems(): iterable
{ {
yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home'); yield MenuItem::linkToDashboard('Dashboard', 'fa fa-home');
yield MenuItem::linkToCrud('Log', 'fa fa-list', LogEntry::class); yield MenuItem::linkToCrud('Log', 'fa fa-list', LogEntry::class);
yield MenuItem::linkToCrud('Buchungsentwürfe', 'fa fa-pen-to-square', BookingEditDraft::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 <?php
declare(strict_types=1);
namespace App\Controller\Admin; namespace App\Controller\Admin;
use App\Entity\LogEntry; 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\Actions;
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController; use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField; use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
class LogEntryCrudController extends AbstractCrudController class LogEntryCrudController extends AbstractCrudController
@@ -22,8 +21,14 @@ class LogEntryCrudController extends AbstractCrudController
public function configureActions(Actions $actions): Actions 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 return $actions
->add(Crud::PAGE_INDEX, Action::DETAIL) ->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::NEW)
->remove(Crud::PAGE_INDEX, Action::EDIT) ->remove(Crud::PAGE_INDEX, Action::EDIT)
->remove(Crud::PAGE_INDEX, Action::DELETE) ->remove(Crud::PAGE_INDEX, Action::DELETE)
@@ -45,8 +50,9 @@ class LogEntryCrudController extends AbstractCrudController
return [ return [
DateTimeField::new('createdAt', 'Zeitstempel'), DateTimeField::new('createdAt', 'Zeitstempel'),
TextField::new('username', 'Benutzer'), TextField::new('username', 'Benutzer'),
TextField::new('requestId', 'Request ID'),
TextField::new('message', 'Aktion'), 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 public function configureCrud(Crud $crud): Crud
{ {
return $crud return $crud
->setEntityLabelInSingular('Kundenaccount') ->setEntityLabelInSingular('Benutzeraccount')
->setEntityLabelInPlural('Kundenaccounts') ->setEntityLabelInPlural('Benutzeraccounts')
->setDefaultSort(['lastLoginAt' => 'DESC']); ->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'] ?? '-'; 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);
}
}
+8
View File
@@ -21,6 +21,14 @@
<span class="text-xl uppercase text-white group-hover:text-primary-light">Meine Daten</span> <span class="text-xl uppercase text-white group-hover:text-primary-light">Meine Daten</span>
</a> </a>
</li> </li>
{% if is_granted('ROLE_ADMIN') %}
<li class="py-4">
<a href="{{ path('admin') }}" class="flex items-center space-x-2 group" hx-boost="false">
<svg class="w-8 h-8 text-white group-hover:text-primary-light" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"/><circle cx="128" cy="128" r="40" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/><path d="M41.43,178.09A99.14,99.14,0,0,1,31.36,153.8l16.78-21a81.59,81.59,0,0,1,0-9.64l-16.77-21a99.43,99.43,0,0,1,10.05-24.3l26.71-3a81,81,0,0,1,6.81-6.81l3-26.7A99.14,99.14,0,0,1,102.2,31.36l21,16.78a81.59,81.59,0,0,1,9.64,0l21-16.77a99.43,99.43,0,0,1,24.3,10.05l3,26.71a81,81,0,0,1,6.81,6.81l26.7,3a99.14,99.14,0,0,1,10.07,24.29l-16.78,21a81.59,81.59,0,0,1,0,9.64l16.77,21a99.43,99.43,0,0,1-10,24.3l-26.71,3a81,81,0,0,1-6.81,6.81l-3,26.7a99.14,99.14,0,0,1-24.29,10.07l-21-16.78a81.59,81.59,0,0,1-9.64,0l-21,16.77a99.43,99.43,0,0,1-24.3-10l-3-26.71a81,81,0,0,1-6.81-6.81Z" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/></svg>
<span class="text-xl uppercase text-white group-hover:text-primary-light">Admin</span>
</a>
</li>
{% endif %}
<li class="py-4"> <li class="py-4">
<a href="{{ path('app_logout') }}" class="flex items-center space-x-2 group"> <a href="{{ path('app_logout') }}" class="flex items-center space-x-2 group">
<svg class="w-8 h-8 text-white group-hover:text-primary-light" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"/><polyline points="112 40 48 40 48 216 112 216" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/><line x1="112" y1="128" x2="224" y2="128" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/><polyline points="184 88 224 128 184 168" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/></svg> <svg class="w-8 h-8 text-white group-hover:text-primary-light" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256"><rect width="256" height="256" fill="none"/><polyline points="112 40 48 40 48 216 112 216" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/><line x1="112" y1="128" x2="224" y2="128" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/><polyline points="184 88 224 128 184 168" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="16"/></svg>
+62
View File
@@ -0,0 +1,62 @@
{% extends '@EasyAdmin/layout.html.twig' %}
{% block content_title %}
XML Dumps
{% endblock %}
{% block main %}
<div class="content-panel">
<div class="content-panel-header">
<h2>XML Dumps for Request ID</h2>
<code>{{ requestId }}</code>
</div>
<div class="content-panel-body">
{% if dumps is empty %}
<div class="alert alert-warning">
<i class="fa fa-exclamation-triangle"></i>
Keine XML-Dumps gefunden. Die Dateien wurden vermutlich bereits bereinigt.
</div>
{% else %}
<table class="table table-striped">
<thead>
<tr>
<th>Dateiname</th>
<th>Typ</th>
<th class="text-end">Dateigr&ouml;&szlig;e</th>
<th class="text-end">Aktion</th>
</tr>
</thead>
<tbody>
{% for dump in dumps %}
<tr>
<td><code>{{ dump.filename }}</code></td>
<td>
{% if dump.type == 'request' %}
<span class="badge bg-primary">Request</span>
{% else %}
<span class="badge bg-success">Response</span>
{% endif %}
</td>
<td class="text-end">{{ (dump.size / 1024)|number_format(1) }} KB</td>
<td class="text-end">
<a href="{{ path('admin_xml_dump_download', {filename: dump.filename}) }}"
class="btn btn-sm btn-outline-primary">
<i class="fa fa-download"></i> Download
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
</div>
<div class="content-panel-footer">
<a href="{{ ea_url().setController('App\\Controller\\Admin\\LogEntryCrudController').setAction('index') }}"
class="btn btn-secondary">
<i class="fa fa-arrow-left"></i> Zur&uuml;ck zur Liste
</a>
</div>
</div>
{% endblock %}