This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 152
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
Showing
8 changed files
with
521 additions
and
3 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
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,10 @@ | ||
#!/bin/bash | ||
pecl install swoole << EOF | ||
`#enable debug/trace log support? [no] :` | ||
`#enable sockets supports? [no] :`y | ||
`#enable openssl support? [no] :` | ||
`#enable http2 support? [no] :` | ||
`#enable async-redis support? [no] :` | ||
`#enable mysqlnd support? [no] :` | ||
`#enable postgresql coroutine client support? [no] :` | ||
EOF |
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,54 @@ | ||
# Swoole | ||
use Zend\Diactoros\ServerRequestFactory; | ||
|
||
[Swoole](https://www.swoole.co.uk/) is an async programming Framework for PHP | ||
that can be used to create high performance HTTP server applications, e.g. web | ||
APIs. We provided the support of Swoole in `zend-diactoros` using two methods to | ||
convert a [Swoole\Http\Request](http://php.net/manual/en/class.swoole-http-request.php) | ||
in a [PSR-7 Request](https://www.php-fig.org/psr/psr-7/#32-psrhttpmessagerequestinterface) | ||
and a [PSR-7 Response](https://www.php-fig.org/psr/psr-7/#33-psrhttpmessageresponseinterface) | ||
in a [Swoole\Http\Response](http://php.net/manual/en/class.swoole-http-response.php). | ||
|
||
To convert a Swoole request in PSR-7 we provided the following static function: | ||
|
||
``` | ||
Zend\Diactoros\ServerRequestFactory::fromSwoole(swoole_http_request $request) | ||
``` | ||
|
||
Where `$request` is an instance of `swoole_http_request` (alias of | ||
`Swoole\Http\Request`). | ||
|
||
To convert a PSR-7 response in a Swoole response we built a specific emitter, | ||
`Zend\Diactoros\Response\SwooleEmitter`. If you can use this emitter instead of | ||
`Zend\Diactoros\Response\SapiEmitter`. | ||
|
||
You need to instantiate the `SwooleEmitter` passing a `swoole_http_response` | ||
object in the constructor. | ||
|
||
You can execute Swoole with [Expressive](https://getexpressive.org/) using the | ||
following server implementation: | ||
|
||
```php | ||
use Zend\Diactoros\ServerRequestFactory; | ||
use Zend\Diactoros\Response\SwooleEmitter; | ||
|
||
$http = new swoole_http_server("127.0.0.1", 9501); | ||
|
||
$http->on("start", function ($server) { | ||
echo "Swoole http server is started at http://127.0.0.1:9501\n"; | ||
}); | ||
|
||
// Bootstrap of zend-expressive | ||
$container = require 'config/container.php'; | ||
$app = $container->get(\Zend\Expressive\Application::class); | ||
|
||
$http->on("request", function ($request, $response) use ($app) { | ||
$psr7Request = ServerRequestFactory::fromSwoole($request); | ||
$psr7Response = $app->handle($psr7Request); | ||
|
||
$emitter = new SwooleEmitter($response); | ||
$emitter->emit($psr7Response); | ||
}); | ||
|
||
$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
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
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,189 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository | ||
* @copyright Copyright (c) 2015-2017 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Diactoros; | ||
|
||
use Psr\Http\Message\StreamInterface; | ||
use RuntimeException; | ||
use swoole_http_request; | ||
|
||
class SwooleStream implements StreamInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $body; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $bodySize; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
protected $index = 0; | ||
|
||
public function __construct(swoole_http_request $request) | ||
{ | ||
$this->request = $request; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getContents() | ||
{ | ||
if (! isset($this->body)) { | ||
$this->body = $this->request->rawcontent(); | ||
} | ||
return $this->body; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __toString() | ||
{ | ||
return $this->getContents(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSize() | ||
{ | ||
if (! isset($this->bodySize)) { | ||
$this->bodySize = strlen($this->getContents()); | ||
} | ||
return $this->bodySize; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function tell() | ||
{ | ||
return $this->index; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function eof() | ||
{ | ||
return $this->index >= $this->getSize() - 1; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isReadable() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($length) | ||
{ | ||
$result = substr($this->getContents(), $this->index, $length); | ||
$this->index += $length; | ||
return $result; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isSeekable() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function seek($offset, $whence = SEEK_SET) | ||
{ | ||
|
||
switch ($whence) { | ||
case SEEK_SET: | ||
if ($offset >= $this->getSize()) { | ||
throw new RuntimeException( | ||
'Offset cannot be longer than content size' | ||
); | ||
} | ||
$this->index = $offset; | ||
break; | ||
case SEEK_CUR: | ||
if ($offset + $this->index >= $this->getSize()) { | ||
throw new RuntimeException( | ||
'Offset + current position cannot be longer than content size' | ||
); | ||
} | ||
$this->index += $offset; | ||
break; | ||
case SEEK_END: | ||
if ($offset + $this->getSize() >= $this->getSize()) { | ||
throw new RuntimeException( | ||
'Offset must be a negative number to be under the content size' | ||
); | ||
} | ||
$this->index = $this->getSize() - 1 + $offset; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function rewind() | ||
{ | ||
$this->index = 0; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isWritable() | ||
{ | ||
return false; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($string) | ||
{ | ||
throw new RuntimeException('Stream is not writable'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getMetadata($key = null) | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function detach() | ||
{ | ||
return null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
return null; | ||
} | ||
} |
Oops, something went wrong.