Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
exception_subscriber
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 on_error
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 getSubscribedEvents
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\console;
15
16use phpbb\exception\exception_interface;
17use Symfony\Component\Console\ConsoleEvents;
18use Symfony\Component\Console\Event\ConsoleErrorEvent;
19use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21class exception_subscriber implements EventSubscriberInterface
22{
23    /** @var \phpbb\language\language */
24    protected $language;
25
26    /**
27     * Constructor.
28     *
29     * @param \phpbb\language\language    $language    Language object
30     */
31    public function __construct(\phpbb\language\language $language)
32    {
33        $this->language = $language;
34    }
35
36    /**
37     * This listener is run when the ConsoleEvents::ERROR event is triggered.
38     * It translate the error message. If in debug mode the original exception is embedded.
39     *
40     * @param ConsoleErrorEvent $event
41     */
42    public function on_error(ConsoleErrorEvent $event)
43    {
44        $original_exception = $event->getError();
45
46        if ($original_exception instanceof exception_interface)
47        {
48            $parameters = array_merge([$original_exception->getMessage()], $original_exception->get_parameters());
49            $message = call_user_func_array([$this->language, 'lang'], $parameters);
50
51            $exception = new \RuntimeException($message , $original_exception->getCode(), $original_exception);
52
53            $event->setError($exception);
54        }
55
56        // If the exception has an exit code of 0, Symfony will suppress it by default which we don't want
57        if ($event->getExitCode() === 0 && $original_exception->getCode() === 0)
58        {
59            $event->setExitCode(1);
60        }
61    }
62
63    public static function getSubscribedEvents()
64    {
65        return [
66            ConsoleEvents::ERROR => 'on_error',
67        ];
68    }
69}