setApp($app); } public static function isEnabled(): bool { return app(Logger::class)->enabled; } public function __construct(OutputInterface $consoleOutput) { $this->consoleOutput = $consoleOutput; } public function enable($enabled = true) { $this->enabled = $enabled; return $this; } public function verbose($verbose = false) { $this->verbose = $verbose; return $this; } public function setApp(MessageComponentInterface $app) { $this->app = $app; return $this; } public function onOpen(ConnectionInterface $connection) { $request = $connection->httpRequest; $queryParameters = []; parse_str($request->getUri()->getQuery(), $queryParameters); $this->warn("New connection opened for app key {$queryParameters['appKey']}."); $this->app->onOpen($connection); } public function onMessage(ConnectionInterface $connection, MessageInterface $message) { $this->info("{$connection->appId}: connection id {$connection->socketId} received message: {$message->getPayload()}."); $this->app->onMessage($connection, $message); } public function onClose(ConnectionInterface $connection) { $this->warn("{$connection->appId}: connection id {$connection->socketId} closed."); $this->app->onClose($connection); } public function onError(ConnectionInterface $connection, Exception $exception) { $exceptionClass = get_class($exception); $appId = $connection->appId ?? 'Unknown app id'; $message = "{$appId}: exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`"; if ($this->verbose) { $message .= $exception->getTraceAsString(); } $this->error($message); $this->app->onError($connection, $exception); } protected function info(string $message) { $this->line($message, 'info'); } protected function warn(string $message) { $this->line($message, 'warning'); } protected function error(string $message) { $this->line($message, 'error'); } protected function line(string $message, string $style) { $styled = $style ? "<$style>$message" : $message; $this->consoleOutput->writeln($styled); } }