src/Eccube/EventListener/ExceptionListener.php line 108

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\EventListener;
  13. use Eccube\Request\Context;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. class ExceptionListener implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var \Twig\Environment
  23.      */
  24.     private $twig;
  25.     /**
  26.      * @var Context
  27.      */
  28.     protected $requestContext;
  29.     /**
  30.      * ExceptionListener constructor.
  31.      */
  32.     public function __construct(\Twig\Environment $twigContext $requestContext)
  33.     {
  34.         $this->twig $twig;
  35.         $this->requestContext $requestContext;
  36.     }
  37.     public function onKernelException(ExceptionEvent $event)
  38.     {
  39.         $title trans('exception.error_title');
  40.         $message trans('exception.error_message');
  41.         $statusCode Response::HTTP_INTERNAL_SERVER_ERROR;
  42.         $exception $event->getThrowable();
  43.         if ($exception instanceof HttpExceptionInterface) {
  44.             $statusCode $exception->getStatusCode();
  45.             switch ($statusCode) {
  46.                 case 400:
  47.                 case 401:
  48.                 case 403:
  49.                 case 405:
  50.                 case 406:
  51.                     $infoMess 'アクセスできません。';
  52.                     $title trans('exception.error_title_can_not_access');
  53.                     if ($exception->getMessage()) {
  54.                         $message $exception->getMessage();
  55.                     } else {
  56.                         $message trans('exception.error_message_can_not_access');
  57.                     }
  58.                     break;
  59.                 case 429:
  60.                     $infoMess '試行回数の制限を超過しました。';
  61.                     $title trans('exception.error_title_can_not_access');
  62.                     $message trans('exception.error_message_rate_limit');
  63.                     break;
  64.                 case 404:
  65.                     $infoMess 'ページがみつかりません。';
  66.                     $title trans('exception.error_title_not_found');
  67.                     $message trans('exception.error_message_not_found');
  68.                     break;
  69.                 default:
  70.                     break;
  71.             }
  72.         }
  73.         if (isset($infoMess)) {
  74.             log_info($infoMess, [
  75.                 $exception->getMessage(),
  76.                 $exception->getFile(),
  77.                 $exception->getLine(),
  78.             ]);
  79.         } else {
  80.             log_error('システムエラーが発生しました。', [
  81.                 $exception->getMessage(),
  82.                 $exception->getFile(),
  83.                 $exception->getLine(),
  84.                 $exception->getTraceAsString(),
  85.             ]);
  86.         }
  87.         try {
  88.             $file $this->requestContext->isAdmin() ? '@admin/error.twig' 'error.twig';
  89.             $content $this->twig->render($file, [
  90.                 'error_title' => $title,
  91.                 'error_message' => $message,
  92.             ]);
  93.         } catch (\Exception $ignore) {
  94.             $content $title;
  95.         }
  96.         $event->setResponse(Response::create($content$statusCode));
  97.     }
  98.     /**
  99.      * Returns an array of event names this subscriber wants to listen to.
  100.      *
  101.      * The array keys are event names and the value can be:
  102.      *
  103.      *  * The method name to call (priority defaults to 0)
  104.      *  * An array composed of the method name to call and the priority
  105.      *  * An array of arrays composed of the method names to call and respective
  106.      *    priorities, or 0 if unset
  107.      *
  108.      * For instance:
  109.      *
  110.      *  * array('eventName' => 'methodName')
  111.      *  * array('eventName' => array('methodName', $priority))
  112.      *  * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
  113.      *
  114.      * @return array The event names to listen to
  115.      */
  116.     public static function getSubscribedEvents()
  117.     {
  118.         return [
  119.             KernelEvents::EXCEPTION => ['onKernelException'],
  120.         ];
  121.     }
  122. }