Skip to content
This repository has been archived by the owner on Feb 13, 2022. It is now read-only.

Commit

Permalink
Merge pull request #171 from leocavalcante/routing
Browse files Browse the repository at this point in the history
fix: swoole affected by Route\DID_MATCH
  • Loading branch information
leocavalcante authored Apr 1, 2019
2 parents 3f68d59 + c9e66b5 commit 4868cb0
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 11 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"ext-mongodb": "^1.5",
"cboden/ratchet": "^0.4",
"cocur/slugify": "^3.2",
"eaglewu/swoole-ide-helper": "dev-master",
"gabordemooij/redbean": "^5.2",
"mongodb/mongodb": "^1.4",
"monolog/monolog": "^1.24",
Expand Down
46 changes: 44 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 22 additions & 9 deletions src/Swoole/Swoole.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

namespace Siler\Swoole;

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use Siler\Container;
use const Siler\Route\DID_MATCH;

const SWOOLE_HTTP_REQUEST = 'swoole_http_request';
const SWOOLE_HTTP_REQUEST_ENDED = 'swoole_http_request_ended';
Expand All @@ -24,13 +28,14 @@
* @param int $port The port binding (defaults to 9501).
* @param string $host The host binding (defaults to 0.0.0.0).
*
* @return \Swoole\Http\Server
* @return Server
*/
function http(callable $handler, int $port = 9501, string $host = '0.0.0.0')
function http(callable $handler, int $port = 9501, string $host = '0.0.0.0'): Server
{
$server = new \Swoole\Http\Server($host, $port);
$server = new Server($host, $port);

$server->on('request', function ($request, $response) use ($handler) {
Container\set(DID_MATCH, false);
Container\set(SWOOLE_HTTP_REQUEST_ENDED, false);
Container\set(SWOOLE_HTTP_REQUEST, $request);
Container\set(SWOOLE_HTTP_RESPONSE, $response);
Expand All @@ -44,15 +49,15 @@ function http(callable $handler, int $port = 9501, string $host = '0.0.0.0')
/**
* Gets the current Swoole HTTP request.
*/
function request()
function request(): Request
{
return Container\get(SWOOLE_HTTP_REQUEST);
}

/**
* Gets the current Swoole HTTP response.
*/
function response()
function response(): Response
{
return Container\get(SWOOLE_HTTP_RESPONSE);
}
Expand All @@ -63,6 +68,8 @@ function response()
* @param string $content Content for the output.
* @param int $status HTTP response status code.
* @param array $headers HTTP response headers.
*
* @return null
*/
function emit(string $content, int $status = 200, array $headers = [])
{
Expand All @@ -78,15 +85,19 @@ function emit(string $content, int $status = 200, array $headers = [])

Container\set(SWOOLE_HTTP_REQUEST_ENDED, true);

return response()->end($content);
response()->end($content);

return null;
}

/**
* Sugar to emit() JSON encoded data.
*
* @param mixed $data
* @param int $status
* @param int $status
* @param array $headers
*
* @return null
*/
function json($data, int $status = 200, array $headers = [])
{
Expand Down Expand Up @@ -125,8 +136,10 @@ function websocket_hooks(array $hooks)
* Returns a Swoole\WebSocket\Server.
*
* @param callable $handler The handler to call on each message.
* @param int $port The port binding (defaults to 9502).
* @param string $host The host binding (defaults to 0.0.0.0).
* @param int $port The port binding (defaults to 9502).
* @param string $host The host binding (defaults to 0.0.0.0).
*
* @return \Swoole\WebSocket\Server
*/
function websocket(callable $handler, int $port = 9502, string $host = '0.0.0.0'): \Swoole\WebSocket\Server
{
Expand Down

0 comments on commit 4868cb0

Please sign in to comment.