Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Php82 #8

Merged
merged 7 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ on:
push:
branches:
- "master"
# schedule:
# - cron: "42 3 * * *"
workflow_dispatch:
schedule:
- cron: "42 3 * * *"

jobs:
phpunit:
Expand Down
37 changes: 3 additions & 34 deletions src/Ratchet/Http/HttpRequestParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
use Ratchet\MessageInterface;
use Ratchet\ConnectionInterface;
use GuzzleHttp\Psr7\Message;
use Ratchet\Traits\DynamicPropertiesTrait;

/**
* This class receives streaming data from a client request
* and parses HTTP headers, returning a PSR-7 Request object
* once it's been buffered
*/
class HttpRequestParser implements MessageInterface {
use DynamicPropertiesTrait;

const EOM = "\r\n\r\n";

/**
Expand All @@ -19,40 +22,6 @@ class HttpRequestParser implements MessageInterface {
*/
public $maxSize = 4096;

/**
* Storage for dynamic properties.
*
* @var array
*/
protected $_properties = [];

/**
* Allow setting dynamic properties.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set($key, $value) {
if (property_exists($this, $key)) {
$this->_properties[$key] = $value;
}
}

/**
* Get a property that has been declared dynamically
*
* @param string $key
*
* @return mixed|void
*/
public function __get($key) {
if (isset($this->_properties[$key])) {
return $this->_properties[$key];
}
}

/**
* @param \Ratchet\ConnectionInterface $context
* @param string $data Data stream to buffer
Expand Down
38 changes: 3 additions & 35 deletions src/Ratchet/Http/HttpServer.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php
namespace Ratchet\Http;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\Traits\DynamicPropertiesTrait;

class HttpServer implements MessageComponentInterface {
use CloseResponseTrait;
use CloseResponseTrait, DynamicPropertiesTrait;

/**
* Buffers incoming HTTP requests returning a Guzzle Request when coalesced
Expand All @@ -18,13 +19,6 @@ class HttpServer implements MessageComponentInterface {
*/
protected $_httpServer;

/**
* Storage for dynamic properties.
*
* @var array
*/
protected $_properties = [];

/**
* @param HttpServerInterface
*/
Expand All @@ -33,32 +27,6 @@ public function __construct(HttpServerInterface $component) {
$this->_reqParser = new HttpRequestParser;
}

/**
* Allow setting dynamic properties.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set($key, $value) {
if (property_exists($this, $key)) {
$this->_properties[$key] = $value;
}
}

/**
* Get a property that has been declared dynamically
*
* @param string $key
*
* @return mixed|void
*/
public function __get($key) {
if (isset($this->_properties[$key])) {
return $this->_properties[$key];
}
}

/**
* {@inheritdoc}
Expand Down
2 changes: 1 addition & 1 deletion src/Ratchet/Http/OriginCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ function onClose(ConnectionInterface $conn) {
function onError(ConnectionInterface $conn, \Exception $e) {
return $this->_component->onError($conn, $e);
}
}
}
6 changes: 3 additions & 3 deletions src/Ratchet/Http/Router.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
namespace Ratchet\Http;
use Ratchet\ConnectionInterface;
use GuzzleHttp\Psr7\Query;
use Psr\Http\Message\RequestInterface;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use GuzzleHttp\Psr7\Query;
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;

class Router implements HttpServerInterface {
use CloseResponseTrait;
Expand Down
16 changes: 2 additions & 14 deletions src/Ratchet/Server/IoConnection.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
<?php
namespace Ratchet\Server;
use Ratchet\ConnectionInterface;
use Ratchet\Traits\DynamicPropertiesTrait;
use React\Socket\ConnectionInterface as ReactConn;

/**
* {@inheritdoc}
*/
class IoConnection implements ConnectionInterface {
use DynamicPropertiesTrait;
/**
* @var \React\Socket\ConnectionInterface
*/
protected $conn;

protected $properties = [];

public function __set($key, $value) {
if (!property_exists($this, $key)) {
$this->properties[$key] = $value;
}
}

public function __get($key) {
if (isset($this->properties[$key])) {
return $this->properties[$key];
}
}

/**
* @param \React\Socket\ConnectionInterface $conn
*/
Expand Down
37 changes: 3 additions & 34 deletions src/Ratchet/Server/IoServer.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Ratchet\Server;
use Ratchet\MessageComponentInterface;
use Ratchet\Traits\DynamicPropertiesTrait;
use React\EventLoop\LoopInterface;
use React\Socket\ServerInterface;
use React\EventLoop\Factory as LoopFactory;
Expand All @@ -12,6 +13,8 @@
* Events are delegated through this to attached applications
*/
class IoServer {
use DynamicPropertiesTrait;

/**
* @var \React\EventLoop\LoopInterface
*/
Expand All @@ -28,13 +31,6 @@ class IoServer {
*/
public $socket;

/**
* Storage for dynamic properties.
*
* @var array
*/
protected $_properties = [];

/**
* @param \Ratchet\MessageComponentInterface $app The Ratchet application stack to host
* @param \React\Socket\ServerInterface $socket The React socket server to run the Ratchet application off of
Expand All @@ -55,33 +51,6 @@ public function __construct(MessageComponentInterface $app, ServerInterface $soc
$socket->on('connection', array($this, 'handleConnect'));
}

/**
* Allow setting dynamic properties.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set($key, $value) {
if (property_exists($this, $key)) {
$this->_properties[$key] = $value;
}
}

/**
* Get a property that has been declared dynamically
*
* @param string $key
*
* @return mixed|void
*/
public function __get($key) {
if (isset($this->_properties[$key])) {
return $this->_properties[$key];
}
}

/**
* @param \Ratchet\MessageComponentInterface $component The application that I/O will call when events are received
* @param int $port The port to server sockets on
Expand Down
49 changes: 49 additions & 0 deletions src/Ratchet/Traits/DynamicPropertiesTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Ratchet\Traits;

trait DynamicPropertiesTrait
{
/**
* Storage for dynamic properties.
*
* @var array
*/
protected $_dynamic_properties = [];

/**
* Allow setting dynamic properties.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set($key, $value) {
if (property_exists($this, $key)) {
$this->_dynamic_properties[$key] = $value;
}
}

/**
* Get a property that has been declared dynamically
*
* @param string $key
*
* @return mixed|void
*/
public function __get($key) {
if (isset($this->_dynamic_properties[$key])) {
return $this->_dynamic_properties[$key];
}
}

/**
* @param $key
*
* @return bool
*/
public function __isset($key) {
return isset($this->_dynamic_properties[$key]);
}
}
17 changes: 14 additions & 3 deletions src/Ratchet/WebSocket/WsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,28 @@ public function send($msg) {
}

/**
* @param int|\Ratchet\RFC6455\Messaging\DataInterface
* @param int|\Ratchet\RFC6455\Messaging\DataInterface $code
* @param null|string $reason
*/
public function close($code = 1000) {
public function close($code = 1000, $reason = null) {
if ($this->WebSocket->closing) {
return;
}

if ($code instanceof DataInterface) {
$this->send($code);
} else {
$this->send(new Frame(pack('n', $code), true, Frame::OP_CLOSE));
if (!is_string($reason)) {
$frame = new Frame(pack('n', $code), true, Frame::OP_CLOSE);
} else {
// Limit reason to 123 bytes to fit into the remainder of the 125 byte payload limit
while (strlen($reason) > 123) {
$reason = substr($reason, 0, -1);
}
$frame = new Frame(pack('nA*', $code, $reason), true, Frame::OP_CLOSE);
}

$this->send($frame);
}

$this->getConnection()->close();
Expand Down
Loading