vendor/symfony/console/EventListener/ErrorListener.php line 51

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Console\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\Console\ConsoleEvents;
  13. use Symfony\Component\Console\Event\ConsoleErrorEvent;
  14. use Symfony\Component\Console\Event\ConsoleEvent;
  15. use Symfony\Component\Console\Event\ConsoleTerminateEvent;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. /**
  18.  * @author James Halsall <james.t.halsall@googlemail.com>
  19.  * @author Robin Chalas <robin.chalas@gmail.com>
  20.  */
  21. class ErrorListener implements EventSubscriberInterface
  22. {
  23.     private ?LoggerInterface $logger;
  24.     public function __construct(?LoggerInterface $logger null)
  25.     {
  26.         $this->logger $logger;
  27.     }
  28.     /**
  29.      * @return void
  30.      */
  31.     public function onConsoleError(ConsoleErrorEvent $event)
  32.     {
  33.         if (null === $this->logger) {
  34.             return;
  35.         }
  36.         $error $event->getError();
  37.         if (!$inputString $this->getInputString($event)) {
  38.             $this->logger->critical('An error occurred while using the console. Message: "{message}"', ['exception' => $error'message' => $error->getMessage()]);
  39.             return;
  40.         }
  41.         $this->logger->critical('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error'command' => $inputString'message' => $error->getMessage()]);
  42.     }
  43.     /**
  44.      * @return void
  45.      */
  46.     public function onConsoleTerminate(ConsoleTerminateEvent $event)
  47.     {
  48.         if (null === $this->logger) {
  49.             return;
  50.         }
  51.         $exitCode $event->getExitCode();
  52.         if (=== $exitCode) {
  53.             return;
  54.         }
  55.         if (!$inputString $this->getInputString($event)) {
  56.             $this->logger->debug('The console exited with code "{code}"', ['code' => $exitCode]);
  57.             return;
  58.         }
  59.         $this->logger->debug('Command "{command}" exited with code "{code}"', ['command' => $inputString'code' => $exitCode]);
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             ConsoleEvents::ERROR => ['onConsoleError', -128],
  65.             ConsoleEvents::TERMINATE => ['onConsoleTerminate', -128],
  66.         ];
  67.     }
  68.     private static function getInputString(ConsoleEvent $event): ?string
  69.     {
  70.         $commandName $event->getCommand()?->getName();
  71.         $input $event->getInput();
  72.         if ($input instanceof \Stringable) {
  73.             if ($commandName) {
  74.                 return str_replace(["'$commandName'""\"$commandName\""], $commandName, (string) $input);
  75.             }
  76.             return (string) $input;
  77.         }
  78.         return $commandName;
  79.     }
  80. }