<?php
namespace App\EventSubscriber;
use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class DashboardExceptionSubscriber implements EventSubscriberInterface {
/**
* @var SessionInterface
*/
private $session;
/**
* @var AdminContextProvider
*/
private $adminContextProvider;
/**
* @var AdminUrlGenerator
*/
private $adminUrlGenerator;
public function __construct(SessionInterface $session, AdminContextProvider $adminContextProvider, AdminUrlGenerator $adminUrlGenerator
) {
$this->session = $session;
$this->adminContextProvider = $adminContextProvider;
$this->adminUrlGenerator = $adminUrlGenerator;
}
public static function getSubscribedEvents() {
return [ KernelEvents::EXCEPTION => ['onKernelException'] ];
}
public function sendFlashPrimary ($title = "", $message = "") { $this->sendFlash("primary", $title, $message); }
public function sendFlashSecondary($title = "", $message = "") { $this->sendFlash("secondary", $title, $message); }
public function sendFlashDark ($title = "", $message = "") { $this->sendFlash("dark", $title, $message); }
public function sendFlashLight ($title = "", $message = "") { $this->sendFlash("light", $title, $message); }
public function sendFlashSuccess ($title = "", $message = "") { $this->sendFlash("success", $title, $message); }
public function sendFlashInfo ($title = "", $message = "") { $this->sendFlash("info", $title, $message); }
public function sendFlashNotice ($title = "", $message = "") { $this->sendFlash("notice", $title, $message); }
public function sendFlashWarning ($title = "", $message = "") { $this->sendFlash("warning", $title, $message); }
public function sendFlashDanger ($title = "", $message = "") { $this->sendFlash("danger", $title, $message); }
public function sendFlash($type, $title = "", $message = ""): void
{
if($title instanceof ExceptionEvent) {
$event = $title;
$exception = $event->getThrowable();
$title = get_class($exception)."<br/>";
$title .= "(".$exception->getFile().":".$exception->getLine().")";
$message = $exception->getMessage();
}
if(!empty($title)) $title = "<b>".$title."</b><br/>";
if(!empty($title.$message))
$this->session->getFlashBag()->add($type, $title.$message);
}
public function onKernelException(ExceptionEvent $event)
{
// Check if exception happened in EasyAdmin (avoid warning outside EA)
if(!$this->adminContextProvider) return;
if(!$this->adminContextProvider->getContext()) return;
// Get back exception & send flash message
$exception = $event->getThrowable();
if ($exception instanceof EntityRemoveException) {
$this->sendFlashDanger('Dieses Element kann nicht gelöscht werden, da bereits abhängige Daten existieren.');
} else {
$this->sendFlashDanger($event);
}
// Get back crud information
$crud = $this->adminContextProvider->getContext()->getCrud();
if(!$crud) return;
$controller = $crud->getControllerFqcn();
// dump($controller);
$action = $crud->getCurrentPage();
// dd($action);
// Avoid infinite redirection
// - If exception happened in "index", redirect to dashboard
// - If exception happened in an other section, redirect to index page first
// - If exception happened after submitting a form, just redirect to the initial page
$url = $this->adminUrlGenerator->unsetAll();
switch($action) {
case "index": break;
default:
$url = $url->setController($controller);
if($action && isset($_POST) && !empty($_POST)) {
$url = $url->setAction($action);
}
}
$event->setResponse(new RedirectResponse($url));
}
}