Skip to content

Commit

Permalink
improve inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Aug 6, 2023
1 parent 4510708 commit 1ee04ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/Inspector/Traits/InspectorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace Chevere\Xr\Inspector\Traits;

use Chevere\Xr\Exceptions\StopException;
use Chevere\Xr\Interfaces\ClientInterface;
use Chevere\Xr\Message;
use Throwable;

trait InspectorTrait
{
Expand All @@ -25,26 +25,18 @@ public function __construct(
}

public function pause(
string $body = '',
string $t = '',
string $e = '',
int $f = 0,
): void {
$backtrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 1);
$message = (new Message(
backtrace: $backtrace,
))
->withTopic($t)
->withEmote($e)
->withFlags($f);

try {
$this->client->sendPause($message);
} catch (StopException $e) {
if (PHP_SAPI === 'cli') {
echo '* ' . $e->getMessage() . PHP_EOL;
$this->client->exit(255);
}
}
$this->sendCommand(
command: 'sendPause',
body: $body,
topic: $t,
emote: $e,
flags: $f,
);
}

public function memory(
Expand All @@ -53,15 +45,17 @@ public function memory(
int $f = 0,
): void {
$memory = memory_get_usage(true);
$this->sendMessage(
$this->sendCommand(
command: 'sendMessage',
body: sprintf('%.2F MB', $memory / 1000000),
topic: $t,
emote: $e,
flags: $f,
);
}

private function sendMessage(
private function sendCommand(
string $command,
string $body = '',
string $topic = '',
string $emote = '',
Expand All @@ -77,6 +71,13 @@ private function sendMessage(
->withEmote($emote)
->withFlags($flags);

$this->client->sendMessage($message);
try {
$this->client->{$command}($message);
} catch (Throwable $e) {
if (PHP_SAPI === 'cli') {
echo '* ' . $e->getMessage() . PHP_EOL;
$this->client->exit(255);
}
}
}
}
1 change: 1 addition & 0 deletions src/Interfaces/InspectorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
interface InspectorInterface
{
public function pause(
string $body = '',
string $t = '',
string $e = '',
int $f = 0,
Expand Down

0 comments on commit 1ee04ea

Please sign in to comment.