src/EventListener/ExceptionListener.php line 64

  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: FogaPC
  5.  * Date: 12/02/2018
  6.  * Time: 20:52
  7.  */
  8. namespace App\EventListener;
  9. use App\Entity\RegistroAttivita;
  10. use Doctrine\Bundle\DoctrineBundle\Registry;
  11. use Doctrine\ORM\ORMException;
  12. use Psr\Log\LoggerInterface;;
  13. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  16. use Symfony\Component\HttpKernel\HttpKernelInterface;
  17. use Twig\Environment;
  18. class ExceptionListener
  19. {
  20.     protected $twig;
  21.     protected $doctrine;
  22.     protected $logger;
  23.     protected $env;
  24.     public function __construct(Environment $_twigRegistry $_doctrineLoggerInterface $_logger$_env)
  25.     {
  26.         $this->twig $_twig;
  27.         $this->doctrine $_doctrine;
  28.         $this->logger $_logger;
  29.         $this->env $_env;
  30.         return $this;
  31.     }
  32.     function ScriviRegistroAttivita($ip$debug$esito$messaggio$utente)
  33.     {
  34.         try {
  35.             $em $this->doctrine->getManager();
  36.             $registro = new RegistroAttivita();
  37.             $registro->setData(new \DateTime('now'));
  38.             $registro->setClientIp($ip);
  39.             $registro->setDebug($debug);
  40.             $registro->setEsito($esito);
  41.             $registro->setMessaggio($messaggio);
  42.             $registro->setUtente($utente);
  43.             if (!$em->isOpen()) {
  44.                 $this->entityManager $em->create(
  45.                     $em->getConnection(),
  46.                     $em->getConfiguration()
  47.                 );
  48.             }
  49.             $em->persist($registro);
  50.             $em->flush();
  51.         }catch (ORMException $ex){
  52.             $this->logger->critical("Errore inserimento registro attività --- " $messaggio);
  53.         }catch (\Exception $ex){
  54.             $this->logger->critical("Errore inserimento registro attività --- " $messaggio);
  55.         }
  56.     }
  57.     public function onKernelException(ExceptionEvent $event)
  58.     {
  59.         if ($this->env != "dev") {
  60.             // You get the exception object from the received event
  61.             $exception $event->getThrowable();
  62.             $message sprintf(
  63.                 'My Error says: %s with code: %s',
  64.                 $exception->getMessage(),
  65.                 $exception->getCode()
  66.             );
  67.             // Customize your response object to display the exception details
  68.             $response = new Response();
  69.             //$response->setContent($message);
  70.             $response->setContent($this->twig->render('ZZ_front_end/pagina_errore/errore.html.twig'));
  71.             $registro "Errore carico pagina<br>Carico pagina: " $event->getRequest()->getSchemeAndHttpHost() . $event->getRequest()->getRequestUri() . "<br>Linea: " $exception->getLine() . "<br>Eccezione: " $exception->getCode() . " - " $exception->getMessage() . "<br>" "Trace: " str_replace("\n""<br>"$exception->getTraceAsString());
  72.             $this->ScriviRegistroAttivita($event->getRequest()->getClientIp(), $exception->getTraceAsString(), false$registronull);
  73.             // HttpExceptionInterface is a special type of exception that
  74.             // holds status code and header details
  75.             if ($exception instanceof HttpExceptionInterface) {
  76.                 $response->setStatusCode($exception->getStatusCode());
  77.                 $response->headers->replace($exception->getHeaders());
  78.             } else {
  79.                 $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  80.             }
  81.             // sends the modified response object to the event
  82.             $event->setResponse($response);
  83.         }
  84.     }
  85. }