laravel-websockets/src/Server/Logger/ConnectionLogger.php

101 lines
1.9 KiB
PHP
Raw Normal View History

2018-11-24 14:23:59 +00:00
<?php
namespace BeyondCode\LaravelWebSockets\Server\Logger;
use Ratchet\ConnectionInterface;
class ConnectionLogger extends Logger implements ConnectionInterface
{
2020-08-18 17:21:22 +00:00
/**
* The connection to watch.
*
* @var \Ratchet\ConnectionInterface
*/
2018-11-24 14:23:59 +00:00
protected $connection;
2020-08-18 17:21:22 +00:00
/**
* Create a new instance and add a connection to watch.
*
* @param \Ratchet\ConnectionInterface $connection
* @return Self
*/
2018-12-04 21:22:33 +00:00
public static function decorate(ConnectionInterface $app): self
2018-11-24 14:23:59 +00:00
{
2018-12-04 21:22:33 +00:00
$logger = app(self::class);
2018-11-24 14:23:59 +00:00
return $logger->setConnection($app);
}
2020-08-18 17:21:22 +00:00
/**
* Set a new connection to watch.
*
* @param \Ratchet\ConnectionInterface $connection
* @return $this
*/
2018-11-24 14:23:59 +00:00
public function setConnection(ConnectionInterface $connection)
{
$this->connection = $connection;
return $this;
}
2020-08-18 17:21:22 +00:00
/**
* Send data through the connection.
*
* @param mixed $data
* @return void
*/
2018-11-24 14:23:59 +00:00
public function send($data)
{
2018-12-04 16:25:11 +00:00
$socketId = $this->connection->socketId ?? null;
$this->info("Connection id {$socketId} sending message {$data}");
2018-11-24 14:23:59 +00:00
$this->connection->send($data);
}
2020-08-18 17:21:22 +00:00
/**
* Close the connection.
*
* @return void
*/
2018-11-24 14:23:59 +00:00
public function close()
{
2018-11-27 08:17:39 +00:00
$this->warn("Connection id {$this->connection->socketId} closing.");
2018-11-24 14:23:59 +00:00
$this->connection->close();
}
2020-08-18 17:21:22 +00:00
/**
* {@inheritdoc}
*/
2018-11-24 14:23:59 +00:00
public function __set($name, $value)
{
return $this->connection->$name = $value;
}
2020-08-18 17:21:22 +00:00
/**
* {@inheritdoc}
*/
2018-11-24 14:23:59 +00:00
public function __get($name)
{
return $this->connection->$name;
}
2020-08-18 17:21:22 +00:00
/**
* {@inheritdoc}
*/
2018-12-04 21:22:33 +00:00
public function __isset($name)
{
2018-11-24 14:23:59 +00:00
return isset($this->connection->$name);
}
2020-08-18 17:21:22 +00:00
/**
* {@inheritdoc}
*/
2018-12-04 21:22:33 +00:00
public function __unset($name)
{
2018-11-24 14:23:59 +00:00
unset($this->connection->$name);
}
2018-12-04 21:22:33 +00:00
}