Вход Регистрация
Файл: gapps/vendor/symfony/http-kernel/EventListener/ExceptionListener.php
Строк: 197
<?php

/*
 * This file is part of the Symfony package.
 *
 * (c) Fabien Potencier <fabien@symfony.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace SymfonyComponentHttpKernelEventListener;

use 
PsrLogLoggerInterface;
use 
SymfonyComponentDebugExceptionFlattenException;
use 
SymfonyComponentHttpFoundationRequest;
use 
SymfonyComponentHttpKernelLogDebugLoggerInterface;
use 
SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
use 
SymfonyComponentHttpKernelKernelEvents;
use 
SymfonyComponentHttpKernelHttpKernelInterface;
use 
SymfonyComponentHttpKernelExceptionHttpExceptionInterface;
use 
SymfonyComponentEventDispatcherEventSubscriberInterface;

/**
 * ExceptionListener.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class ExceptionListener implements EventSubscriberInterface
{
    protected 
$controller;
    protected 
$logger;

    public function 
__construct($controllerLoggerInterface $logger null)
    {
        
$this->controller $controller;
        
$this->logger $logger;
    }

    public function 
onKernelException(GetResponseForExceptionEvent $event)
    {
        
$exception $event->getException();
        
$request $event->getRequest();

        
$this->logException($exceptionsprintf('Uncaught PHP Exception %s: "%s" at %s line %s'get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));

        
$request $this->duplicateRequest($exception$request);

        try {
            
$response $event->getKernel()->handle($requestHttpKernelInterface::SUB_REQUESTfalse);
        } catch (
Exception $e) {
            
$this->logException($esprintf('Exception thrown when handling an exception (%s: %s at %s line %s)'get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()));

            
$wrapper $e;

            while (
$prev $wrapper->getPrevious()) {
                if (
$exception === $wrapper $prev) {
                    throw 
$e;
                }
            }

            
$prev = new ReflectionProperty('Exception''previous');
            
$prev->setAccessible(true);
            
$prev->setValue($wrapper$exception);

            throw 
$e;
        }

        
$event->setResponse($response);
    }

    public static function 
getSubscribedEvents()
    {
        return array(
            
KernelEvents::EXCEPTION => array('onKernelException', -128),
        );
    }

    
/**
     * Logs an exception.
     *
     * @param Exception $exception The Exception instance
     * @param string     $message   The error message to log
     */
    
protected function logException(Exception $exception$message)
    {
        if (
null !== $this->logger) {
            if (!
$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
                
$this->logger->critical($message, array('exception' => $exception));
            } else {
                
$this->logger->error($message, array('exception' => $exception));
            }
        }
    }

    
/**
     * Clones the request for the exception.
     *
     * @param Exception $exception The thrown exception
     * @param Request    $request   The original request
     *
     * @return Request $request The cloned request
     */
    
protected function duplicateRequest(Exception $exceptionRequest $request)
    {
        
$attributes = array(
            
'_controller' => $this->controller,
            
'exception' => FlattenException::create($exception),
            
'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
        );
        
$request $request->duplicate(nullnull$attributes);
        
$request->setMethod('GET');

        return 
$request;
    }
}
Онлайн: 1
Реклама