From 812fedcdc66ff0a9d45a1f769a633b9ecfeac891 Mon Sep 17 00:00:00 2001 From: ogizanagi Date: Sat, 9 May 2015 16:43:35 +0200 Subject: [PATCH] [FrameworkBundle] Applied new styles to translation commands --- .../Command/TranslationDebugCommand.php | 30 +++++-------- .../Command/TranslationUpdateCommand.php | 45 +++++++++++-------- .../Bundle/FrameworkBundle/composer.json | 2 +- 3 files changed, 37 insertions(+), 40 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php index 238b6400d6..79736ebd68 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationDebugCommand.php @@ -11,8 +11,8 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Translation\Catalogue\MergeOperation; -use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; @@ -85,8 +85,9 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { + $output = new SymfonyStyle($input, $output); if (false !== strpos($input->getFirstArgument(), ':d')) { - $output->writeln('The use of "translation:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:translation" instead.'); + $output->caution('The use of "translation:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:translation" instead.'); } $locale = $input->getArgument('locale'); @@ -106,7 +107,7 @@ EOF $rootPath = $input->getArgument('bundle'); if (!is_dir($rootPath)) { - throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $rootPath)); + throw new \InvalidArgumentException(sprintf('"%s" is neither an enabled bundle nor a directory.', $rootPath)); } } } @@ -135,13 +136,13 @@ EOF // No defined or extracted messages if (empty($allMessages) || null !== $domain && empty($allMessages[$domain])) { - $outputMessage = sprintf('No defined or extracted messages for locale "%s"', $locale); + $outputMessage = sprintf('No defined or extracted messages for locale "%s"', $locale); if (null !== $domain) { - $outputMessage .= sprintf(' and domain "%s"', $domain); + $outputMessage .= sprintf(' and domain "%s"', $domain); } - $output->writeln($outputMessage); + $output->warning($outputMessage); return; } @@ -161,19 +162,12 @@ EOF } } - if (class_exists('Symfony\Component\Console\Helper\Table')) { - $table = new Table($output); - } else { - $table = $this->getHelperSet()->get('table'); - } - // Display header line $headers = array('State', 'Domain', 'Id', sprintf('Message Preview (%s)', $locale)); foreach ($fallbackCatalogues as $fallbackCatalogue) { $headers[] = sprintf('Fallback Message Preview (%s)', $fallbackCatalogue->getLocale()); } - $table->setHeaders($headers); - + $rows = array(); // Iterate all message ids and determine their state foreach ($allMessages as $domain => $messages) { foreach (array_keys($messages) as $messageId) { @@ -206,15 +200,11 @@ EOF $row[] = $this->sanitizeString($fallbackCatalogue->get($messageId, $domain)); } - $table->addRow($row); + $rows[] = $row; } } - if (class_exists('Symfony\Component\Console\Helper\Table')) { - $table->render(); - } else { - $table->render($output); - } + $output->table($headers, $rows); } private function formatState($state) diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php index c78cfce7fc..39e1420cd9 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/TranslationUpdateCommand.php @@ -11,6 +11,7 @@ namespace Symfony\Bundle\FrameworkBundle\Command; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Translation\Catalogue\DiffOperation; use Symfony\Component\Translation\Catalogue\MergeOperation; use Symfony\Component\Console\Input\InputInterface; @@ -67,9 +68,10 @@ EOF */ protected function execute(InputInterface $input, OutputInterface $output) { + $output = new SymfonyStyle($input, $output); // check presence of force or dump-message if ($input->getOption('force') !== true && $input->getOption('dump-messages') !== true) { - $output->writeln('You must choose one of --force or --dump-messages'); + $output->error('You must choose one of --force or --dump-messages'); return 1; } @@ -78,8 +80,7 @@ EOF $writer = $this->getContainer()->get('translation.writer'); $supportedFormats = $writer->getFormats(); if (!in_array($input->getOption('output-format'), $supportedFormats)) { - $output->writeln('Wrong output format'); - $output->writeln('Supported formats are '.implode(', ', $supportedFormats).'.'); + $output->error(array('Wrong output format', 'Supported formats are '.implode(', ', $supportedFormats).'.')); return 1; } @@ -87,7 +88,7 @@ EOF // Define Root Path to App folder $rootPath = $kernel->getRootDir(); - $currentName = "app folder"; + $currentName = 'app folder'; // Override with provided Bundle info if (null !== $input->getArgument('bundle')) { @@ -106,20 +107,22 @@ EOF } } + $output->title('Symfony translation update command'); + // get bundle directory $translationsPath = $rootPath.'/Resources/translations'; - $output->writeln(sprintf('Generating "%s" translation files for "%s"', $input->getArgument('locale'), $currentName)); + $output->text(sprintf('Generating "%s" translation files for "%s"', $input->getArgument('locale'), $currentName)); // load any messages from templates $extractedCatalogue = new MessageCatalogue($input->getArgument('locale')); - $output->writeln('Parsing templates'); + $output->text('Parsing templates'); $extractor = $this->getContainer()->get('translation.extractor'); $extractor->setPrefix($input->getOption('prefix')); $extractor->extract($rootPath.'/Resources/views/', $extractedCatalogue); // load any existing messages from the translation files $currentCatalogue = new MessageCatalogue($input->getArgument('locale')); - $output->writeln('Loading translation files'); + $output->text('Loading translation files'); $loader = $this->getContainer()->get('translation.loader'); $loader->loadMessages($translationsPath, $currentCatalogue); @@ -130,26 +133,27 @@ EOF // Exit if no messages found. if (!count($operation->getDomains())) { - $output->writeln("\nNo translation found."); + $output->warning('No translation found.'); return; } // show compiled list of messages if ($input->getOption('dump-messages') === true) { + $output->newLine(); foreach ($operation->getDomains() as $domain) { - $output->writeln(sprintf("\nDisplaying messages for domain %s:\n", $domain)); + $output->section(sprintf('Displaying messages for domain %s:', $domain)); $newKeys = array_keys($operation->getNewMessages($domain)); $allKeys = array_keys($operation->getMessages($domain)); - foreach (array_diff($allKeys, $newKeys) as $id) { - $output->writeln($id); - } - foreach ($newKeys as $id) { - $output->writeln(sprintf('%s', $id)); - } - foreach (array_keys($operation->getObsoleteMessages($domain)) as $id) { - $output->writeln(sprintf('%s', $id)); - } + $output->listing(array_merge( + array_diff($allKeys, $newKeys), + array_map(function ($id) { + return sprintf('%s', $id); + }, $newKeys), + array_map(function($id) { + return sprintf('%s', $id); + }, array_keys($operation->getObsoleteMessages($domain))) + )); } if ($input->getOption('output-format') == 'xlf') { @@ -163,8 +167,11 @@ EOF // save the files if ($input->getOption('force') === true) { - $output->writeln('Writing files'); + $output->text('Writing files'); $writer->writeTranslations($operation->getResult(), $input->getOption('output-format'), array('path' => $translationsPath, 'default_locale' => $this->getContainer()->getParameter('kernel.default_locale'))); } + + $output->newLine(); + $output->success('Success'); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/composer.json b/src/Symfony/Bundle/FrameworkBundle/composer.json index 84c380085b..d56a84ed1d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/composer.json +++ b/src/Symfony/Bundle/FrameworkBundle/composer.json @@ -35,7 +35,7 @@ "require-dev": { "symfony/phpunit-bridge": "~2.7", "symfony/browser-kit": "~2.4", - "symfony/console": "~2.6", + "symfony/console": "~2.7", "symfony/css-selector": "~2.0,>=2.0.5", "symfony/dom-crawler": "~2.0,>=2.0.5", "symfony/finder": "~2.0,>=2.0.5",