From fdeccd58491c04615a291914f6e1195c6a4c8465 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Tue, 29 Sep 2015 09:55:46 +0200 Subject: [PATCH] Updated the styles of the Twig commands --- .../Bridge/Twig/Command/DebugCommand.php | 15 +++--- .../Bridge/Twig/Command/LintCommand.php | 46 +++++++++++-------- .../Twig/Tests/Command/LintCommandTest.php | 6 +-- .../TwigBundle/Command/DebugCommand.php | 4 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/src/Symfony/Bridge/Twig/Command/DebugCommand.php b/src/Symfony/Bridge/Twig/Command/DebugCommand.php index dea7c4834e..bb37d99b21 100644 --- a/src/Symfony/Bridge/Twig/Command/DebugCommand.php +++ b/src/Symfony/Bridge/Twig/Command/DebugCommand.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; /** * Lists twig functions, filters, globals and tests present in the current project. @@ -82,10 +83,11 @@ EOF protected function execute(InputInterface $input, OutputInterface $output) { + $output = new SymfonyStyle($input, $output); $twig = $this->getTwigEnvironment(); if (null === $twig) { - $output->writeln('The Twig environment needs to be set.'); + $output->error('The Twig environment needs to be set.'); return 1; } @@ -118,14 +120,11 @@ EOF if (!$items) { continue; } - if ($index > 0) { - $output->writeln(''); - } - $output->writeln(''.ucfirst($type).''); + + $output->section(ucfirst($type)); + ksort($items); - foreach ($items as $item) { - $output->writeln(' '.$item); - } + $output->listing($items); } return 0; diff --git a/src/Symfony/Bridge/Twig/Command/LintCommand.php b/src/Symfony/Bridge/Twig/Command/LintCommand.php index 8cbf3c062e..44d53da3bd 100644 --- a/src/Symfony/Bridge/Twig/Command/LintCommand.php +++ b/src/Symfony/Bridge/Twig/Command/LintCommand.php @@ -16,6 +16,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; /** @@ -84,14 +85,17 @@ EOF protected function execute(InputInterface $input, OutputInterface $output) { + $stdout = $output; + $output = new SymfonyStyle($input, $output); + if (false !== strpos($input->getFirstArgument(), ':l')) { - $output->writeln('The use of "twig:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:twig" instead.'); + $output->caution('The use of "twig:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:twig" instead.'); } $twig = $this->getTwigEnvironment(); if (null === $twig) { - $output->writeln('The Twig environment needs to be set.'); + $output->error('The Twig environment needs to be set.'); return 1; } @@ -108,12 +112,12 @@ EOF $template .= fread(STDIN, 1024); } - return $this->display($input, $output, array($this->validate($twig, $template, uniqid('sf_')))); + return $this->display($input, $stdout, $output, array($this->validate($twig, $template, uniqid('sf_')))); } $filesInfo = $this->getFilesInfo($twig, $filenames); - return $this->display($input, $output, $filesInfo); + return $this->display($input, $stdout, $output, $filesInfo); } private function getFilesInfo(\Twig_Environment $twig, array $filenames) @@ -157,32 +161,36 @@ EOF return array('template' => $template, 'file' => $file, 'valid' => true); } - private function display(InputInterface $input, OutputInterface $output, $files) + private function display(InputInterface $input, OutputInterface $stdout, $output, $files) { switch ($input->getOption('format')) { case 'txt': - return $this->displayTxt($output, $files); + return $this->displayTxt($stdout, $output, $files); case 'json': - return $this->displayJson($output, $files); + return $this->displayJson($stdout, $files); default: throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format'))); } } - private function displayTxt(OutputInterface $output, $filesInfo) + private function displayTxt(OutputInterface $stdout, $output, $filesInfo) { $errors = 0; foreach ($filesInfo as $info) { - if ($info['valid'] && $output->isVerbose()) { - $output->writeln('OK'.($info['file'] ? sprintf(' in %s', $info['file']) : '')); + if ($info['valid'] && $stdout->isVerbose()) { + $output->comment('OK'.($info['file'] ? sprintf(' in %s', $info['file']) : '')); } elseif (!$info['valid']) { ++$errors; $this->renderException($output, $info['template'], $info['exception'], $info['file']); } } - $output->writeln(sprintf('%d/%d valid files', count($filesInfo) - $errors, count($filesInfo))); + if ($errors === 0) { + $output->success(sprintf('All %d Twig files contain valid syntax.', count($filesInfo))); + } else { + $output->warning(sprintf('%d Twig files have valid syntax and %d contain errors.', count($filesInfo) - $errors, $errors)); + } return min($errors, 1); } @@ -211,20 +219,20 @@ EOF $line = $exception->getTemplateLine(); if ($file) { - $output->writeln(sprintf('KO in %s (line %s)', $file, $line)); + $output->text(sprintf(' ERROR in %s (line %s)', $file, $line)); } else { - $output->writeln(sprintf('KO (line %s)', $line)); + $output->text(sprintf(' ERROR (line %s)', $line)); } - foreach ($this->getContext($template, $line) as $no => $code) { - $output->writeln(sprintf( + foreach ($this->getContext($template, $line) as $lineNumber => $code) { + $output->text(sprintf( '%s %-6s %s', - $no == $line ? '>>' : ' ', - $no, + $lineNumber === $line ? ' >> ' : ' ', + $lineNumber, $code )); - if ($no == $line) { - $output->writeln(sprintf('>> %s ', $exception->getRawMessage())); + if ($lineNumber === $line) { + $output->text(sprintf(' >> %s ', $exception->getRawMessage())); } } } diff --git a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php index 877e4b98eb..4c2aaeb924 100644 --- a/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php +++ b/src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php @@ -31,7 +31,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase $ret = $tester->execute(array('filename' => array($filename)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false)); $this->assertEquals(0, $ret, 'Returns 0 in case of success'); - $this->assertRegExp('/^OK in /', $tester->getDisplay()); + $this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay())); } public function testLintIncorrectFile() @@ -42,7 +42,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase $ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false)); $this->assertEquals(1, $ret, 'Returns 1 in case of error'); - $this->assertRegExp('/^KO in /', $tester->getDisplay()); + $this->assertRegExp('/^ERROR in /', trim($tester->getDisplay())); } /** @@ -65,7 +65,7 @@ class LintCommandTest extends \PHPUnit_Framework_TestCase $ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false)); $this->assertEquals(1, $ret, 'Returns 1 in case of error'); - $this->assertRegExp('/^KO in /', $tester->getDisplay()); + $this->assertRegExp('/^ERROR in /', trim($tester->getDisplay())); } /** diff --git a/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php b/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php index c4aa34098b..17ac48c436 100644 --- a/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php +++ b/src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php @@ -14,6 +14,7 @@ namespace Symfony\Bundle\TwigBundle\Command; use Symfony\Bridge\Twig\Command\DebugCommand as BaseDebugCommand; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerAwareInterface; @@ -57,8 +58,9 @@ class DebugCommand extends BaseDebugCommand implements ContainerAwareInterface protected function execute(InputInterface $input, OutputInterface $output) { + $output = new SymfonyStyle($input, $output); if (false !== strpos($input->getFirstArgument(), ':d')) { - $output->writeln('The use of "twig:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:twig" instead.'); + $output->caution('The use of "twig:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:twig" instead.'); } parent::execute($input, $output);