Skip to content

Commit 85e94dc

Browse files
authored
Merge pull request #35 from driftphp/feature/improved-console-messages
Added improved messages
2 parents fe03f80 + 7e67378 commit 85e94dc

13 files changed

+110
-90
lines changed

.circleci/config.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: 2
22
jobs:
3-
test-php74:
3+
test-php80:
44
docker:
5-
- image: circleci/php:7.4-cli
5+
- image: circleci/php:8.0-cli
66
- image: redis
77
- image: rabbitmq
88
- image: postgres:alpine
@@ -16,14 +16,14 @@ jobs:
1616
- checkout
1717

1818
- run:
19-
name: Run tests / Symfony 4^4
19+
name: Run tests / Symfony 5^0
2020
command: |
21-
composer update -n --prefer-dist --prefer-lowest --no-suggest
22-
sh .circleci/start.sh
21+
composer update -n --prefer-dist --no-suggest
22+
sh .circleci/start_base_testsuite.sh
2323
24-
test-php80:
24+
test-php81:
2525
docker:
26-
- image: circleci/php:8.0-cli
26+
- image: cimg/php:8.1
2727
- image: redis
2828
- image: rabbitmq
2929
- image: postgres:alpine
@@ -46,5 +46,5 @@ workflows:
4646
version: 2
4747
test:
4848
jobs:
49-
- test-php74
5049
- test-php80
50+
- test-php81

Async/AMQPAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ function () use (&$forced) {
210210
});
211211

212212
EventLoopUtils::runLoop($this->loop, 2, function ($iterationsMissing) use ($outputPrinter) {
213-
(new CommandBusHeaderMessage('', 'EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
213+
(new CommandBusHeaderMessage('EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
214214
}, $forced);
215215
}
216216
}

Async/PostgreSQLAdapter.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ function () use (&$forced) {
263263
});
264264

265265
EventLoopUtils::runLoop($this->loop, 2, function ($iterationsMissing) use ($outputPrinter) {
266-
(new CommandBusHeaderMessage('', 'EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
266+
(new CommandBusHeaderMessage('EventLoop stopped. This consumer will run it '.$iterationsMissing.' more times.'))->print($outputPrinter);
267267
}, $forced);
268268
}
269269

@@ -339,23 +339,3 @@ private function getAndDeleteKeyFromQueue(): PromiseInterface
339339
->toPromise();
340340
}
341341
}
342-
343-
/*
344-
*
345-
*
346-
DROP TABLE IF EXISTS commands;
347-
CREATE TABLE commands (id VARCHAR, added_at TIMESTAMP, payload TEXT);
348-
SELECT * FROM commands;
349-
350-
CREATE OR REPLACE FUNCTION notify_commands() RETURNS TRIGGER AS $$
351-
BEGIN
352-
PERFORM pg_notify('commands', '1');
353-
RETURN NEW;
354-
END;
355-
$$ LANGUAGE plpgsql;
356-
DROP TRIGGER IF EXISTS notify_trigger ON commands;
357-
CREATE TRIGGER notify_trigger
358-
AFTER INSERT
359-
ON commands
360-
FOR EACH ROW EXECUTE PROCEDURE notify_commands();
361-
*/

Console/CommandBusHeaderMessage.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,17 @@
2020
/**
2121
* Class ConsumerHeaderMessage.
2222
*/
23-
final class CommandBusHeaderMessage
23+
final class CommandBusHeaderMessage extends CommandMessage
2424
{
25-
private $elapsedTime;
26-
private $message;
25+
private string $message;
2726

2827
/**
2928
* ConsumerMessage constructor.
3029
*
31-
* @param string $elapsedTime
3230
* @param string $message
3331
*/
34-
public function __construct(
35-
string $elapsedTime,
36-
string $message
37-
) {
38-
$this->elapsedTime = $elapsedTime;
32+
public function __construct(string $message)
33+
{
3934
$this->message = $message;
4035
}
4136

@@ -46,11 +41,7 @@ public function __construct(
4641
*/
4742
public function print(OutputPrinter $outputPrinter)
4843
{
49-
$color = '32';
50-
51-
$outputPrinter->print("\033[01;{$color}mBUS\033[0m ");
52-
$outputPrinter->print("(\e[00;37m".$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000))." MB\e[0m)");
53-
$outputPrinter->print(" {$this->message}");
44+
$outputPrinter->print("\033[01;32mBUS\033[0m {$this->message}");
5445
$outputPrinter->printLine();
5546
}
5647
}

Console/CommandConsumedLineMessage.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,26 @@
2020
/**
2121
* Class ConsumerLineMessage.
2222
*/
23-
final class CommandConsumedLineMessage
23+
final class CommandConsumedLineMessage extends CommandMessage
2424
{
2525
/**
2626
* @var string
2727
*/
28-
const CONSUMED = 'Consumed';
28+
const CONSUMED = '[CON]';
2929

3030
/**
3131
* @var string
3232
*/
33-
const IGNORED = 'Ignored ';
33+
const IGNORED = '[IGN]';
3434

3535
/**
3636
* @var string
3737
*/
38-
const REJECTED = 'Rejected';
38+
const REJECTED = '[REJ]';
3939

40-
private $class;
41-
private $elapsedTime;
42-
private $status;
40+
private string $class;
41+
private string $elapsedTime;
42+
private string $status;
4343

4444
/**
4545
* ConsumerMessage constructor.
@@ -72,9 +72,8 @@ public function print(OutputPrinter $outputPrinter)
7272
$color = '31';
7373
}
7474

75-
$outputPrinter->print("\033[01;{$color}m{$this->status}\033[0m");
76-
$outputPrinter->print(" {$this->class} ");
77-
$outputPrinter->print("(\e[00;37m".$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000))." MB\e[0m)");
75+
$performance = $this->styledPerformance($this->elapsedTime);
76+
$outputPrinter->print("\033[01;32mBUS\033[0m $performance \033[01;{$color}m{$this->status}\033[0m {$this->class}");
7877
$outputPrinter->printLine();
7978
}
8079

Console/CommandConsumerCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Drift\EventBus\Bus\EventBus;
2323
use Drift\EventBus\Subscriber\EventBusSubscriber;
2424
use Symfony\Component\Console\Command\Command;
25+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2526
use Symfony\Component\Console\Input\InputInterface;
2627
use Symfony\Component\Console\Input\InputOption;
2728
use Symfony\Component\Console\Output\OutputInterface;
@@ -97,19 +98,21 @@ protected function configure()
9798
*/
9899
protected function execute(InputInterface $input, OutputInterface $output)
99100
{
101+
$outputFormatter = $output->getFormatter();
102+
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
100103
$outputPrinter = new OutputPrinter($output, false, false);
101104
$adapterName = $this->asyncAdapter->getName();
102-
(new CommandBusHeaderMessage('', 'Consumer built'))->print($outputPrinter);
103-
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
104-
(new CommandBusHeaderMessage('', 'Started listening...'))->print($outputPrinter);
105+
(new CommandBusHeaderMessage('Consumer built'))->print($outputPrinter);
106+
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);
107+
(new CommandBusHeaderMessage('Started listening...'))->print($outputPrinter);
105108

106109
$exchanges = self::buildQueueArray($input);
107110
if (
108111
class_exists(EventBusSubscriber::class) &&
109112
!empty($exchanges) &&
110113
!is_null($this->eventBusSubscriber)
111114
) {
112-
(new CommandBusHeaderMessage('', 'Kernel connected to exchanges.'))->print($outputPrinter);
115+
(new CommandBusHeaderMessage('Kernel connected to exchanges.'))->print($outputPrinter);
113116
$this
114117
->eventBusSubscriber
115118
->subscribeToExchanges(

Console/CommandMessage.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Drift\CommandBus\Console;
4+
5+
abstract class CommandMessage
6+
{
7+
/**
8+
* @param string $elapsedType
9+
*
10+
* @return string
11+
*/
12+
protected function styledPerformance(string $elapsedType = null): string
13+
{
14+
$info = array_filter([
15+
$elapsedType ? $this->toLength($elapsedType, str_contains($elapsedType, 'μ') ? 8 : 7) : null,
16+
$this->toLength((string) (int) (memory_get_usage() / 1048576), 4),
17+
$this->toLength((string) (int) (memory_get_usage(true) / 1048576), 4),
18+
]);
19+
20+
return '<performance>['.implode('|', $info).']</performance>';
21+
}
22+
23+
/**
24+
* @param string $string
25+
* @param int $length
26+
*
27+
* @return string
28+
*/
29+
protected function toLength(string $string, int $length): string
30+
{
31+
return str_pad($string, $length, ' ', STR_PAD_LEFT);
32+
}
33+
}

Console/DebugCommandBusCommand.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Drift\CommandBus\Middleware\DiscriminableMiddleware;
2323
use Drift\CommandBus\Middleware\HandlerMiddleware;
2424
use Symfony\Component\Console\Command\Command;
25+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2526
use Symfony\Component\Console\Input\InputInterface;
2627
use Symfony\Component\Console\Output\OutputInterface;
2728

@@ -85,6 +86,8 @@ protected function configure()
8586
*/
8687
protected function execute(InputInterface $input, OutputInterface $output)
8788
{
89+
$outputFormatter = $output->getFormatter();
90+
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
8891
$output->writeln('');
8992
$this->printBus('Query', $this->queryBus, $output);
9093
$this->printBus('Command', $this->commandBus, $output);

Console/InfrastructureCheckCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818
use Clue\React\Block;
1919
use Drift\CommandBus\Async\AsyncAdapter;
2020
use Drift\Console\OutputPrinter;
21+
use Drift\Server\Console\Style\Muted;
22+
use Drift\Server\Console\Style\Purple;
2123
use React\EventLoop\LoopInterface;
2224
use Symfony\Component\Console\Command\Command;
25+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2326
use Symfony\Component\Console\Input\InputInterface;
2427
use Symfony\Component\Console\Output\OutputInterface;
2528

@@ -74,17 +77,19 @@ protected function configure()
7477
*/
7578
protected function execute(InputInterface $input, OutputInterface $output)
7679
{
80+
$outputFormatter = $output->getFormatter();
81+
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
7782
$outputPrinter = new OutputPrinter($output, false, false);
7883
$adapterName = $this->asyncAdapter->getName();
79-
(new CommandBusHeaderMessage('', 'Started checking infrastructure...'))->print($outputPrinter);
80-
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
84+
(new CommandBusHeaderMessage('Started checking infrastructure...'))->print($outputPrinter);
85+
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);
8186

8287
$promise = $this
8388
->asyncAdapter
8489
->checkInfrastructure($outputPrinter);
8590

8691
Block\await($promise, $this->loop);
87-
(new CommandBusHeaderMessage('', 'Infrastructure checked'))->print($outputPrinter);
92+
(new CommandBusHeaderMessage('Infrastructure checked'))->print($outputPrinter);
8893

8994
return 0;
9095
}

Console/InfrastructureCreateCommand.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Drift\Console\OutputPrinter;
2121
use React\EventLoop\LoopInterface;
2222
use Symfony\Component\Console\Command\Command;
23+
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
2324
use Symfony\Component\Console\Input\InputInterface;
2425
use Symfony\Component\Console\Input\InputOption;
2526
use Symfony\Component\Console\Output\OutputInterface;
@@ -81,23 +82,25 @@ protected function configure()
8182
*/
8283
protected function execute(InputInterface $input, OutputInterface $output)
8384
{
85+
$outputFormatter = $output->getFormatter();
86+
$outputFormatter->setStyle('performance', new OutputFormatterStyle('gray'));
8487
$outputPrinter = new OutputPrinter($output, false, false);
8588
if (!$input->getOption('force')) {
86-
(new CommandBusHeaderMessage('', 'Please, use the flag --force'))->print($outputPrinter);
89+
(new CommandBusHeaderMessage('Please, use the flag --force'))->print($outputPrinter);
8790

8891
return 1;
8992
}
9093

9194
$adapterName = $this->asyncAdapter->getName();
92-
(new CommandBusHeaderMessage('', 'Started building infrastructure...'))->print($outputPrinter);
93-
(new CommandBusHeaderMessage('', 'Using adapter '.$adapterName))->print($outputPrinter);
95+
(new CommandBusHeaderMessage('Started building infrastructure...'))->print($outputPrinter);
96+
(new CommandBusHeaderMessage('Using adapter '.$adapterName))->print($outputPrinter);
9497

9598
$promise = $this
9699
->asyncAdapter
97100
->createInfrastructure($outputPrinter);
98101

99102
Block\await($promise, $this->loop);
100-
(new CommandBusHeaderMessage('', 'Infrastructure built'))->print($outputPrinter);
103+
(new CommandBusHeaderMessage('Infrastructure built'))->print($outputPrinter);
101104

102105
return 0;
103106
}

0 commit comments

Comments
 (0)