Merge pull request #3857 from Nicofuma/ticket/14124

[ticket/14124] Automatically translate exceptions in CLI
This commit is contained in:
Marc Alexander 2015-10-07 14:47:23 +02:00
commit da4d6dfa28
10 changed files with 119 additions and 4 deletions

View file

@ -77,5 +77,6 @@ $user->add_lang('cli');
$lang = $phpbb_container->get('language'); $lang = $phpbb_container->get('language');
$application = new \phpbb\console\application('phpBB Console', PHPBB_VERSION, $lang); $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->register_container_commands($phpbb_container->get('console.command_collection'));
$application->run($input); $application->run($input);

View file

@ -1,4 +1,12 @@
services: services:
console.exception_subscriber:
class: phpbb\console\exception_subscriber
arguments:
- @language
- %debug.exceptions%
tags:
- { name: kernel.event_subscriber }
console.command_collection: console.command_collection:
class: phpbb\di\service_collection class: phpbb\di\service_collection
arguments: arguments:

View file

@ -4,6 +4,9 @@ imports:
core: core:
require_dev_dependencies: true require_dev_dependencies: true
debug:
exceptions: true
twig: twig:
debug: true debug: true
auto_reload: true auto_reload: true

View file

@ -68,3 +68,11 @@ services:
- null - null
- @template.twig.loader - @template.twig.loader
- [] - []
console.exception_subscriber:
class: phpbb\console\exception_subscriber
arguments:
- @language
- %debug.exceptions%
tags:
- { name: kernel.event_subscriber }

View file

@ -62,5 +62,6 @@ $language = $phpbb_installer_container->get('language');
$language->add_lang(array('common', 'acp/common', 'acp/board', 'install_new', 'posting', 'cli')); $language->add_lang(array('common', 'acp/common', 'acp/board', 'install_new', 'posting', 'cli'));
$application = new \phpbb\console\application('phpBB Installer', PHPBB_VERSION, $language); $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->register_container_commands($phpbb_installer_container->get('console.installer.command_collection'));
$application->run($input); $application->run($input);

View file

@ -13,6 +13,7 @@
namespace phpbb\console\command\cron; namespace phpbb\console\command\cron;
use phpbb\exception\runtime_exception;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -93,8 +94,7 @@ class run extends \phpbb\console\command\command
} }
else else
{ {
$output->writeln('<error>' . $this->user->lang('CRON_LOCK_ERROR') . '</error>'); throw new runtime_exception('CRON_LOCK_ERROR', array(), null, 1);
return 1;
} }
} }
@ -165,8 +165,7 @@ class run extends \phpbb\console\command\command
} }
else else
{ {
$output->writeln('<error>' . $this->user->lang('CRON_NO_SUCH_TASK', $task_name) . '</error>'); throw new runtime_exception('CRON_NO_SUCH_TASK', array( $task_name), null, 2);
return 2;
} }
} }
} }

View file

@ -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',
);
}
}

View file

@ -31,6 +31,12 @@ class container_configuration implements ConfigurationInterface
$rootNode $rootNode
->children() ->children()
->booleanNode('require_dev_dependencies')->defaultValue(false)->end() ->booleanNode('require_dev_dependencies')->defaultValue(false)->end()
->arrayNode('debug')
->addDefaultsIfNotSet()
->children()
->booleanNode('exceptions')->defaultValue(false)->end()
->end()
->end()
->arrayNode('twig') ->arrayNode('twig')
->addDefaultsIfNotSet() ->addDefaultsIfNotSet()
->children() ->children()

View file

@ -80,6 +80,7 @@ class core extends Extension
{ {
$twig_environment_options['auto_reload'] = true; $twig_environment_options['auto_reload'] = true;
} }
// Replace the 8th argument, the options passed to the environment // Replace the 8th argument, the options passed to the environment
$definition->replaceArgument(7, $twig_environment_options); $definition->replaceArgument(7, $twig_environment_options);
@ -88,6 +89,12 @@ class core extends Extension
$definition = $container->getDefinition('template.twig.extensions.debug'); $definition = $container->getDefinition('template.twig.extensions.debug');
$definition->addTag('twig.extension'); $definition->addTag('twig.extension');
} }
// Set the debug options
foreach ($config['debug'] as $name => $value)
{
$container->setParameter('debug.' . $name, $value);
}
} }
/** /**

View file

@ -78,6 +78,10 @@ class phpbb_console_command_cron_run_test extends phpbb_database_test_case
$this->assertSame(false, $this->lock->owns_lock()); $this->assertSame(false, $this->lock->owns_lock());
} }
/**
* @expectedException \phpbb\exception\runtime_exception
* @expectedExceptionMessage CRON_LOCK_ERROR
*/
public function test_error_lock() public function test_error_lock()
{ {
$this->lock->acquire(); $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()); $this->assertSame(false, $this->lock->owns_lock());
} }
/**
* @expectedException \phpbb\exception\runtime_exception
* @expectedExceptionMessage CRON_NO_SUCH_TASK
*/
public function test_arg_invalid() public function test_arg_invalid()
{ {
$command_tester = $this->get_command_tester(); $command_tester = $this->get_command_tester();