Skip to content

Commit

Permalink
Updates CLI output (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon authored Nov 29, 2022
1 parent 39571bc commit f763887
Show file tree
Hide file tree
Showing 15 changed files with 352 additions and 14 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"laravel": {
"providers": [
"Laravel\\Reverb\\ServiceProvider"
]
],
"aliases": {
"Output": "Laravel\\Reverb\\Output"
}
}
},
"config": {
Expand Down
4 changes: 3 additions & 1 deletion src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Laravel\Reverb\Application;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Output;

class Channel
{
Expand Down Expand Up @@ -78,7 +79,8 @@ public function broadcast(Application $app, array $payload, Connection $except =
try {
$connection->send(json_encode($payload));
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
Output::error('Broadcasting to '.$connection->id().' resulted in an error');
Output::info($e->getMessage());
}
});
}
Expand Down
44 changes: 44 additions & 0 deletions src/Console/Components/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Laravel\Reverb\Console\Components;

use Illuminate\Console\View\Components\Component;
use Symfony\Component\Console\Output\OutputInterface;

class Message extends Component
{
/**
* Renders the component using the given arguments.
*
* @param string $first
* @param string|null $second
* @param int $verbosity
* @return void
*/
public function render($message, $verbosity = OutputInterface::VERBOSITY_NORMAL)
{
$this->renderView('message', [
'message' => $message,
], $verbosity);
}

/**
* Compile the given view contents.
*
* @param string $view
* @param array $data
* @return void
*/
protected function compile($view, $data)
{
extract($data);

ob_start();

include __DIR__."/views/$view.php";

return tap(ob_get_contents(), function () {
ob_end_clean();
});
}
}
5 changes: 5 additions & 0 deletions src/Console/Components/views/message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div class="flex mx-1 mb-1">
<code>
<?php echo htmlspecialchars($message) ?>
</code>
</div>
39 changes: 39 additions & 0 deletions src/Contracts/Logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Laravel\Reverb\Contracts;

interface Logger
{
/**
* Log an infomational message.
*
* @param string $title
* @param string|null $message
* @return void
*/
public function info(string $title, ?string $message = null): void;

/**
* Log an error message.
*
* @param string $message
* @return void
*/
public function error(string $message): void;

/**
* Append a new line to the log.
*
* @param int $lines
* @return void
*/
public function line(?int $lines = 1): void;

/**
* Log a message sent to the server.
*
* @param string $message
* @return void
*/
public function message(string $message): void;
}
73 changes: 73 additions & 0 deletions src/Loggers/CliLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Laravel\Reverb\Loggers;

use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Laravel\Reverb\Console\Components\Message;
use Laravel\Reverb\Contracts\Logger;

class CliLogger implements Logger
{
protected $components;

public function __construct(protected OutputStyle $output)
{
$this->components = new Factory($output);
}

/**
* Log an infomational message.
*
* @param string $title
* @param string|null $message
* @return void
*/
public function info(string $title, ?string $message = null): void
{
$this->components->twoColumnDetail($title, $message);
}

/**
* Log an error message.
*
* @param string $message
* @return void
*/
public function error(string $string): void
{
$this->output->error($string);
}

/**
* Append a new line to the log.
*
* @param int $lines
* @return void
*/
public function line(?int $lines = 1): void
{
$this->output->newLine($lines);
}

/**
* Log a message sent to the server.
*
* @param string $message
* @return void
*/
public function message(string $message): void
{
$message = json_decode($message, true);

if (isset($message['data']['channel_data'])) {
$message['data']['channel_data'] = json_decode($message['data']['channel_data'], true);
}

$message = json_encode($message, JSON_PRETTY_PRINT);

with(new Message($this->output))->render(
$message
);
}
}
53 changes: 53 additions & 0 deletions src/Loggers/NullLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Laravel\Reverb\Loggers;

use Laravel\Reverb\Contracts\Logger;

class NullLogger implements Logger
{
/**
* Log an infomational message.
*
* @param string $title
* @param string|null $message
* @return void
*/
public function info(string $title, ?string $message = null): void
{
//
}

/**
* Log an error message.
*
* @param string $message
* @return void
*/
public function error(string $string): void
{
//
}

/**
* Append a new line to the log.
*
* @param int $lines
* @return void
*/
public function line(?int $lines = 1): void
{
//
}

/**
* Log a message sent to the server.
*
* @param string $message
* @return void
*/
public function message(string $message): void
{
//
}
}
67 changes: 67 additions & 0 deletions src/Loggers/StandardLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Laravel\Reverb\Loggers;

use Laravel\Reverb\Contracts\Logger;

class StandardLogger implements Logger
{
/**
* Log an infomational message.
*
* @param string $title
* @param string|null $message
* @return void
*/
public function info(string $title, ?string $message = null): void
{
$output = $title;

if ($message) {
$output .= ': '.$message;
}

fwrite(STDOUT, $output.PHP_EOL);
}

/**
* Log an error message.
*
* @param string $message
* @return void
*/
public function error(string $string): void
{
fwrite(STDERR, $string.PHP_EOL);
}

/**
* Append a new line to the log.
*
* @param int $lines
* @return void
*/
public function line(?int $lines = 1): void
{
//
}

/**
* Log a message sent to the server.
*
* @param string $message
* @return void
*/
public function message(string $message): void
{
$message = json_decode($message, true);

if (isset($message['data']['channel_data'])) {
$message['data']['channel_data'] = json_decode($message['data']['channel_data'], true);
}

$message = json_encode($message, JSON_PRETTY_PRINT);

fwrite(STDOUT, $message.PHP_EOL);
}
}
14 changes: 14 additions & 0 deletions src/Output.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Laravel\Reverb;

use Illuminate\Support\Facades\Facade;
use Laravel\Reverb\Contracts\Logger;

class Output extends Facade
{
protected static function getFacadeAccessor()
{
return Logger::class;
}
}
Loading

0 comments on commit f763887

Please sign in to comment.