-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Updated the styles of the Twig commands #15989
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing typehint |
||
{ | ||
$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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as #15991 (comment) |
||
$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())); | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this class is missing an |
||
{ | ||
$output = new SymfonyStyle($input, $output); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not replace the |
||
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); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing typehint for style
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rather keep
$output
for the object received as$output
in arguments and used a different name for the styling wrapper. It would be more readable IMO