|
| 1 | +# Swoole Runtime with nyholm/psr7 |
| 2 | + |
| 3 | +A runtime for [Swoole](https://www.swoole.co.uk/). |
| 4 | + |
| 5 | +If you are new to the Symfony Runtime component, read more in the [main readme](https://github.com/php-runtime/runtime). |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +``` |
| 10 | +composer require runtime/swoole-nyholm |
| 11 | +``` |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Define the environment variable `APP_RUNTIME` for your application. |
| 16 | + |
| 17 | +``` |
| 18 | +APP_RUNTIME=Runtime\SwooleNyholm\Runtime |
| 19 | +``` |
| 20 | + |
| 21 | +### Pure PHP |
| 22 | + |
| 23 | +```php |
| 24 | +// public/index.php |
| 25 | + |
| 26 | +use Swoole\Http\Request; |
| 27 | +use Swoole\Http\Response; |
| 28 | + |
| 29 | +require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; |
| 30 | + |
| 31 | +return function () { |
| 32 | + return function (Request $request, Response $response) { |
| 33 | + $response->header("Content-Type", "text/plain"); |
| 34 | + $response->end("Hello World\n"); |
| 35 | + }; |
| 36 | +}; |
| 37 | +``` |
| 38 | + |
| 39 | +### PSR |
| 40 | + |
| 41 | +```php |
| 42 | +// public/index.php |
| 43 | + |
| 44 | +use Nyholm\Psr7\Response; |
| 45 | +use Psr\Http\Message\ResponseInterface; |
| 46 | +use Psr\Http\Message\ServerRequestInterface; |
| 47 | +use Psr\Http\Server\RequestHandlerInterface; |
| 48 | + |
| 49 | +require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; |
| 50 | + |
| 51 | +class App implements RequestHandlerInterface { |
| 52 | + public function handle(ServerRequestInterface $request): ResponseInterface { |
| 53 | + $name = $request->getQueryParams()['name'] ?? 'World'; |
| 54 | + return new Response(200, ['Server' => 'swoole-runtime'], "Hello, $name!"); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +return function(): RequestHandlerInterface { |
| 59 | + return new App(); |
| 60 | +}; |
| 61 | +``` |
| 62 | + |
| 63 | +## Using Options |
| 64 | + |
| 65 | +You can define some configurations using Symfony's Runtime `APP_RUNTIME_OPTIONS` API. |
| 66 | + |
| 67 | +| Option | Description | Default | |
| 68 | +| --- | --- | --- | |
| 69 | +| `host` | The host where the server should binds to (precedes `SWOOLE_HOST` environment variable) | `127.0.0.1` | |
| 70 | +| `port` | The port where the server should be listing (precedes `SWOOLE_PORT` environment variable) | `8000` | |
| 71 | +| `mode` | Swoole's server mode (precedes `SWOOLE_MODE` environment variable) | `SWOOLE_PROCESS` | |
| 72 | +| `settings` | All Swoole's server settings ([swoole.co.uk/docs/modules/swoole-server/configuration](https://www.swoole.co.uk/docs/modules/swoole-server/configuration)) | `[]` | |
| 73 | + |
| 74 | +```php |
| 75 | +// public/index.php |
| 76 | + |
| 77 | +use App\Kernel; |
| 78 | + |
| 79 | +$_SERVER['APP_RUNTIME_OPTIONS'] = [ |
| 80 | + 'host' => '0.0.0.0', |
| 81 | + 'port' => 9501, |
| 82 | + 'mode' => SWOOLE_BASE, |
| 83 | + 'settings' => [ |
| 84 | + \Swoole\Constant::OPTION_WORKER_NUM => swoole_cpu_num() * 2, |
| 85 | + \Swoole\Constant::OPTION_ENABLE_STATIC_HANDLER => true, |
| 86 | + \Swoole\Constant::OPTION_DOCUMENT_ROOT => dirname(__DIR__).'/public' |
| 87 | + ], |
| 88 | +]; |
| 89 | + |
| 90 | +require_once dirname(__DIR__).'/vendor/autoload_runtime.php'; |
| 91 | + |
| 92 | +return function (array $context) { |
| 93 | + return new Kernel($context['APP_ENV'], (bool) $context['APP_DEBUG']); |
| 94 | +}; |
| 95 | +``` |
0 commit comments