-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ece602a
Showing
5 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
/vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |