<?php
namespace App\EventSubscriber;
use App\Event\ActivityLogDataEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ControllerEventSubscriber implements EventSubscriberInterface
{
private EventDispatcherInterface $dispatcher;
public function __construct(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}
public function onKernelController(ControllerEvent $event): void
{
$request = $event->getRequest();
// Optional: Add conditions to control when to dispatch the custom event
if ($request->attributes->get('_route') === 'specific_route') {
$data = [
'controller' => get_class($event->getController()),
'route' => $request->attributes->get('_route'),
'timestamp' => time(),
];
// Dispatch the custom event
$activityLogEvent = new ActivityLogDataEvent($data);
$this->dispatcher->dispatch($activityLogEvent, 'activity_log.data_event');
}
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
];
}
}