A request handler adapter for swoole, using PSR-7, PSR-15 and PSR-17.
- php: ^8.2
- ext-swoole: ^5.1.6|6.0.0-RC1|^6.0
- dflydev/fig-cookies: ^3.1
- psr/http-factory: ^1.1
- psr/http-message: ^1.1|^2.0
- psr/http-server-handler: ^1.0.2
- psr/log: ^2.0|^3.0.2
Through Composer as chubbyphp/chubbyphp-swoole-request-handler.
composer require chubbyphp/chubbyphp-swoole-request-handler "^1.5"
<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\SwooleRequestHandler\OnRequest;
use Chubbyphp\SwooleRequestHandler\PsrRequestFactory;
use Chubbyphp\SwooleRequestHandler\SwooleResponseEmitter;
use Psr\Http\Server\RequestHandlerInterface;
use Some\Psr17\Factory\ServerRequestFactory;
use Some\Psr17\Factory\StreamFactory;
use Some\Psr17\Factory\UploadedFileFactory;
use Swoole\Http\Server;
$loader = require __DIR__.'/vendor/autoload.php';
/** @var RequestHandlerInterface $app*/
$app = ...;
$http = new Server('0.0.0.0', 8080);
$http->on('start', function (Server $server): void {
echo 'Swoole http server is started at http://0.0.0.0:8080'.PHP_EOL;
});
$http->on('request', new OnRequest(
new PsrRequestFactory(
new ServerRequestFactory(),
new StreamFactory(),
new UploadedFileFactory()
),
new SwooleResponseEmitter(),
$app
));
$http->start();
<?php
declare(strict_types=1);
namespace App;
use Blackfire\Client;
use Chubbyphp\SwooleRequestHandler\Adapter\BlackfireOnRequestAdapter;
use Chubbyphp\SwooleRequestHandler\OnRequest;
/** @var OnRequest $onRequest */
$onRequest = ...;
if (extension_loaded('blackfire') {
$onRequest = new BlackfireOnRequestAdapter($onRequest, new Client());
}
$http->on('request', $onRequest);
<?php
declare(strict_types=1);
namespace App;
use Chubbyphp\SwooleRequestHandler\Adapter\NewRelicOnRequestAdapter;
use Chubbyphp\SwooleRequestHandler\OnRequest;
/** @var OnRequest $onRequest */
$onRequest = ...;
if (extension_loaded('newrelic') && false !== $name = ini_get('newrelic.appname')) {
$onRequest = new NewRelicOnRequestAdapter($onRequest, $name);
}
$http->on('request', $onRequest);
2024 Dominik Zogg