[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/EventListener/ -> DebugHandlersListener.php (source)

   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\Console\ConsoleEvents;
  16  use Symfony\Component\Console\Event\ConsoleEvent;
  17  use Symfony\Component\Console\Output\ConsoleOutputInterface;
  18  use Symfony\Component\Debug\ErrorHandler;
  19  use Symfony\Component\Debug\ExceptionHandler;
  20  use Symfony\Component\EventDispatcher\Event;
  21  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22  use Symfony\Component\HttpKernel\Event\KernelEvent;
  23  use Symfony\Component\HttpKernel\KernelEvents;
  24  
  25  /**
  26   * Configures errors and exceptions handlers.
  27   *
  28   * @author Nicolas Grekas <p@tchwork.com>
  29   */
  30  class DebugHandlersListener implements EventSubscriberInterface
  31  {
  32      private $exceptionHandler;
  33      private $logger;
  34      private $levels;
  35      private $throwAt;
  36      private $scream;
  37      private $fileLinkFormat;
  38      private $firstCall = true;
  39      private $hasTerminatedWithException;
  40  
  41      /**
  42       * @param callable|null        $exceptionHandler A handler that will be called on Exception
  43       * @param LoggerInterface|null $logger           A PSR-3 logger
  44       * @param array|int            $levels           An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  45       * @param int|null             $throwAt          Thrown errors in a bit field of E_* constants, or null to keep the current value
  46       * @param bool                 $scream           Enables/disables screaming mode, where even silenced errors are logged
  47       * @param string               $fileLinkFormat   The format for links to source files
  48       */
  49      public function __construct($exceptionHandler, LoggerInterface $logger = null, $levels = null, $throwAt = -1, $scream = true, $fileLinkFormat = null)
  50      {
  51          $this->exceptionHandler = $exceptionHandler;
  52          $this->logger = $logger;
  53          $this->levels = $levels;
  54          $this->throwAt = is_numeric($throwAt) ? (int) $throwAt : (null === $throwAt ? null : ($throwAt ? -1 : null));
  55          $this->scream = (bool) $scream;
  56          $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
  57      }
  58  
  59      /**
  60       * Configures the error handler.
  61       */
  62      public function configure(Event $event = null)
  63      {
  64          if (!$event instanceof KernelEvent ? !$this->firstCall : !$event->isMasterRequest()) {
  65              return;
  66          }
  67          $this->firstCall = $this->hasTerminatedWithException = false;
  68  
  69          $handler = set_exception_handler('var_dump');
  70          $handler = \is_array($handler) ? $handler[0] : null;
  71          restore_exception_handler();
  72  
  73          if ($this->logger || null !== $this->throwAt) {
  74              if ($handler instanceof ErrorHandler) {
  75                  if ($this->logger) {
  76                      $handler->setDefaultLogger($this->logger, $this->levels);
  77                      if (\is_array($this->levels)) {
  78                          $scream = 0;
  79                          foreach ($this->levels as $type => $log) {
  80                              $scream |= $type;
  81                          }
  82                      } else {
  83                          $scream = null === $this->levels ? E_ALL | E_STRICT : $this->levels;
  84                      }
  85                      if ($this->scream) {
  86                          $handler->screamAt($scream);
  87                      }
  88                      $this->logger = $this->levels = null;
  89                  }
  90                  if (null !== $this->throwAt) {
  91                      $handler->throwAt($this->throwAt, true);
  92                  }
  93              }
  94          }
  95          if (!$this->exceptionHandler) {
  96              if ($event instanceof KernelEvent) {
  97                  if (method_exists($kernel = $event->getKernel(), 'terminateWithException')) {
  98                      $request = $event->getRequest();
  99                      $hasRun = &$this->hasTerminatedWithException;
 100                      $this->exceptionHandler = function (\Exception $e) use ($kernel, $request, &$hasRun) {
 101                          if ($hasRun) {
 102                              throw $e;
 103                          }
 104                          $hasRun = true;
 105                          $kernel->terminateWithException($e, $request);
 106                      };
 107                  }
 108              } elseif ($event instanceof ConsoleEvent && $app = $event->getCommand()->getApplication()) {
 109                  $output = $event->getOutput();
 110                  if ($output instanceof ConsoleOutputInterface) {
 111                      $output = $output->getErrorOutput();
 112                  }
 113                  $this->exceptionHandler = function ($e) use ($app, $output) {
 114                      $app->renderException($e, $output);
 115                  };
 116              }
 117          }
 118          if ($this->exceptionHandler) {
 119              if ($handler instanceof ErrorHandler) {
 120                  $h = $handler->setExceptionHandler('var_dump');
 121                  if (\is_array($h) && $h[0] instanceof ExceptionHandler) {
 122                      $handler->setExceptionHandler($h);
 123                      $handler = $h[0];
 124                  } else {
 125                      $handler->setExceptionHandler($this->exceptionHandler);
 126                  }
 127              }
 128              if ($handler instanceof ExceptionHandler) {
 129                  $handler->setHandler($this->exceptionHandler);
 130                  if (null !== $this->fileLinkFormat) {
 131                      $handler->setFileLinkFormat($this->fileLinkFormat);
 132                  }
 133              }
 134              $this->exceptionHandler = null;
 135          }
 136      }
 137  
 138      public static function getSubscribedEvents()
 139      {
 140          $events = array(KernelEvents::REQUEST => array('configure', 2048));
 141  
 142          if (\defined('Symfony\Component\Console\ConsoleEvents::COMMAND')) {
 143              $events[ConsoleEvents::COMMAND] = array('configure', 2048);
 144          }
 145  
 146          return $events;
 147      }
 148  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1