pachico / slim-swoole
Convenient library to run SlimPHP applications with Swoole
Installs: 9 423
Dependents: 0
Suggesters: 0
Security: 0
Stars: 90
Watchers: 11
Forks: 12
Open Issues: 2
Requires
- php: ^7.0
- dflydev/fig-cookies: ^1.0
- psr/http-message: ^1.0
Requires (Dev)
- eaglewu/swoole-ide-helper: dev-master
- phpunit/phpunit: 6.5.6
- slim/slim: ^3.9
- squizlabs/php_codesniffer: ^3.2
This package is auto-updated.
Last update: 2023-06-14 20:03:02 UTC
README
This is a brige library to run Slim framework Slim framework applications using Swoole engine.
Overview
The main purpose of this library is to easily run your already existing SlimPHP applications using Swoole Framework. It requires you to bootstrap your application only once when you start Swoole HTTP server and, thanks to its event driven design, it will process each request reusing your already started application for better performance.
The execution sequence is as follows:
- You bootstrap your SlimPHP application as you would normally do.
- You instantiate the
BrigeManager
passing to it your SlimPHP application. - You start Swoole's HTTP server.
- You bind to the
on('request')
event handler theBridgeManager
instance which will:- Transform the Swoole request to a SlimPHP based on server and request attributes.
- Process your request through SlimPHP's application stack (including middlewares)
- Merge SlimPHP Response to Swoole Response
- End the request. All this is done under the hood, so you will just need to call:
$bridgeManager->process($swooleRequest, $swooleResponse)->end();
(See usage paragraph for a complete example.)
Caution: it is still in development so any contribution and test will be more than welcome.
Requirements
- PHP-CLI >= 7.0 (Required by Swoole)
- Swoole framework (this has been tested with version 1.10.1)
Install
Via Composer
$ composer require pachico/slim-swoole
Usage
<?php use Pachico\SlimSwoole\BridgeManager; use Slim\Http; require __DIR__ . '/../vendor/autoload.php'; /** * This is how you would normally bootstrap your Slim application * For the sake of demonstration, we also add a simple middleware * to check that the entire app stack is being setup and executed * properly. */ $app = new \Slim\App(); $app->any('/foo[/{myArg}]', function (Http\Request $request, Http\Response $response, array $args) { $data = [ 'args' => $args, 'body' => (string) $request->getBody(), 'parsedBody' => $request->getParsedBody(), 'params' => $request->getParams(), 'headers' => $request->getHeaders(), 'uploadedFiles' => $request->getUploadedFiles() ]; return $response->withJson($data); })->add(function (Http\Request $request, Http\Response $response, callable $next) { $response->getBody()->write('BEFORE' . PHP_EOL); $response = $next($request, $response); $response->getBody()->write(PHP_EOL . 'AFTER'); return $response; }); /** * We instanciate the BridgeManager (this library) */ $bridgeManager = new BridgeManager($app); /** * We start the Swoole server */ $http = new swoole_http_server("0.0.0.0", 8081); /** * We register the on "start" event */ $http->on("start", function (\swoole_http_server $server) { echo sprintf('Swoole http server is started at http://%s:%s', $server->host, $server->port), PHP_EOL; }); /** * We register the on "request event, which will use the BridgeManager to transform request, process it * as a Slim request and merge back the response * */ $http->on( "request", function (swoole_http_request $swooleRequest, swoole_http_response $swooleResponse) use ($bridgeManager) { $bridgeManager->process($swooleRequest, $swooleResponse)->end(); } ); $http->start();
Change log
Please see CHANGELOG for more information on what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
Security
If you discover any security related issues, please email pachicodev@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.