[ticket/12610] Use Symfony style guide

PHPBB3-12610
This commit is contained in:
Tristan Darricau 2016-12-04 17:43:51 +01:00
parent 6c35ca80ed
commit 32aa0596f3
No known key found for this signature in database
GPG key ID: 817043C2E29DB881
2 changed files with 87 additions and 87 deletions

View file

@ -226,6 +226,7 @@ services:
- '@user' - '@user'
- '@config' - '@config'
- '@service_container' - '@service_container'
- '@language'
tags: tags:
- { name: console.command } - { name: console.command }

View file

@ -21,6 +21,7 @@ use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
class check extends \phpbb\console\command\command class check extends \phpbb\console\command\command
@ -40,13 +41,13 @@ class check extends \phpbb\console\command\command
*/ */
public function __construct(user $user, config $config, ContainerInterface $phpbb_container, language $language) public function __construct(user $user, config $config, ContainerInterface $phpbb_container, language $language)
{ {
parent::__construct($user);
$this->config = $config; $this->config = $config;
$this->phpbb_container = $phpbb_container; $this->phpbb_container = $phpbb_container;
$this->language = $language; $this->language = $language;
$this->language->add_lang(array('acp/common', 'acp/extensions')); $this->language->add_lang(array('acp/common', 'acp/extensions'));
parent::__construct($user);
} }
/** /**
@ -81,6 +82,8 @@ class check extends \phpbb\console\command\command
*/ */
protected function execute(InputInterface $input, OutputInterface $output) protected function execute(InputInterface $input, OutputInterface $output)
{ {
$io = new SymfonyStyle($input, $output);
$recheck = true; $recheck = true;
if ($input->getOption('cache')) if ($input->getOption('cache'))
{ {
@ -93,7 +96,8 @@ class check extends \phpbb\console\command\command
$stability = $input->getOption('stability'); $stability = $input->getOption('stability');
if (!($stability == 'stable') && !($stability == 'unstable')) if (!($stability == 'stable') && !($stability == 'unstable'))
{ {
throw new \RuntimeException($this->language->lang('CLI_ERROR_INVALID_STABILITY', $stability)); $io->error($this->language->lang('CLI_ERROR_INVALID_STABILITY', $stability));
return 3;
} }
} }
@ -102,30 +106,29 @@ class check extends \phpbb\console\command\command
{ {
if ($ext_name == 'all') if ($ext_name == 'all')
{ {
return $this->check_all_ext($input, $output, $stability, $recheck); return $this->check_all_ext($io, $stability, $recheck);
} }
else else
{ {
return $this->check_ext($input, $output, $stability, $recheck, $ext_name); return $this->check_ext($io, $stability, $recheck, $ext_name);
} }
} }
else else
{ {
return $this->check_core($input, $output, $stability, $recheck); return $this->check_core($io,$stability, $recheck);
} }
} }
/** /**
* Check if a given extension is up to date * Check if a given extension is up to date
* *
* @param InputInterface $input Input stream, used to get the options. * @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param OutputInterface $output Output stream, used to print messages. * @param string $stability Force a given stability
* @param OutputInterface $stability Force a given stability * @param bool $recheck Disallow the use of the cache
* @param bool $recheck Disallow the use of the cache * @param string $ext_name The extension name
* @param string $ext_name The extension name * @return int
* @return int */
*/ protected function check_ext(SymfonyStyle $io, $stability, $recheck, $ext_name)
protected function check_ext(InputInterface $input, OutputInterface $output, $stability, $recheck, $ext_name)
{ {
try try
{ {
@ -134,34 +137,29 @@ class check extends \phpbb\console\command\command
$updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability); $updates_available = $ext_manager->version_check($md_manager, $recheck, false, $stability);
$metadata = $md_manager->get_metadata('all'); $metadata = $md_manager->get_metadata('all');
if ($input->getOption('verbose')) if ($io->isVerbose())
{ {
$output->writeln('<info>' . $md_manager->get_metadata('display-name') . '</info>'); $io->title($md_manager->get_metadata('display-name'));
$output->writeln('');
$output->writeln('<comment>' . $this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . '</comment> ' . $metadata['version']); $io->note($this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $metadata['version']);
} }
if (!empty($updates_available)) if (!empty($updates_available))
{ {
$output->writeln(''); if ($io->isVerbose())
$output->writeln('<question>' . $this->language->lang('NOT_UP_TO_DATE', $metadata['name']) . '</question>');
if ($input->getOption('verbose'))
{ {
$this->display_versions($output, $updates_available); $io->caution($this->language->lang('NOT_UP_TO_DATE', $metadata['name']));
$this->display_versions($io, $updates_available);
} }
return 1; return 1;
} }
else else
{ {
$output->writeln(''); if ($io->isVerbose())
$output->writeln('<question>' . $this->language->lang('NOT_UP_TO_DATE', $metadata['name']) . '</question>');
if ($input->getOption('verbose'))
{ {
$output->writeln('<info>' . $this->language->lang('UPDATE_NOT_NEEDED') . '</info>'); $io->success($this->language->lang('UPDATE_NOT_NEEDED'));
} }
return 0; return 0;
@ -169,54 +167,50 @@ class check extends \phpbb\console\command\command
} }
catch (\RuntimeException $e) catch (\RuntimeException $e)
{ {
$output->writeln('<error>'.$this->language->lang('EXTENSION_NOT_INSTALLED', $ext_name).'</error>'); $io->error($this->language->lang('EXTENSION_NOT_INSTALLED', $ext_name));
return 1; return 1;
} }
} }
/** /**
* Check if the core is up to date * Check if the core is up to date
* *
* @param InputInterface $input Input stream, used to get the options. * @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param OutputInterface $output Output stream, used to print messages. * @param string $stability Force a given stability
* @param OutputInterface $stability Force a given stability * @param bool $recheck Disallow the use of the cache
* @param bool $recheck Disallow the use of the cache * @return int
* @return int */
*/ protected function check_core(SymfonyStyle $io, $stability, $recheck)
protected function check_core(InputInterface $input, OutputInterface $output, $stability, $recheck)
{ {
$version_helper = $this->phpbb_container->get('version_helper'); $version_helper = $this->phpbb_container->get('version_helper');
$version_helper->force_stability($stability); $version_helper->force_stability($stability);
$updates_available = $version_helper->get_suggested_updates($recheck); $updates_available = $version_helper->get_suggested_updates($recheck);
if ($input->getOption('verbose')) if ($io->isVerbose())
{ {
$output->writeln('<info>phpBB core</info>'); $io->title('phpBB core');
$output->writeln('');
$output->writeln('<comment>' . $this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . '</comment> ' . $this->config['version']); $io->note( $this->language->lang('CURRENT_VERSION') . $this->language->lang('COLON') . ' ' . $this->config['version']);
} }
if (!empty($updates_available)) if (!empty($updates_available))
{ {
$output->writeln(''); $io->caution($this->language->lang('UPDATE_NEEDED'));
$output->writeln('<question>' . $this->language->lang('UPDATE_NEEDED') . '</question>');
if ($input->getOption('verbose')) if ($io->isVerbose())
{ {
$this->display_versions($output, $updates_available); $this->display_versions($io, $updates_available);
} }
return 1; return 1;
} }
else else
{ {
if ($input->getOption('verbose')) if ($io->isVerbose())
{ {
$output->writeln(''); $io->success($this->language->lang('UPDATE_NOT_NEEDED'));
$output->writeln('<question>' . $this->language->lang('UPDATE_NOT_NEEDED') . '</question>');
} }
return 0; return 0;
@ -226,27 +220,22 @@ class check extends \phpbb\console\command\command
/** /**
* Check if all the available extensions are up to date * Check if all the available extensions are up to date
* *
* @param InputInterface $input Input stream, used to get the options. * @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param OutputInterface $output Output stream, used to print messages. * @param bool $recheck Disallow the use of the cache
* @param OutputInterface $stability Force a given stability
* @param bool $recheck Disallow the use of the cache
* @return int * @return int
*/ */
protected function check_all_ext(InputInterface $input, OutputInterface $output, $stability, $recheck) protected function check_all_ext(SymfonyStyle $io, $stability, $recheck)
{ {
/** @var \phpbb\extension\manager $ext_manager */ /** @var \phpbb\extension\manager $ext_manager */
$ext_manager = $this->phpbb_container->get('ext.manager'); $ext_manager = $this->phpbb_container->get('ext.manager');
$ext_name_length = max(30, strlen($this->language->lang('EXTENSION_NAME'))); $rows = [];
$current_version_length = max(15, strlen($this->language->lang('CURRENT_VERSION')));
$latest_version_length = max(15, strlen($this->language->lang('LATEST_VERSION')));
$output->writeln(sprintf("%-{$ext_name_length}s | %-{$current_version_length}s | %s", $this->language->lang('EXTENSION_NAME'), $this->language->lang('CURRENT_VERSION'), $this->language->lang('LATEST_VERSION')));
$output->writeln(sprintf("%'-{$ext_name_length}s-+-%'-{$current_version_length}s-+-%'-{$latest_version_length}s", '', '', ''));
foreach ($ext_manager->all_available() as $ext_name => $ext_path) foreach ($ext_manager->all_available() as $ext_name => $ext_path)
{ {
$message = sprintf("<info>%-{$ext_name_length}s</info>", $ext_name); $row = [];
$md_manager = $ext_manager->create_extension_metadata_manager($ext_name, null); $row[] = sprintf("<info>%s</info>", $ext_name);
$md_manager = $ext_manager->create_extension_metadata_manager($ext_name);
try try
{ {
$metadata = $md_manager->get_metadata('all'); $metadata = $md_manager->get_metadata('all');
@ -261,70 +250,80 @@ class check extends \phpbb\console\command\command
return $entry['current']; return $entry['current'];
}, $updates_available); }, $updates_available);
$message .= sprintf(" | <comment>%-{$current_version_length}s</comment> | %s", $row[] = sprintf("<comment>%s</comment>", $metadata['version']);
$metadata['version'], $row[] = implode(', ', $versions);
implode(', ', $versions)
);
} }
else else
{ {
$message .= sprintf(" | <info>%-{$current_version_length}s</info> | ", $row[] = sprintf("<info>%s</info>", $metadata['version']);
$metadata['version'] $row[] = '';
);
} }
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$message .= ' | '; $row[] = $metadata['version'];
$row[] = '';
} }
} }
else else
{ {
$message .= sprintf(" | %-{$current_version_length}s | ", $metadata['version']); $row[] = $metadata['version'];
$row[] = '';
} }
} }
catch (exception_interface $e) catch (exception_interface $e)
{ {
$exception_message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters())); $exception_message = call_user_func_array(array($this->user, 'lang'), array_merge(array($e->getMessage()), $e->get_parameters()));
$message .= ('<error>' . $exception_message . '</error>'); $row[] = '<error>' . $exception_message . '</error>';
} }
catch (\RuntimeException $e) catch (\RuntimeException $e)
{ {
$message .= ('<error>' . $e->getMessage() . '</error>'); $row[] = '<error>' . $e->getMessage() . '</error>';
} }
$output->writeln($message); $rows[] = $row;
} }
$io->table([
$this->language->lang('EXTENSION_NAME'),
$this->language->lang('CURRENT_VERSION'),
$this->language->lang('LATEST_VERSION'),
], $rows);
return 0; return 0;
} }
/** /**
* Display the details of the available updates * Display the details of the available updates
* *
* @param OutputInterface $output Output stream, used to print messages. * @param SymfonyStyle $io IO handler, for formatted and unified IO
* @param array $updates_available The list of the available updates * @param array $updates_available The list of the available updates
*/ */
protected function display_versions(OutputInterface $output, $updates_available) protected function display_versions(SymfonyStyle $io, $updates_available)
{ {
$output->writeln(''); $io->section($this->language->lang('UPDATES_AVAILABLE'));
$output->writeln('<comment>' . $this->language->lang('UPDATES_AVAILABLE') . '</comment>');
$rows = [];
foreach ($updates_available as $version_data) foreach ($updates_available as $version_data)
{ {
$messages = array(); $row = ['', '', ''];
$messages[] = sprintf("\t%-30s| %s", $this->language->lang('VERSION'), $version_data['current']); $row[0] = $version_data['current'];
if (isset($version_data['announcement'])) if (isset($version_data['announcement']))
{ {
$messages[] = sprintf("\t%-30s| %s", $this->language->lang('ANNOUNCEMENT_TOPIC'), $version_data['announcement']); $row[1] = $version_data['announcement'];
} }
if (isset($version_data['download'])) if (isset($version_data['download']))
{ {
$messages[] = sprintf("\t%-30s| %s", $this->language->lang('DOWNLOAD_LATEST'), $version_data['download']); $row[2] = $version_data['download'];
} }
$messages[] = ''; $rows[] = $row;
$output->writeln(implode("\n", $messages));
} }
$io->table([
$this->language->lang('VERSION'),
$this->language->lang('ANNOUNCEMENT_TOPIC'),
$this->language->lang('DOWNLOAD_LATEST'),
], $rows);
} }
} }