aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/console
diff options
context:
space:
mode:
authorTristan Darricau <tristan.darricau@sensiolabs.com>2015-08-24 12:04:22 +0200
committerTristan Darricau <tristan.darricau@sensiolabs.com>2015-08-25 22:24:28 +0200
commit17e7a89a60f700efc8a0b082b7a82005e6288e80 (patch)
tree628505c1a127ba5b531e4bb24bda4f659bb7317d /phpBB/phpbb/console
parentf6a4843c6df3a9b0490eb3d273ebed7d04a89582 (diff)
downloadforums-17e7a89a60f700efc8a0b082b7a82005e6288e80.tar
forums-17e7a89a60f700efc8a0b082b7a82005e6288e80.tar.gz
forums-17e7a89a60f700efc8a0b082b7a82005e6288e80.tar.bz2
forums-17e7a89a60f700efc8a0b082b7a82005e6288e80.tar.xz
forums-17e7a89a60f700efc8a0b082b7a82005e6288e80.zip
[ticket/14124] Automatically translate exceptions in CLI
PHPBB3-14124
Diffstat (limited to 'phpBB/phpbb/console')
-rw-r--r--phpBB/phpbb/console/application.php1
-rw-r--r--phpBB/phpbb/console/exception_subscriber.php74
2 files changed, 75 insertions, 0 deletions
diff --git a/phpBB/phpbb/console/application.php b/phpBB/phpbb/console/application.php
index 2c69a3cc73..f9f2213da6 100644
--- a/phpBB/phpbb/console/application.php
+++ b/phpBB/phpbb/console/application.php
@@ -13,6 +13,7 @@
namespace phpbb\console;
+use phpbb\exception\exception_interface;
use Symfony\Component\Console\Shell;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
diff --git a/phpBB/phpbb/console/exception_subscriber.php b/phpBB/phpbb/console/exception_subscriber.php
new file mode 100644
index 0000000000..b920d4abae
--- /dev/null
+++ b/phpBB/phpbb/console/exception_subscriber.php
@@ -0,0 +1,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',
+ );
+ }
+}