diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fd1dab --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +/vendor/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..3d6afb4 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# BEAR.Swoole + +This library provides the support of Swoole into an BEAR.Sunday application. + +## Installation + +Run the following to install this library: + + composer require bear/swoole + + +## Entry Script + +Place the entry script file at `bin/swoole.php` with IP address and port number. + +```php +on("start", function ($server) use ($ip, $port) { + echo "Swoole http server is started at http://{$ip}:{$port}" . PHP_EOL; + }); + $app = (new Bootstrap)->getApp($name, $context); + $http->on("request", function (Request $request, Response $response) use ($app, $responder) { + if ($app->httpCache->isNotModified($request->server)) { + $response->status(304); + $response->end(''); + } + $method = strtolower($request->server['request_method']); + $query = $method === 'get' ? $request->get : $request->post; + $path = 'page://self'. $request->server['request_uri']; + try { + /* @var ResourceObject $ro */ + $ro = $app->resource->{$method}->uri($path)($query); + $responder->setResponse($response); + $ro->transfer($responder, $_SERVER); + } catch (\Exception $e) { + echo (string) $e . PHP_EOL; // on server screen + $match = new RouterMatch; + [$match->method, $match->path, $match->query] = [$method, $path, $query]; + (new VndError($responder))->handle($e, $match)->transfer(); + } + }); + $http->start(); +}; diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..f09e710 --- /dev/null +++ b/composer.json @@ -0,0 +1,15 @@ +{ + "name": "bear/swoole", + "description": "Swoole script for BEAR.Sunday", + "type": "library", + "require-dev": { + "eaglewu/swoole-ide-helper": "dev-master" + }, + "license": "MIT", + "authors": [ + { + "name": "Akihito Koriyama", + "email": "akihito.koriyama@gmail.com" + } + ] +} diff --git a/responder.php b/responder.php new file mode 100644 index 0000000..f903fd5 --- /dev/null +++ b/responder.php @@ -0,0 +1,23 @@ +response = $response; + } + + public function __invoke(ResourceObject $ro, array $server) + { + $ro->toString(); + foreach ($ro->headers as $key => $value) { + $this->response->header($key, $value); + } + $this->response->status($ro->code); + $this->response->end($ro->view); + } +};