From 17e7a89a60f700efc8a0b082b7a82005e6288e80 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Mon, 24 Aug 2015 12:04:22 +0200 Subject: [PATCH 1/4] [ticket/14124] Automatically translate exceptions in CLI PHPBB3-14124 --- phpBB/bin/phpbbcli.php | 1 + .../default/container/services_console.yml | 8 ++ phpBB/config/development/config.yml | 3 + phpBB/config/installer/container/services.yml | 8 ++ phpBB/install_new/phpbbcli.php | 1 + phpBB/phpbb/console/application.php | 1 + phpBB/phpbb/console/exception_subscriber.php | 74 +++++++++++++++++++ .../di/extension/container_configuration.php | 6 ++ phpBB/phpbb/di/extension/core.php | 7 ++ 9 files changed, 109 insertions(+) create mode 100644 phpBB/phpbb/console/exception_subscriber.php diff --git a/phpBB/bin/phpbbcli.php b/phpBB/bin/phpbbcli.php index 18657aed0a..d85b9a8d46 100755 --- a/phpBB/bin/phpbbcli.php +++ b/phpBB/bin/phpbbcli.php @@ -71,5 +71,6 @@ $user->add_lang('cli'); $lang = $phpbb_container->get('language'); $application = new \phpbb\console\application('phpBB Console', PHPBB_VERSION, $lang); +$application->setDispatcher($phpbb_container->get('dispatcher')); $application->register_container_commands($phpbb_container->get('console.command_collection')); $application->run($input); diff --git a/phpBB/config/default/container/services_console.yml b/phpBB/config/default/container/services_console.yml index aee3cbdee6..71b6244e13 100644 --- a/phpBB/config/default/container/services_console.yml +++ b/phpBB/config/default/container/services_console.yml @@ -1,4 +1,12 @@ services: + console.exception_subscriber: + class: phpbb\console\exception_subscriber + arguments: + - @language + - %debug.exceptions% + tags: + - { name: kernel.event_subscriber } + console.command_collection: class: phpbb\di\service_collection arguments: diff --git a/phpBB/config/development/config.yml b/phpBB/config/development/config.yml index 93ae07fb3f..f39eb52e73 100644 --- a/phpBB/config/development/config.yml +++ b/phpBB/config/development/config.yml @@ -4,6 +4,9 @@ imports: core: require_dev_dependencies: true + debug: + exceptions: true + twig: debug: true auto_reload: true diff --git a/phpBB/config/installer/container/services.yml b/phpBB/config/installer/container/services.yml index 8296bcc079..5cd30f9222 100644 --- a/phpBB/config/installer/container/services.yml +++ b/phpBB/config/installer/container/services.yml @@ -68,3 +68,11 @@ services: - null - @template.twig.loader - [] + + console.exception_subscriber: + class: phpbb\console\exception_subscriber + arguments: + - @language + - %debug.exceptions% + tags: + - { name: kernel.event_subscriber } diff --git a/phpBB/install_new/phpbbcli.php b/phpBB/install_new/phpbbcli.php index 508d051945..c1e9a9eaef 100644 --- a/phpBB/install_new/phpbbcli.php +++ b/phpBB/install_new/phpbbcli.php @@ -62,5 +62,6 @@ $language = $phpbb_installer_container->get('language'); $language->add_lang(array('common', 'acp/common', 'acp/board', 'install_new', 'posting', 'cli')); $application = new \phpbb\console\application('phpBB Installer', PHPBB_VERSION, $language); +$application->setDispatcher($phpbb_installer_container->get('dispatcher')); $application->register_container_commands($phpbb_installer_container->get('console.installer.command_collection')); $application->run($input); 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 @@ + + * @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', + ); + } +} diff --git a/phpBB/phpbb/di/extension/container_configuration.php b/phpBB/phpbb/di/extension/container_configuration.php index 4cc7c7c0d1..4585d6509e 100644 --- a/phpBB/phpbb/di/extension/container_configuration.php +++ b/phpBB/phpbb/di/extension/container_configuration.php @@ -31,6 +31,12 @@ class container_configuration implements ConfigurationInterface $rootNode ->children() ->booleanNode('require_dev_dependencies')->defaultValue(false)->end() + ->arrayNode('debug') + ->addDefaultsIfNotSet() + ->children() + ->booleanNode('exceptions')->defaultValue(false)->end() + ->end() + ->end() ->arrayNode('twig') ->addDefaultsIfNotSet() ->children() diff --git a/phpBB/phpbb/di/extension/core.php b/phpBB/phpbb/di/extension/core.php index 91b321a684..c48a80a558 100644 --- a/phpBB/phpbb/di/extension/core.php +++ b/phpBB/phpbb/di/extension/core.php @@ -80,6 +80,7 @@ class core extends Extension { $twig_environment_options['auto_reload'] = true; } + // Replace the 8th argument, the options passed to the environment $definition->replaceArgument(7, $twig_environment_options); @@ -88,6 +89,12 @@ class core extends Extension $definition = $container->getDefinition('template.twig.extensions.debug'); $definition->addTag('twig.extension'); } + + // Set the debug options + foreach ($config['debug'] as $name => $value) + { + $container->setParameter('debug.' . $name, $value); + } } /** From 2a07de70c2decb9669990286391cef1d427244bc Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Tue, 25 Aug 2015 22:24:37 +0200 Subject: [PATCH 2/4] [ticket/14124] Migrate cron:run exceptions PHPBB3-14124 --- phpBB/phpbb/console/command/cron/run.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/phpBB/phpbb/console/command/cron/run.php b/phpBB/phpbb/console/command/cron/run.php index 72ad1205ef..da185b81b3 100644 --- a/phpBB/phpbb/console/command/cron/run.php +++ b/phpBB/phpbb/console/command/cron/run.php @@ -13,6 +13,7 @@ namespace phpbb\console\command\cron; +use phpbb\exception\runtime_exception; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\OutputInterface; @@ -92,8 +93,7 @@ class run extends \phpbb\console\command\command } else { - $output->writeln('' . $this->user->lang('CRON_LOCK_ERROR') . ''); - return 1; + throw new runtime_exception('CRON_LOCK_ERROR', array(), null, 1); } } @@ -164,8 +164,7 @@ class run extends \phpbb\console\command\command } else { - $output->writeln('' . $this->user->lang('CRON_NO_SUCH_TASK', $task_name) . ''); - return 2; + throw new runtime_exception('CRON_NO_SUCH_TASK', array( $task_name), null, 2); } } } From 2e81771c6e18009a16b11b515e869a6a9afb0d2e Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 26 Aug 2015 12:49:18 +0200 Subject: [PATCH 3/4] [ticket/14124] Update cron tests PHPBB3-14124 --- tests/console/cron/run_test.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/console/cron/run_test.php b/tests/console/cron/run_test.php index 51ea49b282..d6c7b21781 100644 --- a/tests/console/cron/run_test.php +++ b/tests/console/cron/run_test.php @@ -78,6 +78,10 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case $this->assertSame(false, $this->lock->owns_lock()); } + /** + * @expectedException \phpbb\exception\runtime_exception + * @expectedExceptionMessage CRON_LOCK_ERROR + */ public function test_error_lock() { $this->lock->acquire(); @@ -126,6 +130,10 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case $this->assertSame(false, $this->lock->owns_lock()); } + /** + * @expectedException \phpbb\exception\runtime_exception + * @expectedExceptionMessage CRON_NO_SUCH_TASK + */ public function test_arg_invalid() { $command_tester = $this->get_command_tester(); From d13d66fc01688e0e9f49bae5cf368dc16220aa40 Mon Sep 17 00:00:00 2001 From: Tristan Darricau Date: Wed, 26 Aug 2015 14:09:42 +0200 Subject: [PATCH 4/4] [ticket/14124] CS PHPBB3-14124 --- phpBB/phpbb/console/application.php | 1 - 1 file changed, 1 deletion(-) diff --git a/phpBB/phpbb/console/application.php b/phpBB/phpbb/console/application.php index f9f2213da6..2c69a3cc73 100644 --- a/phpBB/phpbb/console/application.php +++ b/phpBB/phpbb/console/application.php @@ -13,7 +13,6 @@ 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;