From ed26893e9ff9f051be601828b657c032c285795e Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Sun, 7 Aug 2022 20:16:20 +0200 Subject: [PATCH 1/2] Added LoggerAwareInterface support and abstract implementation --- src/Application/LoggerAwareApplication.php | 29 ++++++++++++++++++++++ src/Application/StatusApplication.php | 1 + src/Server.php | 7 +++++- 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/Application/LoggerAwareApplication.php diff --git a/src/Application/LoggerAwareApplication.php b/src/Application/LoggerAwareApplication.php new file mode 100644 index 0000000..0a4a9ba --- /dev/null +++ b/src/Application/LoggerAwareApplication.php @@ -0,0 +1,29 @@ +logger = new NullLogger(); + } + + public function setLogger(LoggerInterface $logger): void + { + $this->logger = $logger; + } + + protected function getLogger(): LoggerInterface + { + return $this->logger; + } +} diff --git a/src/Application/StatusApplication.php b/src/Application/StatusApplication.php index 067d9e1..ef98c8a 100644 --- a/src/Application/StatusApplication.php +++ b/src/Application/StatusApplication.php @@ -164,6 +164,7 @@ public function statusMsg(string $text, string $type = 'info'): void ]; $encodedData = $this->encodeData('statusMsg', $data); $this->sendAll($encodedData); + $this->getLogger()->log($type, 'Status message: ' . $text); } /** diff --git a/src/Server.php b/src/Server.php index 4ad301b..d151fa8 100644 --- a/src/Server.php +++ b/src/Server.php @@ -5,6 +5,7 @@ namespace Bloatless\WebSocket; use Bloatless\WebSocket\Application\ApplicationInterface; +use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerInterface; /** @@ -142,7 +143,7 @@ public function run(): void while (true) { $this->timers->runAll(); - + $changed_sockets = $this->allsockets; @stream_select($changed_sockets, $write, $except, 0, 5000); foreach ($changed_sockets as $socket) { @@ -247,6 +248,10 @@ public function registerApplication(string $key, ApplicationInterface $applicati { $this->applications[$key] = $application; + if ($application instanceof LoggerAwareInterface) { + $application->setLogger($this->logger); + } + // status is kind of a system-app, needs some special cases: if ($key === 'status') { $serverInfo = array( From 4f1185037694ca7c425f19159e4547efd271397a Mon Sep 17 00:00:00 2001 From: Ricardo Boss Date: Sun, 7 Aug 2022 20:19:55 +0200 Subject: [PATCH 2/2] Make StatusApplication extend LoggerAwareApplication --- src/Application/StatusApplication.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Application/StatusApplication.php b/src/Application/StatusApplication.php index ef98c8a..724b7ed 100644 --- a/src/Application/StatusApplication.php +++ b/src/Application/StatusApplication.php @@ -6,7 +6,7 @@ use Bloatless\WebSocket\Connection; -class StatusApplication extends Application +class StatusApplication extends LoggerAwareApplication { /** * Holds client connected to the status application.