Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
prophet777 committed May 27, 2015
0 parents commit 5b2a108
Show file tree
Hide file tree
Showing 7 changed files with 242 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
composer.lock
15 changes: 15 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)
->fixers(array(
'ordered_use',
'multiline_spaces_before_semicolon',
'concat_with_spaces'
))
->finder(
Symfony\CS\Finder\DefaultFinder::create()
->exclude(['vendor'])
->in(__DIR__)
)
;
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2011-2015 Johann Saunier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Ratchet Stack
==============

Builder for ratchet middlewares based on ComponentInterface.

Ratchet Stack Builder is a small library that helps you construct a nested ComponentInterface decorator tree. It models it as a stack of middlewares.

Inspired of [StackPHP](https://github.com/stackphp/builder)


## Installation

```cmd
composer require gos/ratchet-stack
```

## Example

```php
use Gos\Component\RatchetStack\Builder;
use React\Socket\Server;
use React\EventLoop\Factory;

$stack = new Builder();
$loop = Factory::create();

$socket = new Server($loop);
$socket->listen($this->port, $this->host);

$stack
->push('Ratchet\Server\IoServer', $socket, $loop)
->push('Ratchet\Http\HttpServer')
->push('Ratchet\WebSocket\WsServer')
->push('Ratchet\Session\SessionProvider', $this->sessionHandler)
->push('Ratchet\Wamp\WampServer')
;

$wampApplication = new WampApplication(); //Instance of WampServerInterface

$app = $stack->resolve($wampApplication); //Give IoServer instance
$app->run();
```

26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "gos/ratchet-stack",
"type": "library",
"description": "Ratchet Stack",
"keywords": ["Stack", "Application", "Ratchet"],
"homepage": "https://github.com/GeniusesOfSymfony/RatchetStack",
"license": "MIT",
"authors": [
{
"name": "Johann Saunier",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3",
"cboden/Ratchet": "~0.3.0"
},
"autoload": {
"psr-4": { "Gos\\Component\\RatchetStack\\": "src/" }
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
76 changes: 76 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Gos\Component\RatchetStack;

use Ratchet\ComponentInterface;

class Builder
{
/**
* @var \SplStack
*/
protected $components;

public function __construct()
{
$this->components = new \SplStack();
}

/**
* @return $this
*/
public function unshift(/*$kernelClass, $args...*/)
{
if (func_num_args() === 0) {
throw new \InvalidArgumentException('Missing argument(s) when calling unshift');
}

$msgApp = func_get_args();
$this->components->unshift($msgApp);

return $this;
}

/**
* @return $this
*/
public function push(/*$kernelClass, $args...*/)
{
if (func_num_args() === 0) {
throw new \InvalidArgumentException('Missing argument(s) when calling push');
}

$msgApp = func_get_args();
$this->components->push($msgApp);

return $this;
}

/**
* @param ComponentInterface $component
*
* @return ServerStack
*/
public function resolve(ComponentInterface $component)
{
$middlewares = array($component);

foreach ($this->components as $comp) {
$args = $comp;
$firstArg = array_shift($args);

if (is_callable($firstArg)) {
$component = $firstArg($component);
} else {
$class = $firstArg;
array_unshift($args, $component);
$reflection = new \ReflectionClass($class);
$component = $reflection->newInstanceArgs($args);
}

array_unshift($middlewares, $component);
}

return new ServerStack($component);
}
}
61 changes: 61 additions & 0 deletions src/ServerStack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Gos\Component\RatchetStack;

use Ratchet\Server\IoServer;

class ServerStack /* Implements IoServerInterface */
{
/**
* @var IoServer
*/
private $server;

/**
* @param IoServer $server
*/
public function __construct(IoServer $server)
{
$this->server = $server;
}

/**
* {@inheritdoc}
*/
public function run()
{
$this->server->run();
}

/**
* {@inheritdoc}
*/
public function handleConnect($conn)
{
$this->server->handleConnect($conn);
}

/**
* {@inheritdoc}
*/
public function handleData($data, $conn)
{
$this->server->handleData($data, $conn);
}

/**
* {@inheritdoc}
*/
public function handleEnd($conn)
{
$this->server->handleEnd($conn);
}

/**
* {@inheritdoc}
*/
public function handleError(\Exception $e, $conn)
{
$this->server->handleError($e, $conn);
}
}

0 comments on commit 5b2a108

Please sign in to comment.