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

Open in your IDE?
  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.     private $logger;
  23.     public function __construct(LoggerInterface $logger null) {
  24.         $this->logger $logger;
  25.     }
  26.     public function onConsoleError(ConsoleErrorEvent $event) {
  27.         if (null === $this->logger) {
  28.             return;
  29.         }
  30.         $error $event->getError();
  31.         if (!$inputString $this->getInputString($event)) {
  32.             return $this->logger->error('An error occurred while using the console. Message: "{message}"', ['exception' => $error'message' => $error->getMessage()]);
  33.         }
  34.         $this->logger->error('Error thrown while running command "{command}". Message: "{message}"', ['exception' => $error'command' => $inputString'message' => $error->getMessage()]);
  35.     }
  36.     public function onConsoleTerminate(ConsoleTerminateEvent $event) {
  37.         if (null === $this->logger) {
  38.             return;
  39.         }
  40.         $exitCode $event->getExitCode();
  41.         if (=== $exitCode) {
  42.             return;
  43.         }
  44.         if (!$inputString $this->getInputString($event)) {
  45.             return $this->logger->debug('The console exited with code "{code}"', ['code' => $exitCode]);
  46.         }
  47.         $this->logger->debug('Command "{command}" exited with code "{code}"', ['command' => $inputString'code' => $exitCode]);
  48.     }
  49.     public static function getSubscribedEvents() {
  50.         return [
  51.             ConsoleEvents::ERROR => ['onConsoleError', -128],
  52.             ConsoleEvents::TERMINATE => ['onConsoleTerminate', -128],
  53.         ];
  54.     }
  55.     private static function getInputString(ConsoleEvent $event) {
  56.         $commandName $event->getCommand() ? $event->getCommand()->getName() : null;
  57.         $input $event->getInput();
  58.         if (method_exists($input'__toString')) {
  59.             if ($commandName) {
  60.                 return str_replace(["'$commandName'""\"$commandName\""], $commandName, (string) $input);
  61.             }
  62.             return (string) $input;
  63.         }
  64.         return $commandName;
  65.     }
  66. }