[ Index ] |
PHP Cross Reference of phpBB-3.1.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\HttpKernel\EventListener; 13 14 use Psr\Log\LoggerInterface; 15 use Symfony\Component\Debug\Exception\FlattenException; 16 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; 17 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; 18 use Symfony\Component\HttpKernel\KernelEvents; 19 use Symfony\Component\HttpKernel\HttpKernelInterface; 20 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface; 21 use Symfony\Component\EventDispatcher\EventSubscriberInterface; 22 23 /** 24 * ExceptionListener. 25 * 26 * @author Fabien Potencier <fabien@symfony.com> 27 */ 28 class ExceptionListener implements EventSubscriberInterface 29 { 30 protected $controller; 31 protected $logger; 32 33 public function __construct($controller, LoggerInterface $logger = null) 34 { 35 $this->controller = $controller; 36 $this->logger = $logger; 37 } 38 39 public function onKernelException(GetResponseForExceptionEvent $event) 40 { 41 $exception = $event->getException(); 42 $request = $event->getRequest(); 43 44 $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine())); 45 46 $attributes = array( 47 '_controller' => $this->controller, 48 'exception' => FlattenException::create($exception), 49 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null, 50 // keep for BC -- as $format can be an argument of the controller callable 51 // see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php 52 // @deprecated in 2.4, to be removed in 3.0 53 'format' => $request->getRequestFormat(), 54 ); 55 56 $request = $request->duplicate(null, null, $attributes); 57 $request->setMethod('GET'); 58 59 try { 60 $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false); 61 } catch (\Exception $e) { 62 $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()), false); 63 64 $wrapper = $e; 65 66 while ($prev = $wrapper->getPrevious()) { 67 if ($exception === $wrapper = $prev) { 68 throw $e; 69 } 70 } 71 72 $prev = new \ReflectionProperty('Exception', 'previous'); 73 $prev->setAccessible(true); 74 $prev->setValue($wrapper, $exception); 75 76 throw $e; 77 } 78 79 $event->setResponse($response); 80 } 81 82 public static function getSubscribedEvents() 83 { 84 return array( 85 KernelEvents::EXCEPTION => array('onKernelException', -128), 86 ); 87 } 88 89 /** 90 * Logs an exception. 91 * 92 * @param \Exception $exception The \Exception instance 93 * @param string $message The error message to log 94 * @param bool $original False when the handling of the exception thrown another exception 95 */ 96 protected function logException(\Exception $exception, $message, $original = true) 97 { 98 $isCritical = !$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500; 99 $context = array('exception' => $exception); 100 if (null !== $this->logger) { 101 if ($isCritical) { 102 $this->logger->critical($message, $context); 103 } else { 104 $this->logger->error($message, $context); 105 } 106 } elseif (!$original || $isCritical) { 107 error_log($message); 108 } 109 } 110 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 11 00:25:41 2018 | Cross-referenced by PHPXref 0.7.1 |