From 5b2a1082349afc70dd9c0a5476e078c6edc29d5d Mon Sep 17 00:00:00 2001 From: prophet777 Date: Wed, 27 May 2015 13:46:21 +0200 Subject: [PATCH] initial commit --- .gitignore | 2 ++ .php_cs | 15 +++++++++ LICENSE | 19 ++++++++++++ README.md | 43 +++++++++++++++++++++++++ composer.json | 26 ++++++++++++++++ src/Builder.php | 76 +++++++++++++++++++++++++++++++++++++++++++++ src/ServerStack.php | 61 ++++++++++++++++++++++++++++++++++++ 7 files changed, 242 insertions(+) create mode 100644 .gitignore create mode 100644 .php_cs create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/Builder.php create mode 100644 src/ServerStack.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a9875b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +composer.lock diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..ac0bdfd --- /dev/null +++ b/.php_cs @@ -0,0 +1,15 @@ +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__) + ) +; \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..24c8463 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..87a6dc3 --- /dev/null +++ b/README.md @@ -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(); +``` + diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..c160648 --- /dev/null +++ b/composer.json @@ -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": "johann_27@hotmail.fr" + } + ], + "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" + } + } +} diff --git a/src/Builder.php b/src/Builder.php new file mode 100644 index 0000000..9578b58 --- /dev/null +++ b/src/Builder.php @@ -0,0 +1,76 @@ +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); + } +} diff --git a/src/ServerStack.php b/src/ServerStack.php new file mode 100644 index 0000000..e3b2cdd --- /dev/null +++ b/src/ServerStack.php @@ -0,0 +1,61 @@ +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); + } +}