src/AppBundle/EventListener/ExceptionListener.php line 20

Open in your IDE?
  1. <?php
  2. namespace AppBundle\EventListener;
  3. use AppBundle\Service\GlobalParam;
  4. use Symfony\Component\DependencyInjection\Container;
  5. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  8. use Doctrine\DBAL\Exception\ConnectionException;
  9. class ExceptionListener {
  10.     protected $container;
  11.     public function __construct(Container $container) {
  12.         $this->container $container;
  13.     }
  14.     public function onKernelException(GetResponseForExceptionEvent $event) {
  15.         $request $event->getRequest();
  16.         // You get the exception object from the received event
  17.         $exception $event->getException();
  18.         $message sprintf(
  19.                 'Error: %s with code: %s on %s line %d'$exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine()
  20.         );
  21.         /** @var GlobalParam $global */
  22.         $global $this->container->get('app.global');
  23.         $errorViewParams $global->has('_error_view_params') ? $global->get('_error_view_params', []) : [];
  24.         $response = new Response();
  25.         $response->setContent($message);
  26.         if ($exception instanceof ConnectionException) {
  27.             //データベース接続エラー
  28.             $str "err";
  29.         } elseif ($exception instanceof HttpExceptionInterface) {
  30.             // HttpExceptionInterface is a special type of exception that
  31.             // holds status code and header details
  32.             if ($exception instanceof HttpExceptionInterface) {
  33.                 $status_code $exception->getStatusCode();
  34.                 $response->setStatusCode($status_code);
  35.                 $engine $this->container->get('templating');
  36.                 $parts explode('/'trim($request->getPathInfo(), '/'));
  37.                 //if(!isset($error_page)) return ;
  38.                 //$error_page = strtr($error_page, ['/' => \DIRECTORY_SEPARATOR, '\\' => \DIRECTORY_SEPARATOR]);
  39.                 $status_messages = [
  40.                     403 => 'Forbidden',
  41.                     404 => 'Not Found',
  42.                     500 => 'Internal Server Error'
  43.                 ];
  44.                 //if (file_exists($error_page)) {
  45.                 //    http_response_code($status_code);
  46.                 //    include $error_page;
  47.                 //    exit;
  48.                 //}
  49.                 $templates = [
  50.                     '403' => '::errors/403.html.php',
  51.                     '404' => '::errors/404.html.php',
  52.                     '500' => '::errors/500.html.php',
  53.                 ];
  54.                 if (isset($templates[$status_code])) {
  55.                     $response->headers->replace($exception->getHeaders());
  56.                     $template $templates[$status_code];
  57.                     $content $engine->render($templatearray_merge($errorViewParams, [
  58.                         'statusCode' => $status_code,
  59.                         'message' => $exception->getMessage(),
  60.                         'exception' => $exception,
  61.                         'showTrace' => in_array(CMS__ENVIRONMENT, [CMS__ENV_DEVELOP])
  62.                     ]));
  63.                     $response->setContent($content);
  64.                 }
  65.             } else {
  66.                 $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR);
  67.             }
  68.             // sends the modified response object to the event
  69.             $event->setResponse($response);
  70.         }
  71.     }
  72. }