8000 minor #15989 Updated the styles of the Twig commands (javiereguiluz) · symfony/symfony@1309cfb · GitHub
[go: up one dir, main page]

Skip to content

Commit 1309cfb

Browse files
committed
minor #15989 Updated the styles of the Twig commands (javiereguiluz)
This PR was squashed before being merged into the 2.8 branch (closes #15989). Discussion ---------- Updated the styles of the Twig commands | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - ![twig_1](https://cloud.githubusercontent.com/assets/73419/10158152/5d1668d2-6690-11e5-9399-8a9a89b4e919.png) ![twig_2](https://cloud.githubusercontent.com/assets/73419/10158156/60dbd470-6690-11e5-9a4e-1e702c961fa6.png) ![twig_3](https://cloud.githubusercontent.com/assets/73419/10158160/66030dba-6690-11e5-890f-09dd9579aa9a.png) ![twig_4](https://cloud.githubusercontent.com/assets/73419/10158172/71ed2ffc-6690-11e5-8c5c-8190c5e6c235.png) Commits ------- fdeccd5 Updated the styles of the Twig commands
2 parents 56fa973 + fdeccd5 commit 1309cfb

File tree

4 files changed

+40
-31
lines changed

4 files changed

+40
-31
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputOption;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920

2021
/**
2122
* Lists twig functions, filters, globals and tests present in the current project.
@@ -82,10 +83,11 @@ protected function configure()
8283

8384
protected function execute(InputInterface $input, OutputInterface $output)
8485
{
86+
$output = new SymfonyStyle($input, $output);
8587
$twig = $this->getTwigEnvironment();
8688

8789
if (null === $twig) {
88-
$output->writeln('<error>The Twig environment needs to be set.</error>');
90+
$output->error('The Twig environment needs to be set.');
8991

9092
return 1;
9193
}
@@ -118,14 +120,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
118120
if (!$items) {
119121
continue;
120122
}
121-
if ($index > 0) {
122-
$output->writeln('');
123-
}
124-
$output->writeln('<info>'.ucfirst($type).'</info>');
123+
124+
$output->section(ucfirst($type));
125+
125126
ksort($items);
126-
foreach ($items as $item) {
127-
$output->writeln(' '.$item);
128-
}
127+
$output->listing($items);
129128
}
130129

131130
return 0;

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Symfony\Component\Console\Style\SymfonyStyle;
1920
use Symfony\Component\Finder\Finder;
2021

2122
/**
@@ -84,14 +85,17 @@ protected function configure()
8485

8586
protected function execute(InputInterface $input, OutputInterface $output)
8687
{
88+
$stdout = $output;
89+
$output = new SymfonyStyle($input, $output);
90+
8791
if (false !== strpos($input->getFirstArgument(), ':l')) {
88-
$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>');
92+
$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.');
8993
}
9094

9195
$twig = $this->getTwigEnvironment();
9296

9397
if (null === $twig) {
94-
$output->writeln('<error>The Twig environment needs to be set.</error>');
98+
$output->error('The Twig environment needs to be set.');
9599

96100
return 1;
97101
}
@@ -108,12 +112,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
108112
$template .= fread(STDIN, 1024);
109113
}
110114

111-
return $this->display($input, $output, array($this->validate($twig, $template, uniqid('sf_'))));
115+
return $this->display($input, $stdout, $output, array($this->validate($twig, $template, uniqid('sf_'))));
112116
}
113117

114118
$filesInfo = $this->getFilesInfo($twig, $filenames);
115119

116-
return $this->display($input, $output, $filesInfo);
120+
return $this->display($input, $stdout, $output, $filesInfo);
117121
}
118122

119123
private function getFilesInfo(\Twig_Environment $twig, array $filenames)
@@ -157,32 +161,36 @@ private function validate(\Twig_Environment $twig, $template, $file)
157161
return array('template' => $template, 'file' => $file, 'valid' => true);
158162
}
159163

160-
private function display(InputInterface $input, OutputInterface $output, $files)
164+
private function display(InputInterface $input, OutputInterface $stdout, $output, $files)
161165
{
162166
switch ($input->getOption('format')) {
163167
case 'txt':
164-
return $this->displayTxt($output, $files);
168+
return $this->displayTxt($stdout, $output, $files);
165169
case 'json':
166-
return $this->displayJson($output, $files);
170+
return $this->d F438 isplayJson($stdout, $files);
167171
default:
168172
throw new \InvalidArgumentException(sprintf('The format "%s" is not supported.', $input->getOption('format')));
169173
}
170174
}
171175

172-
private function displayTxt(OutputInterface $output, $filesInfo)
176+
private function displayTxt(OutputInterface $stdout, $output, $filesInfo)
173177
{
174178
$errors = 0;
175179

176180
foreach ($filesInfo as $info) {
177-
if ($info['valid'] && $output->isVerbose()) {
178-
$output->writeln('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
181+
if ($info['valid'] && $stdout->isVerbose()) {
182+
$output->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
179183
} elseif (!$info['valid']) {
180184
++$errors;
181185
$this->renderException($output, $info['template'], $info['exception'], $info['file']);
182186
}
183187
}
184188

185-
$output->writeln(sprintf('<comment>%d/%d valid files</comment>', count($filesInfo) - $errors, count($filesInfo)));
189+
if ($errors === 0) {
190+
$output->success(sprintf('All %d Twig files contain valid syntax.', count($filesInfo)));
191+
} else {
192+
$output->warning(sprintf('%d Twig files have valid syntax and %d contain errors.', count($filesInfo) - $errors, $errors));
193+
}
186194

187195
return min($errors, 1);
188196
}
@@ -211,20 +219,20 @@ private function renderException(OutputInterface $output, $template, \Twig_Error
211219
$line = $exception->getTemplateLine();
212220

213221
if ($file) {
214-
$output->writeln(sprintf('<error>KO</error> in %s (line %s)', $file, $line));
222+
$output->text(sprintf('<error> ERROR </error> in %s (line %s)', $file, $line));
215223
} else {
216-
$output->writeln(sprintf('<error>KO</error> (line %s)', $line));
224+
$output->text(sprintf('<error> ERROR </error> (line %s)', $line));
217225
}
218226

219-
foreach ($this->getContext($template, $line) as $no => $code) {
220-
$output->writeln(sprintf(
227+
foreach ($this->getContext($template, $line) as $lineNumber => $code) {
228+
$output->text(sprintf(
221229
'%s %-6s %s',
222-
$no == $line ? '<error>>></error>' : ' ',
223-
$no,
230+
$lineNumber === $line ? '<error> >> </error>' : ' ',
231+
$lineNumber,
224232
$code
225233
));
226-
if ($no == $line) {
227-
$output->writeln(sprintf('<error>>> %s</error> ', $exception->getRawMessage()));
234+
if ($lineNumber === $line) {
235+
$output->text(sprintf('<error> >> %s</error> ', $exception->getRawMessage()));
228236
}
229237
}
230238
}

src/Symfony/Bridge/Twig/Tests/Command/LintCommandTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testLintCorrectFile()
3131
$ret = $tester->execute(array('filename' => array($filename)), array('verbosity' => OutputInterface::VERBOSITY_VERBOSE, 'decorated' => false));
3232

3333
$this->assertEquals(0, $ret, 'Returns 0 in case of success');
34-
$this->assertRegExp('/^OK in /', $tester->getDisplay());
34+
$this->assertRegExp('/^\/\/ OK in /', trim($tester->getDisplay()));
3535
}
3636

3737
public function testLintIncorrectFile()
@@ -42,7 +42,7 @@ public function testLintIncorrectFile()
4242
$ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false));
4343

4444
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
45-
$this->assertRegExp('/^KO in /', $tester->getDisplay());
45+
$this->assertRegExp('/^ERROR in /', trim($tester->getDisplay()));
4646
}
4747

4848
/**
@@ -65,7 +65,7 @@ public function testLintFileCompileTimeException()
6565
$ret = $tester->execute(array('filename' => array($filename)), array('decorated' => false));
6666

6767
$this->assertEquals(1, $ret, 'Returns 1 in case of error');
68-
$this->assertRegExp('/^KO in /', $tester->getDisplay());
68+
$this->assertRegExp('/^ERROR in /', trim($tester->getDisplay()));
6969
}
7070

7171
/**

src/Symfony/Bundle/TwigBundle/Command/DebugCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\Command\DebugCommand as BaseDebugCommand;
1515
use Symfony\Component\Console\Input\InputInterface;
1616
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\Console\Style\SymfonyStyle;
1718
use Symfony\Component\DependencyInjection\ContainerInterface;
1819
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1920

@@ -57,8 +58,9 @@ protected function configure()
5758

5859
protected function execute(InputInterface $input, OutputInterface $output)
5960
{
61+
$output = new SymfonyStyle($input, $output);
6062
if (false !== strpos($input->getFirstArgument(), ':d')) {
61-
$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>');
63+
$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.');
6264
}
6365

6466
parent::execute($input, $output);

0 commit comments

Comments
 (0)
0