2018-11-27 20:30:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace BeyondCode\LaravelWebSockets\Server\Logger;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use Ratchet\MessageComponentInterface;
|
|
|
|
|
|
|
|
|
|
class HttpLogger extends Logger implements MessageComponentInterface
|
|
|
|
|
{
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* The HTTP app instance to watch.
|
|
|
|
|
*
|
|
|
|
|
* @var \Ratchet\Http\HttpServerInterface
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
protected $app;
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Create a new instance and add the app to watch.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\MessageComponentInterface $app
|
|
|
|
|
* @return Self
|
|
|
|
|
*/
|
2018-12-04 21:22:33 +00:00
|
|
|
public static function decorate(MessageComponentInterface $app): self
|
2018-11-27 20:30:33 +00:00
|
|
|
{
|
2018-12-04 21:22:33 +00:00
|
|
|
$logger = app(self::class);
|
2018-11-27 20:30:33 +00:00
|
|
|
|
|
|
|
|
return $logger->setApp($app);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Set a new app to watch.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\MessageComponentInterface $app
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function setApp(MessageComponentInterface $app)
|
|
|
|
|
{
|
|
|
|
|
$this->app = $app;
|
|
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP open request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function onOpen(ConnectionInterface $connection)
|
|
|
|
|
{
|
|
|
|
|
$this->app->onOpen($connection);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP message request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @param mixed $message
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function onMessage(ConnectionInterface $connection, $message)
|
|
|
|
|
{
|
|
|
|
|
$this->app->onMessage($connection, $message);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle the HTTP close request.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function onClose(ConnectionInterface $connection)
|
|
|
|
|
{
|
|
|
|
|
$this->app->onClose($connection);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-18 17:21:22 +00:00
|
|
|
/**
|
|
|
|
|
* Handle HTTP errors.
|
|
|
|
|
*
|
|
|
|
|
* @param \Ratchet\ConnectionInterface $connection
|
|
|
|
|
* @param Exception $exception
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-11-27 20:30:33 +00:00
|
|
|
public function onError(ConnectionInterface $connection, Exception $exception)
|
|
|
|
|
{
|
|
|
|
|
$exceptionClass = get_class($exception);
|
|
|
|
|
|
|
|
|
|
$message = "Exception `{$exceptionClass}` thrown: `{$exception->getMessage()}`";
|
|
|
|
|
|
|
|
|
|
if ($this->verbose) {
|
|
|
|
|
$message .= $exception->getTraceAsString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->error($message);
|
|
|
|
|
|
|
|
|
|
$this->app->onError($connection, $exception);
|
|
|
|
|
}
|
2018-12-04 21:22:33 +00:00
|
|
|
}
|