Skip to content

Commit 0e9a9c6

Browse files
authored
Merge pull request #887 from hydephp/dynamic-info-comment-helper
Update InfoComment command helper to dynamically format output from a single fluent string
2 parents 5ddafc9 + 103cd33 commit 0e9a9c6

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

packages/framework/src/Console/Commands/ChangeSourceDirectoryCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected function getValidatedName(string $name): string
7676
{
7777
$this->validateName($name);
7878
$this->validateDirectoryCanBeUsed($name);
79-
$this->infoComment('Setting', $name, 'as the project source directory!');
79+
$this->infoComment("Setting [$name] as the project source directory!");
8080

8181
return $name;
8282
}

packages/framework/src/Console/Concerns/Command.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,19 @@ public static function createClickableFilepath(string $filepath): string
2222

2323
/**
2424
* Write a nicely formatted and consistent message to the console. Using InfoComment for a lack of a better term.
25+
*
26+
* Text in [brackets] will automatically be wrapped in <comment> tags.
2527
*/
26-
public function infoComment(string $info, string $comment, ?string $moreInfo = null): void
28+
public function infoComment(string $string): void
2729
{
28-
$this->line("<info>$info</info> [<comment>$comment</comment>]".($moreInfo ? " <info>$moreInfo</info>" : ''));
30+
$replacements = [
31+
'[' => '</info>[<comment>',
32+
']' => '</comment>]<info>',
33+
];
34+
35+
$string = str_replace(array_keys($replacements), array_values($replacements), $string);
36+
37+
$this->line("<info>$string</info>");
2938
}
3039

3140
/** @experimental This method may change (or be removed) before the 1.0.0 release */

packages/framework/tests/Feature/CommandTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ public function testInfoComment()
3737
{
3838
$command = new MockableTestCommand();
3939
$command->closure = function (Command $command) {
40-
$command->infoComment('foo', 'bar');
40+
$command->infoComment('foo [bar]');
4141
};
4242

4343
$output = $this->mock(OutputStyle::class);
4444
$output->shouldReceive('writeln')->once()->withArgs(function (string $message): bool {
45-
return $message === '<info>foo</info> [<comment>bar</comment>]';
45+
return $message === '<info>foo </info>[<comment>bar</comment>]<info></info>';
4646
});
4747

4848
$command->setMockedOutput($output);
@@ -53,12 +53,28 @@ public function testInfoCommentWithExtraInfo()
5353
{
5454
$command = new MockableTestCommand();
5555
$command->closure = function (Command $command) {
56-
$command->infoComment('foo', 'bar', 'baz');
56+
$command->infoComment('foo [bar] baz');
5757
};
5858

5959
$output = $this->mock(OutputStyle::class);
6060
$output->shouldReceive('writeln')->once()->withArgs(function (string $message): bool {
61-
return $message === '<info>foo</info> [<comment>bar</comment>] <info>baz</info>';
61+
return $message === '<info>foo </info>[<comment>bar</comment>]<info> baz</info>';
62+
});
63+
64+
$command->setMockedOutput($output);
65+
$command->handle();
66+
}
67+
68+
public function testInfoCommentWithExtraInfoAndComments()
69+
{
70+
$command = new MockableTestCommand();
71+
$command->closure = function (Command $command) {
72+
$command->infoComment('foo [bar] baz [qux]');
73+
};
74+
75+
$output = $this->mock(OutputStyle::class);
76+
$output->shouldReceive('writeln')->once()->withArgs(function (string $message): bool {
77+
return $message === '<info>foo </info>[<comment>bar</comment>]<info> baz </info>[<comment>qux</comment>]<info></info>';
6278
});
6379

6480
$command->setMockedOutput($output);

0 commit comments

Comments
 (0)