Skip to content

Commit 31213bc

Browse files
innocenzibrendt
andauthored
refactor(console): improve visual consistency (#888)
Co-authored-by: Brent Roose <[email protected]>
1 parent bc49c49 commit 31213bc

File tree

106 files changed

+1313
-614
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+1313
-614
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@
8181
"tempest/vite": "self.version"
8282
},
8383
"suggest": {
84-
"ext-pcntl": "Required to use some interactive console components."
84+
"ext-pcntl": "Required to use some interactive console components.",
85+
"ext-posix": "Required to use some interactive console components."
8586
},
8687
"minimum-stability": "dev",
8788
"prefer-stable": true,

src/Tempest/Cache/src/CacheClearCommand.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,23 @@ public function __invoke(bool $all = false): void
3131
);
3232
}
3333

34+
if (count($caches) === 0) {
35+
$this->console->info('No cache selected.');
36+
37+
return;
38+
}
39+
40+
$this->console->header('Clearing caches');
41+
3442
foreach ($caches as $cacheClass) {
3543
/** @var Cache $cache */
3644
$cache = $this->container->get($cacheClass);
37-
3845
$cache->clear();
3946

40-
$this->info("<em>{$cacheClass}</em> cleared successfully.");
47+
$this->console->keyValue(
48+
key: $cacheClass,
49+
value: "<style='bold fg-green'>CLEARED</style>",
50+
);
4151
}
4252
}
4353
}

src/Tempest/Cache/src/CacheStatusCommand.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,25 @@ public function __construct(
2323
#[ConsoleCommand(name: 'cache:status', description: 'Shows which caches are enabled')]
2424
public function __invoke(): void
2525
{
26-
$caches = $this->cacheConfig->caches;
27-
28-
foreach ($caches as $cacheClass) {
26+
$this->console->header('Cache status');
27+
$this->console->keyValue('Total caches', (string) count($this->cacheConfig->caches));
28+
$this->console->keyValue('Global cache', match ($this->cacheConfig->enable) {
29+
true => '<style="bold fg-green">ENABLED</style>',
30+
false => '<style="bold fg-red">FORCEFULLY DISABLED</style>',
31+
default => '<style="bold fg-gray">DISABLED</style>',
32+
});
33+
34+
foreach ($this->cacheConfig->caches as $cacheClass) {
2935
/** @var Cache $cache */
3036
$cache = $this->container->get($cacheClass);
3137

32-
$reason = match ($this->cacheConfig->enable) {
33-
true => ' (global CACHE = true)',
34-
false => ' (global CACHE = false)',
35-
null => '',
36-
};
37-
38-
$this->writeln(sprintf(
39-
'<em>%s</em> %s%s',
40-
$cacheClass,
41-
$cache->isEnabled() ? '<style="fg-green">enabled</style>' : '<style="fg-red">disabled</style>',
42-
$reason,
43-
));
38+
$this->console->keyValue(
39+
key: $cacheClass,
40+
value: match ($cache->isEnabled()) {
41+
true => '<style="bold fg-green">ENABLED</style>',
42+
false => '<style="bold fg-red">DISABLED</style>',
43+
},
44+
);
4445
}
4546
}
4647
}

src/Tempest/CommandBus/src/AsyncCommandRepositories/FileCommandRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public function markAsDone(string $uuid): void
3939

4040
public function markAsFailed(string $uuid): void
4141
{
42+
if (! is_file(__DIR__ . "/../stored-commands/{$uuid}.pending.txt")) {
43+
return;
44+
}
45+
4246
rename(
4347
from: __DIR__ . "/../stored-commands/{$uuid}.pending.txt",
4448
to: __DIR__ . "/../stored-commands/{$uuid}.failed.txt",
@@ -50,7 +54,6 @@ public function getPendingCommands(): array
5054
return arr(glob(__DIR__ . '/../stored-commands/*.pending.txt'))
5155
->mapWithKeys(function (string $path) {
5256
$uuid = str_replace('.pending.txt', '', pathinfo($path, PATHINFO_BASENAME));
53-
5457
$payload = file_get_contents($path);
5558

5659
yield $uuid => unserialize($payload);

src/Tempest/CommandBus/src/CommandHandlerNotFound.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public function __construct(object $command)
1212
{
1313
$commandName = $command::class;
1414

15-
parent::__construct("No handler found for {$commandName}");
15+
parent::__construct("No handler found for [{$commandName}].");
1616
}
1717
}

src/Tempest/CommandBus/src/Exceptions/CouldNotResolveCommand.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,8 @@
88

99
final class CouldNotResolveCommand extends Exception
1010
{
11+
public function __construct(public string $uuid)
12+
{
13+
parent::__construct("Could not resolve command [{$uuid}].");
14+
}
1115
}

src/Tempest/CommandBus/src/HandleAsyncCommand.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(
2424
) {
2525
}
2626

27-
#[ConsoleCommand(name: 'command:handle')]
27+
#[ConsoleCommand(name: 'command:handle', description: 'Manually executes a pending command')]
2828
public function __invoke(?string $uuid = null): ExitCode
2929
{
3030
try {
@@ -35,7 +35,7 @@ public function __invoke(?string $uuid = null): ExitCode
3535
}
3636

3737
if (! $command) {
38-
$this->error('No pending command found');
38+
$this->error('No pending command found.');
3939

4040
return ExitCode::ERROR;
4141
}
@@ -44,7 +44,7 @@ public function __invoke(?string $uuid = null): ExitCode
4444

4545
if (! $commandHandler) {
4646
$commandClass = $command::class;
47-
$this->error("No handler found for command {$commandClass}");
47+
$this->error("No handler found for command {$commandClass}.");
4848

4949
return ExitCode::ERROR;
5050
}
@@ -55,7 +55,6 @@ public function __invoke(?string $uuid = null): ExitCode
5555
);
5656

5757
$this->repository->markAsDone($uuid);
58-
$this->success('Done');
5958

6059
return ExitCode::SUCCESS;
6160
} catch (Throwable $throwable) {

src/Tempest/CommandBus/src/MonitorAsyncCommands.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,28 @@ public function __construct(
2323
) {
2424
}
2525

26-
#[ConsoleCommand(name: 'command:monitor')]
26+
#[ConsoleCommand(name: 'command:monitor', description: 'Monitors and executes pending async commands')]
2727
public function __invoke(): void
2828
{
29-
$this->success('Monitoring for new commands. Press ctrl+c to stop.');
29+
$this->info('Monitoring for new commands. Press <em>Ctrl+C</em> to stop.');
30+
$this->writeln();
3031

3132
/** @var \Symfony\Component\Process\Process[] $processes */
3233
$processes = [];
3334

3435
while (true) { // @phpstan-ignore-line
3536
foreach ($processes as $uuid => $process) {
36-
$time = new DateTimeImmutable();
37-
3837
if ($process->isTerminated()) {
3938
if ($process->isSuccessful()) {
40-
$this->writeln("<style=\"fg-green\">{$uuid}</style> finished at {$time->format('Y-m-d H:i:s')}");
39+
$this->console->keyValue(
40+
key: "<style='fg-gray'>{$uuid}</style>",
41+
value: "<style='fg-green bold'>SUCCESS</style>",
42+
);
4143
} else {
42-
$this->writeln("<style=\"fg-red\">{$uuid}</style> failed at {$time->format('Y-m-d H:i:s')}");
44+
$this->console->keyValue(
45+
key: "<style='fg-gray'>{$uuid}</style>",
46+
value: "<style='fg-red bold'>FAILED</style>",
47+
);
4348
}
4449

4550
if ($output = trim($process->getOutput())) {
@@ -73,7 +78,10 @@ public function __invoke(): void
7378
$uuid = $availableCommands->keys()->first();
7479

7580
$time = new DateTimeImmutable();
76-
$this->writeln("<h2>{$uuid}</h2> started at {$time->format('Y-m-d H:i:s')}");
81+
$this->console->keyValue(
82+
key: $uuid,
83+
value: "<style='fg-gray'>{$time->format('Y-m-d H:i:s')}</style>",
84+
);
7785

7886
$process = new Process([
7987
$this->argumentBag->getBinaryPath(),

src/Tempest/Console/composer.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
"tempest/validation": "dev-main",
1818
"ext-readline": "*"
1919
},
20+
"suggest": {
21+
"ext-pcntl": "Required to use some interactive console components.",
22+
"ext-posix": "Required to use some interactive console components."
23+
},
2024
"autoload": {
2125
"psr-4": {
2226
"Tempest\\Console\\": "src"

src/Tempest/Console/src/Actions/RenderConsoleCommand.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __invoke(ConsoleCommand $consoleCommand): void
3232

3333
if ($this->renderDescription) {
3434
if ($consoleCommand->description !== null && $consoleCommand->description !== '') {
35-
$parts[] = $consoleCommand->description;
35+
$parts[] = "<style='dim'>{$consoleCommand->description}</style>";
3636
}
3737
}
3838

@@ -43,7 +43,6 @@ private function renderName(ConsoleCommand $consoleCommand): string
4343
{
4444
return str($consoleCommand->getName())
4545
->alignRight($this->longestCommandName, padding: $this->longestCommandName ? 2 : 0)
46-
->wrap(before: '<style="fg-cyan">', after: '</style>')
4746
->toString();
4847
}
4948

@@ -58,7 +57,7 @@ private function renderArgument(ConsoleArgumentDefinition $argument): string
5857
default => $argument->name,
5958
};
6059

61-
$formattedArgumentName = str($formattedArgumentName)->wrap('<style="fg-cyan">', '</style>');
60+
$formattedArgumentName = str($formattedArgumentName)->wrap('<style="fg-blue">', '</style>');
6261

6362
if (! $argument->hasDefault) {
6463
return $formattedArgumentName->wrap('<style="fg-gray dim"><</style>', '<style="fg-gray dim">></style>')->toString();
@@ -88,8 +87,8 @@ private function renderEnumArgument(ConsoleArgumentDefinition $argument): string
8887
array: $argument->type::cases(),
8988
);
9089

91-
$partsAsString = ' {<style="fg-cyan">' . implode('|', $parts) . '</style>}';
92-
$line = "<style=\"fg-cyan\">{$argument->name}</style>";
90+
$partsAsString = ' {<style="fg-blue">' . implode('|', $parts) . '</style>}';
91+
$line = "<style=\"fg-blue\">{$argument->name}</style>";
9392

9493
if ($argument->hasDefault) {
9594
return "[{$line}={$argument->default->value}{$partsAsString}]";

0 commit comments

Comments
 (0)