Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / symfony / console / EventListener / ErrorListener.php
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\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 <james.t.halsall@googlemail.com>
23  * @author Robin Chalas <robin.chalas@gmail.com>
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             return $this->logger->error('An error occurred while using the console. Message: "{message}"', array('exception' => $error, 'message' => $error->getMessage()));
44         }
45
46         $this->logger->error('Error thrown while running command "{command}". Message: "{message}"', array('exception' => $error, 'command' => $inputString, 'message' => $error->getMessage()));
47     }
48
49     public function onConsoleTerminate(ConsoleTerminateEvent $event)
50     {
51         if (null === $this->logger) {
52             return;
53         }
54
55         $exitCode = $event->getExitCode();
56
57         if (0 === $exitCode) {
58             return;
59         }
60
61         if (!$inputString = $this->getInputString($event)) {
62             return $this->logger->debug('The console exited with code "{code}"', array('code' => $exitCode));
63         }
64
65         $this->logger->debug('Command "{command}" exited with code "{code}"', array('command' => $inputString, 'code' => $exitCode));
66     }
67
68     public static function getSubscribedEvents()
69     {
70         return array(
71             ConsoleEvents::ERROR => array('onConsoleError', -128),
72             ConsoleEvents::TERMINATE => array('onConsoleTerminate', -128),
73         );
74     }
75
76     private static function getInputString(ConsoleEvent $event)
77     {
78         $commandName = $event->getCommand() ? $event->getCommand()->getName() : null;
79         $input = $event->getInput();
80
81         if (method_exists($input, '__toString')) {
82             if ($commandName) {
83                 return str_replace(array("'$commandName'", "\"$commandName\""), $commandName, (string) $input);
84             }
85
86             return (string) $input;
87         }
88
89         return $commandName;
90     }
91 }