Updated the styles of the Twig commands

This commit is contained in:
Javier Eguiluz 2015-09-29 09:55:46 +02:00 committed by Fabien Potencier
parent bee1faaa95
commit fdeccd5849
4 changed files with 40 additions and 31 deletions

View File

@ -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('<error>The Twig environment needs to be set.</error>');
$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('<info>'.ucfirst($type).'</info>');
$output->section(ucfirst($type));
ksort($items);
foreach ($items as $item) {
$output->writeln(' '.$item);
}
$output->listing($items);
}
return 0;

View File

@ -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('<comment>The use of "twig:lint" command is deprecated since version 2.7 and will be removed in 3.0. Use the "lint:twig" instead.</comment>');
$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('<error>The Twig environment needs to be set.</error>');
$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('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
if ($info['valid'] && $stdout->isVerbose()) {
$output->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
} elseif (!$info['valid']) {
++$errors;
$this->renderException($output, $info['template'], $info['exception'], $info['file']);
}
}
$output->writeln(sprintf('<comment>%d/%d valid files</comment>', 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('<error>KO</error> in %s (line %s)', $file, $line));
$output->text(sprintf('<error> ERROR </error> in %s (line %s)', $file, $line));
} else {
$output->writeln(sprintf('<error>KO</error> (line %s)', $line));
$output->text(sprintf('<error> ERROR </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 ? '<error>>></error>' : ' ',
$no,
$lineNumber === $line ? '<error> >> </error>' : ' ',
$lineNumber,
$code
));
if ($no == $line) {
$output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
if ($lineNumber === $line) {
$output->text(sprintf('<error> >> %s</error> ', $exception->getRawMessage()));
}
}
}

View File

@ -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()));
}
/**

View File

@ -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('<comment>The use of "twig:debug" command is deprecated since version 2.7 and will be removed in 3.0. Use the "debug:twig" instead.</comment>');
$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);