aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console/exception_subscriber.php
blob: b920d4abae25a09b9f14007089833d7b051e73c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
/**
 *
 * This file is part of the phpBB Forum Software package.
 *
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
 * @license GNU General Public License, version 2 (GPL-2.0)
 *
 * For full copyright and license information, please see
 * the docs/CREDITS.txt file.
 *
 */

namespace phpbb\console;

use phpbb\exception\exception_interface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleExceptionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class exception_subscriber implements EventSubscriberInterface
{
	/**
	 * @var \phpbb\language\language
	 */
	protected $language;

	/**
	 * Construct method
	 *
	 * @param \phpbb\language\language $language Language object
	 * @param bool $debug Debug mode
	 */
	public function __construct(\phpbb\language\language $language, $debug = false)
	{
		$this->language = $language;
		$this->debug = $debug;
	}

	/**
	 * This listener is run when the ConsoleEvents::EXCEPTION event is triggered.
	 * It translate the exception message. If din debug mode the original exception is embedded.
	 *
	 * @param ConsoleExceptionEvent $event
	 */
	public function on_exception(ConsoleExceptionEvent $event)
	{
		$original_exception = $event->getException();

		if ($original_exception instanceof exception_interface)
		{
			$parameters = array_merge(array($original_exception->getMessage()), $original_exception->get_parameters());
			$message = call_user_func_array(array($this->language, 'lang'), $parameters);

			if ($this->debug)
			{
				$exception = new \RuntimeException($message , $original_exception->getCode(), $original_exception);
			}
			else
			{
				$exception = new \RuntimeException($message , $original_exception->getCode());
			}

			$event->setException($exception);
		}
	}

	static public function getSubscribedEvents()
	{
		return array(
			ConsoleEvents::EXCEPTION => 'on_exception',
		);
	}
}