Skip to content

Commit 6967c3c

Browse files
add global verbosity, stderr support and update documentation
1 parent 26b4f1b commit 6967c3c

14 files changed

+860
-45
lines changed

README.md

Lines changed: 785 additions & 17 deletions
Large diffs are not rendered by default.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"ext-ctype": "*"
1616
},
1717
"require-dev": {
18-
"phpdevcommunity/unitester": "^0.1.0@alpha"
18+
"depo/unitester": "^1.0.0"
1919
},
2020
"license": "MIT",
2121
"authors": [
@@ -24,4 +24,4 @@
2424
"homepage": "https://www.phpdevcommunity.com"
2525
}
2626
]
27-
}
27+
}

src/CommandRunner.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ public function __construct(array $commands)
4747

4848
public function run(CommandParser $commandParser, OutputInterface $output): int
4949
{
50+
if ($commandParser->hasOption('verbose') || $commandParser->hasOption('v')) {
51+
$output->setVerbose(true);
52+
}
53+
5054
try {
5155

5256
if ($commandParser->getCommandName() === null || $commandParser->getCommandName() === '--help') {
@@ -162,7 +166,7 @@ private function execute(CommandInterface $command, CommandParser $commandParser
162166
$peakMemoryBytes = memory_get_peak_usage(true);
163167
$peakMemoryMB = round($peakMemoryBytes / 1024 / 1024, 2);
164168
$duration = round($endTime - $startTime, 2);
165-
if ($input->getOptionValue('verbose')) {
169+
if ($output->isVerbose()) {
166170
$output->writeln(sprintf(
167171
'Execution time: %.2fs; Peak memory usage: %.2f MB',
168172
$duration,

src/Option/CommandOption.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ public function isFlag(): bool
6060
public static function flag(string $name, ?string $shortcut = null, ?string $description = null): self {
6161
return new self($name, $shortcut, $description, true);
6262
}
63+
6364
public static function withValue(string $name, ?string $shortcut = null, ?string $description = null): self {
6465
return new self($name, $shortcut, $description, false);
6566
}

src/Output.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public function __construct(callable $output = null)
2222
$this->output = $output;
2323
}
2424

25+
/**
26+
* @var bool
27+
*/
28+
private bool $verbose = false;
29+
2530
public function write(string $message): void
2631
{
2732
$output = $this->output;
@@ -33,4 +38,14 @@ public function writeln(string $message): void
3338
$this->write($message);
3439
$this->write(PHP_EOL);
3540
}
41+
42+
public function setVerbose(bool $verbose): void
43+
{
44+
$this->verbose = $verbose;
45+
}
46+
47+
public function isVerbose(): bool
48+
{
49+
return $this->verbose;
50+
}
3651
}

src/Output/ConsoleOutput.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function success(string $message): void
6161
public function error(string $message): void
6262
{
6363
[$formattedMessage, $lineLength, $color] = $this->formatMessage('ERROR', $message, 'red');
64-
$this->outputMessage($formattedMessage, $lineLength, $color);
64+
$this->outputMessage($formattedMessage, $lineLength, $color, true);
6565
}
6666

6767
public function warning(string $message): void
@@ -76,6 +76,16 @@ public function info(string $message): void
7676
$this->outputMessage($formattedMessage, $lineLength, $color);
7777
}
7878

79+
public function debug(string $message): void
80+
{
81+
if (!$this->output->isVerbose()) {
82+
return;
83+
}
84+
[$formattedMessage, $lineLength, $color] = $this->formatMessage('DEBUG', $message, 'cyan');
85+
$this->outputMessage($formattedMessage, $lineLength, $color);
86+
}
87+
88+
7989
public function title(string $message): void
8090
{
8191
$consoleWidth = $this->geTerminalWidth();
@@ -92,6 +102,7 @@ public function title(string $message): void
92102
public function list(array $items): void
93103
{
94104
foreach ($items as $item) {
105+
$item = $this->variableToString($item);
95106
$this->write('- ' . $item);
96107
$this->write(PHP_EOL);
97108
}
@@ -298,7 +309,7 @@ public function boxed(string $message, string $borderChar = '*', int $padding =
298309
$this->write(PHP_EOL);
299310
}
300311

301-
public function writeColor(string $message, ?string $color = null, ?string $background = null): void
312+
public function writeColor(string $message, ?string $color = null, ?string $background = null, bool $isError = false): void
302313
{
303314

304315
$formattedMessage = '';
@@ -312,11 +323,15 @@ public function writeColor(string $message, ?string $color = null, ?string $back
312323

313324
$formattedMessage .= $message . "\033[0m";
314325

315-
$this->write($formattedMessage);
326+
$this->write($formattedMessage, $isError);
316327
}
317328

318-
public function write(string $message): void
329+
public function write(string $message, bool $isError = false): void
319330
{
331+
if ($isError) {
332+
fwrite(STDERR, $message);
333+
return;
334+
}
320335
$this->output->write($message);
321336
}
322337

@@ -325,24 +340,24 @@ public function writeln(string $message): void
325340
$this->output->writeln($message);
326341
}
327342

328-
private function outputMessage($formattedMessage, int $lineLength, string $color): void
343+
private function outputMessage($formattedMessage, int $lineLength, string $color, bool $isError = false): void
329344
{
330-
$this->write(PHP_EOL);
331-
$this->writeColor(str_repeat(' ', $lineLength), 'white', $color);
332-
$this->write(PHP_EOL);
345+
$this->write(PHP_EOL, $isError);
346+
$this->writeColor(str_repeat(' ', $lineLength), 'white', $color, $isError);
347+
$this->write(PHP_EOL, $isError);
333348

334349
if (is_string($formattedMessage)) {
335350
$formattedMessage = [$formattedMessage];
336351
}
337352

338353
foreach ($formattedMessage as $line) {
339-
$this->writeColor($line, 'white', $color);
354+
$this->writeColor($line, 'white', $color, $isError);
340355
}
341356

342-
$this->write(PHP_EOL);
343-
$this->writeColor(str_repeat(' ', $lineLength), 'white', $color);
344-
$this->write(PHP_EOL);
345-
$this->write(PHP_EOL);
357+
$this->write(PHP_EOL, $isError);
358+
$this->writeColor(str_repeat(' ', $lineLength), 'white', $color, $isError);
359+
$this->write(PHP_EOL, $isError);
360+
$this->write(PHP_EOL, $isError);
346361
}
347362

348363
private function formatMessage(string $prefix, string $message, string $color): array
@@ -390,4 +405,14 @@ private function variableToString($variable): string
390405

391406
return var_export($variable, true);
392407
}
408+
409+
public function setVerbose(bool $verbose): void
410+
{
411+
$this->output->setVerbose($verbose);
412+
}
413+
414+
public function isVerbose(): bool
415+
{
416+
return $this->output->isVerbose();
417+
}
393418
}

src/OutputInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ interface OutputInterface
66
{
77
public function write(string $message): void;
88
public function writeln(string $message): void;
9+
public function setVerbose(bool $verbose): void;
10+
public function isVerbose(): bool;
911
}

tests/CommandArgumentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PhpDevCommunity\Console\Argument\CommandArgument;
66
use PhpDevCommunity\Console\Output;
7-
use PhpDevCommunity\UniTester\TestCase;
7+
use Depo\UniTester\TestCase;
88

99
class CommandArgumentTest extends TestCase
1010
{
@@ -90,4 +90,4 @@ public function testGetDescriptionReturnsCorrectValue()
9090
$this->assertEquals('description', $arg->getDescription());
9191
}
9292

93-
}
93+
}

tests/CommandOptionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use PhpDevCommunity\Console\Option\CommandOption;
66
use PhpDevCommunity\Console\Output;
7-
use PhpDevCommunity\UniTester\TestCase;
7+
use Depo\UniTester\TestCase;
88

99
class CommandOptionTest extends TestCase
1010
{
@@ -62,4 +62,4 @@ public function testIsFlag(): void
6262
$this->assertTrue($option->isFlag());
6363
}
6464

65-
}
65+
}

tests/CommandParserTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Test\PhpDevCommunity\Console;
44

55
use PhpDevCommunity\Console\CommandParser;
6-
use PhpDevCommunity\UniTester\TestCase;
6+
use Depo\UniTester\TestCase;
77

88
class CommandParserTest extends TestCase
99
{
@@ -78,4 +78,4 @@ private static function createArgv(array $argv): array
7878
{
7979
return array_merge(['bin/console'], $argv);
8080
}
81-
}
81+
}

0 commit comments

Comments
 (0)