Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Nov 7, 2018
0 parents commit ece602a
Show file tree
Hide file tree
Showing 5 changed files with 116 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 @@
.idea
/vendor/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php
require dirname(__DIR__) . '/autoload.php';
exit((require dirname(__DIR__) . '/vendor/bear/swoole/bootstrap.php')(
'prod-hal-app',
'BEAR\Skeleton',
'127.0.0.1',
'8080'
));
```


## Execute

You can run an BEAR.Sunday application with Swoole using the following command:


php bin/swoole.php
43 changes: 43 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use BEAR\Package\Bootstrap;
use BEAR\Resource\ResourceObject;
use BEAR\Sunday\Extension\Router\RouterMatch;
use BEAR\Sunday\Extension\Transfer\TransferInterface;
use BEAR\Sunday\Provide\Error\VndError;
use Swoole\Http\Request;
use Swoole\Http\Response;

$responder = require __DIR__ . 'responder.php';

return function (string $context, string $name, string $ip, string $port) use ($responder) : int {
if (! class_exists('swoole_http_server')) {
throw new \RuntimeException('Swoole is not installed. See https://github.com/swoole/swoole-src/wiki/Installing');
}
$http = new swoole_http_server($ip, $port);
$http->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();
};
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]"
}
]
}
23 changes: 23 additions & 0 deletions responder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

return new class implements TransferInterface {
/**
* @var Response
*/
private $response;

public function setResponse(Response $response)
{
$this->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);
}
};

0 comments on commit ece602a

Please sign in to comment.