packico / slim-swoole
该软件包已被废弃,不再维护。未建议替代包。
方便的库,用于使用 Swoole 运行 SlimPHP 应用程序
0.3.0
2019-06-12 18:35 UTC
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
这是一个桥接库,用于使用 Slim 框架 和 Swoole 引擎 运行 Slim 框架应用程序。
概述
此库的主要目的是轻松运行您现有的 SlimPHP 应用程序,使用 Swoole 框架。它要求您在启动 Swoole HTTP 服务器时仅引导应用程序一次,并得益于其事件驱动设计,它将重新使用您已启动的应用程序来处理每个请求,从而提高性能。
执行顺序如下
- 按照常规方式引导您的 SlimPHP 应用程序。
- 您将 SlimPHP 应用程序传递给
BrigeManager
实例化。 - 您启动 Swoole 的 HTTP 服务器。
- 您将
on('request')
事件处理器绑定到BridgeManager
实例,该实例将- 将 Swoole 请求转换为基于服务器和请求属性的 SlimPHP。
- 通过 SlimPHP 的应用程序堆栈(包括中间件)处理您的请求
- 将 SlimPHP 响应合并到 Swoole 响应
- 结束请求。所有这些都是在幕后完成的,所以您只需要调用
$bridgeManager->process($swooleRequest, $swooleResponse)->end();
(请参阅使用部分以获取完整示例。)。
注意:它仍在开发中,因此任何贡献和测试都将非常欢迎。
要求
- PHP-CLI >= 7.0(由 Swoole 需要)
- Swoole 框架(已测试与版本 1.10.1 兼容)
安装
通过 Composer
$ composer require pachico/slim-swoole
使用
<?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();
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
测试
$ composer test
贡献
请参阅 CONTRIBUTING 和 CODE_OF_CONDUCT 以获取详细信息。
安全
如果您发现任何安全相关的问题,请通过电子邮件 pachicodev@gmail.com 而不是使用问题跟踪器。
致谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。