src/EventSubscriber/DashboardExceptionSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  5. use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityRemoveException;
  6. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  7. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  12. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. class DashboardExceptionSubscriber implements EventSubscriberInterface {
  15.     /**
  16.      * @var SessionInterface
  17.      */
  18.     private $session;
  19.     /**
  20.      * @var AdminContextProvider
  21.      */
  22.     private $adminContextProvider;
  23.     /**
  24.      * @var AdminUrlGenerator
  25.      */
  26.     private $adminUrlGenerator;
  27.     public function __construct(SessionInterface $sessionAdminContextProvider $adminContextProviderAdminUrlGenerator $adminUrlGenerator
  28.     ) {
  29.         $this->session $session;
  30.         $this->adminContextProvider $adminContextProvider;
  31.         $this->adminUrlGenerator $adminUrlGenerator;
  32.     }
  33.     public static function getSubscribedEvents() {
  34.         return [ KernelEvents::EXCEPTION => ['onKernelException'] ];
  35.     }
  36.     public function sendFlashPrimary  ($title ""$message "") { $this->sendFlash("primary",   $title$message); }
  37.     public function sendFlashSecondary($title ""$message "") { $this->sendFlash("secondary"$title$message); }
  38.     public function sendFlashDark     ($title ""$message "") { $this->sendFlash("dark",      $title$message); }
  39.     public function sendFlashLight    ($title ""$message "") { $this->sendFlash("light",     $title$message); }
  40.     public function sendFlashSuccess  ($title ""$message "") { $this->sendFlash("success",   $title$message); }
  41.     public function sendFlashInfo     ($title ""$message "") { $this->sendFlash("info",      $title$message); }
  42.     public function sendFlashNotice   ($title ""$message "") { $this->sendFlash("notice",    $title$message); }
  43.     public function sendFlashWarning  ($title ""$message "") { $this->sendFlash("warning",   $title$message); }
  44.     public function sendFlashDanger   ($title ""$message "") { $this->sendFlash("danger",    $title$message); }
  45.     public function sendFlash($type$title ""$message ""): void
  46.     {
  47.         if($title instanceof ExceptionEvent) {
  48.             $event     $title;
  49.             $exception $event->getThrowable();
  50.             $title   get_class($exception)."<br/>";
  51.             $title  .= "(".$exception->getFile().":".$exception->getLine().")";
  52.             $message $exception->getMessage();
  53.         }
  54.         if(!empty($title)) $title "<b>".$title."</b><br/>";
  55.         if(!empty($title.$message))
  56.             $this->session->getFlashBag()->add($type$title.$message);
  57.     }
  58.     public function onKernelException(ExceptionEvent $event)
  59.     {
  60.         // Check if exception happened in EasyAdmin (avoid warning outside EA)
  61.         if(!$this->adminContextProvider) return;
  62.         if(!$this->adminContextProvider->getContext()) return;
  63.         // Get back exception & send flash message
  64.         $exception $event->getThrowable();
  65.         if ($exception instanceof EntityRemoveException) {
  66.             $this->sendFlashDanger('Dieses Element kann nicht gelöscht werden, da bereits abhängige Daten existieren.');
  67.         } else {
  68.             $this->sendFlashDanger($event);
  69.         }
  70.         // Get back crud information
  71.         $crud       $this->adminContextProvider->getContext()->getCrud();
  72.         if(!$crud) return;
  73.         $controller $crud->getControllerFqcn();
  74. //        dump($controller);
  75.         $action     $crud->getCurrentPage();
  76. //        dd($action);
  77.         // Avoid infinite redirection
  78.         // - If exception happened in "index", redirect to dashboard
  79.         // - If exception happened in an other section, redirect to index page first
  80.         // - If exception happened after submitting a form, just redirect to the initial page
  81.         $url $this->adminUrlGenerator->unsetAll();
  82.         switch($action) {
  83.             case "index": break;
  84.             default:
  85.                 $url $url->setController($controller);
  86.                 if($action && isset($_POST) && !empty($_POST)) {
  87.                     $url $url->setAction($action);
  88.                 }
  89.         }
  90.         $event->setResponse(new RedirectResponse($url));
  91.     }
  92. }