Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a5108f4

Browse files
committed
feature #19495 [master][console] Allow multiple options to be set. (SpacePossum)
This PR was merged into the 3.2-dev branch. Discussion ---------- [master][console] Allow multiple options to be set. | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | yes | Tests pass? | yes | License | MIT This PR add the possibility to set multiple options on a formatted string output. Example: ```php $output->writeln('<fg=green;options=bold,underscore>Test</>'); ``` Secondly it makes the behavior on invalid values consistent. ```php // current $output->writeln('<fg=lime;>Test</>'); // throws exception $output->writeln('<options=italic;>Test</>'); // silent ignore // new $output->writeln('<fg=lime;>Test</>'); // throws exception $output->writeln('<options=italic;>Test</>'); // throws exception ``` All other changes are about making the code more strict or other SCA/CS fixes. Commits ------- 1430138 Allow multiple options to be set.
2 parents 0e63f47 + 1430138 commit a5108f4

File tree

2 files changed

+82
-7
lines changed

2 files changed

+82
-7
lines changed

src/Symfony/Component/Console/Formatter/OutputFormatter.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function format($message)
139139
$message = (string) $message;
140140
$offset = 0;
141141
$output = '';
142-
$tagRegex = '[a-z][a-z0-9_=;-]*+';
142+
$tagRegex = '[a-z][a-z0-9,_=;-]*+';
143143
preg_match_all("#<(($tagRegex) | /($tagRegex)?)>#ix", $message, $matches, PREG_OFFSET_CAPTURE);
144144
foreach ($matches[0] as $i => $match) {
145145
$pos = $match[1];
@@ -202,7 +202,7 @@ private function createStyleFromString($string)
202202
return $this->styles[$string];
203203
}
204204

205-
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
205+
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, PREG_SET_ORDER)) {
206206
return false;
207207
}
208208

@@ -214,12 +214,20 @@ private function createStyleFromString($string)
214214
$style->setForeground($match[1]);
215215
} elseif ('bg' == $match[0]) {
216216
$style->setBackground($match[1]);
217-
} else {
218-
try {
219-
$style->setOption($match[1]);
220-
} catch (\InvalidArgumentException $e) {
221-
return false;
217+
} elseif ('options' === $match[0]) {
218+
preg_match_all('([^,;]+)', $match[1], $options);
219+
$options = array_shift($options);
220+
foreach ($options as $option) {
221+
try {
222+
$style->setOption($option);
223+
} catch (\InvalidArgumentException $e) {
224+
trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
225+
226+
return false;
227+
}
222228
}
229+
} else {
230+
return false;
223231
}
224232
}
225233

src/Symfony/Component/Console/Tests/Formatter/OutputFormatterTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Tests\Formatter;
1313

14+
use Symfony\Bridge\PhpUnit\ErrorAssert;
1415
use Symfony\Component\Console\Formatter\OutputFormatter;
1516
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
1617

@@ -152,6 +153,72 @@ public function testInlineStyle()
152153
$this->assertEquals("\033[34;41msome text\033[39;49m", $formatter->format('<fg=blue;bg=red>some text</fg=blue;bg=red>'));
153154
}
154155

156+
/**
157+
* @param string $tag
158+
* @param string|null $expected
159+
* @param string|null $input
160+
*
161+
* @dataProvider provideInlineStyleOptionsCases
162+
*/
163+
public function testInlineStyleOptions($tag, $expected = null, $input = null)
164+
{
165+
$styleString = substr($tag, 1, -1);
166+
$formatter = new OutputFormatter(true);
167+
$method = new \ReflectionMethod($formatter, 'createStyleFromString');
168+
$method->setAccessible(true);
169+
$result = $method->invoke($formatter, $styleString);
170+
if (null === $expected) {
171+
$this->assertFalse($result);
172+
$expected = $tag.$input.'</'.$styleString.'>';
173+
$this->assertSame($expected, $formatter->format($expected));
174+
} else {
175+
/* @var OutputFormatterStyle $result */
176+
$this->assertInstanceOf(OutputFormatterStyle::class, $result);
177+
$this->assertSame($expected, $formatter->format($tag.$input.'</>'));
178+
$this->assertSame($expected, $formatter->format($tag.$input.'</'.$styleString.'>'));
179+
}
180+
}
181+
182+
public function provideInlineStyleOptionsCases()
183+
{
184+
return array(
185+
array('<unknown=_unknown_>'),
186+
array('<unknown=_unknown_;a=1;b>'),
187+
array('<fg=green;>', "\033[32m[test]\033[39m", '[test]'),
188+
array('<fg=green;bg=blue;>', "\033[32;44ma\033[39;49m", 'a'),
189+
array('<fg=green;options=bold>', "\033[32;1mb\033[39;22m", 'b'),
190+
array('<fg=green;options=reverse;>', "\033[32;7m<a>\033[39;27m", '<a>'),
191+
array('<fg=green;options=bold,underscore>', "\033[32;1;4mz\033[39;22;24m", 'z'),
192+
array('<fg=green;options=bold,underscore,reverse;>', "\033[32;1;4;7md\033[39;22;24;27m", 'd'),
193+
);
194+
}
195+
196+
/**
197+
* @group legacy
198+
* @dataProvider provideInlineStyleTagsWithUnknownOptions
199+
* @requires function Symfony\Bridge\PhpUnit\ErrorAssert::assertDeprecationsAreTriggered
200+
*/
201+
public function testInlineStyleOptionsUnknownAreDeprecated($tag, $option)
202+
{
203+
ErrorAssert::assertDeprecationsAreTriggered(
204+
array(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "Invalid option specified: "%s". Expected one of (bold, underscore, blink, reverse, conceal)".', $option)),
205+
function () use ($tag) {
206+
$formatter = new OutputFormatter(true);
207+
$formatter->format($tag);
208+
}
209+
);
210+
}
211+
212+
public function provideInlineStyleTagsWithUnknownOptions()
213+
{
214+
return array(
215+
array('<options=abc;>', 'abc'),
216+
array('<options=abc,def;>', 'abc'),
217+
array('<fg=green;options=xyz;>', 'xyz'),
218+
array('<fg=green;options=efg,abc>', 'efg'),
219+
);
220+
}
221+
155222
public function testNonStyleTag()
156223
{
157224
$formatter = new OutputFormatter(true);

0 commit comments

Comments
 (0)