<?php
namespace Rawafed\AdminBundle\EventListener;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\Templating\EngineInterface;
use Rawafed\AdminBundle\Classes\AdminController;
class ExceptionHandler
{
private $enviroment;
private $templating;
private $logger;
private $controller;
public function __construct($enviroment, EngineInterface $templating, LoggerInterface $logger)
{
$this->enviroment = $enviroment;
$this->templating = $templating;
$this->logger = $logger;
}
private function render($view, array $parameters = array(), Response $response = null)
{
return $this->templating->renderResponse($view, $parameters, $response);
}
public function onKernelController(ControllerEvent $event)
{
$this->controller = $event->getController();
}
public function onKernelException(ExceptionEvent $event)
{
if ($this->enviroment != 'prod') {
return;
}
$exception = $event->getThrowable();
$this->logger->error($exception);
if ($this->controller) {
if ($this->controller[0] instanceof AdminController) {
$response = $this->render('AdminBundle:Admin:exception.html.twig', [
'exception' => $exception
]);
$event->setResponse($response);
}
}
}
}