[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/symfony/console/EventListener/ -> ErrorListener.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <[email protected]>
   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\Console\EventListener;
  13  
  14  use Psr\Log\LoggerInterface;
  15  use Symfony\Component\Console\ConsoleEvents;
  16  use Symfony\Component\Console\Event\ConsoleErrorEvent;
  17  use Symfony\Component\Console\Event\ConsoleEvent;
  18  use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  19  use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20  
  21  /**
  22   * @author James Halsall <[email protected]>
  23   * @author Robin Chalas <[email protected]>
  24   */
  25  class ErrorListener implements EventSubscriberInterface
  26  {
  27      private $logger;
  28  
  29      public function __construct(LoggerInterface $logger = null)
  30      {
  31          $this->logger = $logger;
  32      }
  33  
  34      public function onConsoleError(ConsoleErrorEvent $event)
  35      {
  36          if (null === $this->logger) {
  37              return;
  38          }
  39  
  40          $error = $event->getError();
  41  
  42          if (!$inputString = $this->getInputString($event)) {
  43              $this->logger->critical('An error occurred while using the console. Message: "{message}"', ['exception' => $error, 'message' => $error->getMessage()]);
  44  
  45              return;
  46          }
  47  
  48          $this->logger->critical('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error, 'command' => $inputString, 'message' => $error->getMessage()]);
  49      }
  50  
  51      public function onConsoleTerminate(ConsoleTerminateEvent $event)
  52      {
  53          if (null === $this->logger) {
  54              return;
  55          }
  56  
  57          $exitCode = $event->getExitCode();
  58  
  59          if (0 === $exitCode) {
  60              return;
  61          }
  62  
  63          if (!$inputString = $this->getInputString($event)) {
  64              $this->logger->debug('The console exited with code "{code}"', ['code' => $exitCode]);
  65  
  66              return;
  67          }
  68  
  69          $this->logger->debug('Command "{command}" exited with code "{code}"', ['command' => $inputString, 'code' => $exitCode]);
  70      }
  71  
  72      public static function getSubscribedEvents()
  73      {
  74          return [
  75              ConsoleEvents::ERROR => ['onConsoleError', -128],
  76              ConsoleEvents::TERMINATE => ['onConsoleTerminate', -128],
  77          ];
  78      }
  79  
  80      private static function getInputString(ConsoleEvent $event): ?string
  81      {
  82          $commandName = $event->getCommand() ? $event->getCommand()->getName() : null;
  83          $input = $event->getInput();
  84  
  85          if (method_exists($input, '__toString')) {
  86              if ($commandName) {
  87                  return str_replace(["'$commandName'", "\"$commandName\""], $commandName, (string) $input);
  88              }
  89  
  90              return (string) $input;
  91          }
  92  
  93          return $commandName;
  94      }
  95  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer