Permalink
Browse files

Updated the styles of the Twig commands

  • Loading branch information...
javiereguiluz authored and fabpot committed Sep 29, 2015
1 parent bee1faa commit fdeccd58491c04615a291914f6e1195c6a4c8465
@@ -16,6 +16,7 @@
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 @@ protected function configure()
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 @@ protected function execute(InputInterface $input, OutputInterface $output)
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;
@@ -16,6 +16,7 @@
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 @@ protected function configure()
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 @@ protected function execute(InputInterface $input, OutputInterface $output)
$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 @@ private function validate(\Twig_Environment $twig, $template, $file)
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 @@ private function renderException(OutputInterface $output, $template, \Twig_Error
$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()));
}
}
}
@@ -31,7 +31,7 @@ public function testLintCorrectFile()
$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 @@ public function testLintIncorrectFile()
$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 @@ public function testLintFileCompileTimeException()
$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()));
}
/**
@@ -14,6 +14,7 @@
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 @@ protected function configure()
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);

0 comments on commit fdeccd5

Please sign in to comment.