<?php 
 
namespace AppBundle\EventListener; 
 
use AppBundle\Service\GlobalParam; 
use Symfony\Component\DependencyInjection\Container; 
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; 
use Doctrine\DBAL\Exception\ConnectionException; 
 
class ExceptionListener { 
 
    protected $container; 
 
    public function __construct(Container $container) { 
        $this->container = $container; 
    } 
 
    public function onKernelException(GetResponseForExceptionEvent $event) { 
        $request = $event->getRequest(); 
 
        // You get the exception object from the received event 
        $exception = $event->getException(); 
        $message = sprintf( 
                'Error: %s with code: %s on %s line %d', $exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine() 
        ); 
 
        /** @var GlobalParam $global */ 
        $global = $this->container->get('app.global'); 
        $errorViewParams = $global->has('_error_view_params') ? $global->get('_error_view_params', []) : []; 
 
        $response = new Response(); 
        $response->setContent($message); 
 
        if ($exception instanceof ConnectionException) { 
            //データベース接続エラー 
            $str = "err"; 
        } elseif ($exception instanceof HttpExceptionInterface) { 
 
            // HttpExceptionInterface is a special type of exception that 
            // holds status code and header details 
            if ($exception instanceof HttpExceptionInterface) { 
                $status_code = $exception->getStatusCode(); 
                $response->setStatusCode($status_code); 
 
                $engine = $this->container->get('templating'); 
 
                $parts = explode('/', trim($request->getPathInfo(), '/')); 
 
                //if(!isset($error_page)) return ; 
                //$error_page = strtr($error_page, ['/' => \DIRECTORY_SEPARATOR, '\\' => \DIRECTORY_SEPARATOR]); 
 
                $status_messages = [ 
                    403 => 'Forbidden', 
                    404 => 'Not Found', 
                    500 => 'Internal Server Error' 
                ]; 
 
                //if (file_exists($error_page)) { 
                //    http_response_code($status_code); 
                //    include $error_page; 
                //    exit; 
                //} 
 
                $templates = [ 
                    '403' => '::errors/403.html.php', 
                    '404' => '::errors/404.html.php', 
                    '500' => '::errors/500.html.php', 
                ]; 
 
                if (isset($templates[$status_code])) { 
                    $response->headers->replace($exception->getHeaders()); 
                    $template = $templates[$status_code]; 
                    $content = $engine->render($template, array_merge($errorViewParams, [ 
                        'statusCode' => $status_code, 
                        'message' => $exception->getMessage(), 
                        'exception' => $exception, 
                        'showTrace' => in_array(CMS__ENVIRONMENT, [CMS__ENV_DEVELOP]) 
                    ])); 
 
                    $response->setContent($content); 
                } 
            } else { 
                $response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR); 
            } 
 
            // sends the modified response object to the event 
            $event->setResponse($response); 
        } 
    } 
}