antidot-fw / react-framework
Antidot React 框架
1.2.0
2021-11-28 18:24 UTC
Requires
- php: ^7.4|>=8.0
- antidot-fw/framework: ^1.0
- beberlei/assert: ^3.3
- drift/server: ^0.1.20
- psr/container: ^1.0.0
- ramsey/uuid: ^4.1
- react/http: ^1.2
Requires (Dev)
- clue/block-react: ^1.4
- infection/infection: ^0.20
- phpro/grumphp: ^1.0.0
- phpunit/phpunit: ^8.0 || ^9.0
- squizlabs/php_codesniffer: ^3.4
- symfony/var-dumper: ^5.1
- vimeo/psalm: ^4.4
Suggests
- react/filesystem: ^0.1.2
README
需求
- PHP ^7.4|^8.0
- Antidot 框架
- DriftPHP 服务器
- React Http
- React Promises
- Ramsey Uuid
描述
此包允许按照 PSR-15 中间件标准方法运行异步 PHP。
安装
安装此库的首选方法是使用 reactive-antidot-starter
项目。
composer create-project antidot-fw/reactive-antidot-starter
要在现有的 Antidot 框架项目安装上安装它,我们需要调整一些配置并替换或创建新的 index.php
文件。
composer require antidot-fw/react-framework
配置
- 禁用 LaminasRequest 处理器运行器
- 在 Antidot 框架提供者之后加载 Antidot React 配置提供者
启动项目中的示例配置
<?php // config/config.php declare(strict_types=1); use Antidot\DevTools\Container\Config\ConfigProvider as DevToolsConfigProvider; use Antidot\SymfonyConfigTranslator\Container\Config\ConfigAggregator; use Antidot\Yaml\YamlConfigProvider; use Laminas\ConfigAggregator\ArrayProvider; use Laminas\ConfigAggregator\PhpFileProvider; // To enable or disable caching, set the `ConfigAggregator::ENABLE_CACHE` boolean in // `config/autoload/local.php`. $cacheConfig = [ 'config_cache_path' => 'var/cache/config-cache.php', ]; $aggregator = new ConfigAggregator([ \WShafer\PSR11MonoLog\ConfigProvider::class, \Antidot\Event\Container\Config\ConfigProvider::class, \Antidot\Logger\Container\Config\ConfigProvider::class, \Antidot\Cli\Container\Config\ConfigProvider::class, \Antidot\Fast\Router\Container\Config\ConfigProvider::class, \Antidot\Container\Config\ConfigProvider::class, \Antidot\React\Container\Config\ConfigProvider::class, class_exists(DevToolsConfigProvider::class) ? DevToolsConfigProvider::class : fn() => [], new PhpFileProvider(realpath(__DIR__).'/services/{{,*.}prod,{,*.}local,{,*.}dev}.php'), new YamlConfigProvider(realpath(__DIR__).'/services/{{,*.}prod,{,*.}local,{,*.}dev}.yaml'), new ArrayProvider($cacheConfig), ], $cacheConfig['config_cache_path']); return $aggregator->getMergedConfig();
默认配置
<?php $config = [ 'server' => [ 'host' => '0.0.0.0', 'port' => 5555, 'buffer_size' => 4096, 'max_concurrency' => 100, 'workers' => 1, 'static_folder' => 'public' ] ]
用法
它允许在 PSR-15 和 PSR-7 中间件和请求处理器中执行承诺。
PSR-15 中间件
<?php declare(strict_types = 1); namespace App; use Antidot\React\PromiseResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class SomeMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { return new PromiseResponse( resolve($request) ->then(static fn(ServerrequestInsterface $request) => $handler->handle($request)) ); } }
PSR-7 请求处理器
<?php declare(strict_types = 1); namespace App; use Antidot\React\PromiseResponse; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; class SomeMiddleware implements RequestHandlerInterface { public function process(ServerRequestInterface $request): ResponseInterface { return resolve($request)->then( function(ServerrequestInterface $request): ResponseInterface { return new Response('Hello World!!!'); } );; } }
服务器
Antidot 框架 CLI 工具将添加两个新命令,以允许在 Drift 服务器 上运行应用程序
server:run
:运行 Drift HTTP 服务器server:watch
:监视 Drift HTTP 服务器以进行开发目的
$ bin/console
...
server
server:run Run Drift HTTP Server
server:watch Watch Drift HTTP Server for development purposes
$ bin/console server:run -h Description: Run Drift HTTP Server Usage: server:run [options] [--] [<path>] Arguments: path The server will start listening to this address [default: "0.0.0.0:5555"] Options: --static-folder[=STATIC-FOLDER] Static folder path [default: "public"] --no-static-folder Disable static folder --debug Enable debug --no-header Disable the header --no-cookies Disable cookies --no-file-uploads Disable file uploads --concurrent-requests[=CONCURRENT-REQUESTS] Limit of concurrent requests [default: 100] --request-body-buffer[=REQUEST-BODY-BUFFER] Limit of the buffer used for the Request body. In KiB. [default: 4096] --adapter[=ADAPTER] Server Adapter [default: "Antidot\React\DriftKernelAdapter"] --allowed-loop-stops[=ALLOWED-LOOP-STOPS] Number of allowed loop stops [default: 0] --workers[=WORKERS] Number of workers. Use -1 to get as many workers as physical thread available for your system. Maximum of 128 workers. Option disabled for watch command. [default: 16] -q, --quiet Do not output any message
$ bin/console server:watch -h Description: Watch Drift HTTP Server for development purposes Usage: server:watch [options] [--] [<path>] Arguments: path The server will start listening to this address [default: "0.0.0.0:5555"] Options: --static-folder[=STATIC-FOLDER] Static folder path [default: "public"] --no-static-folder Disable static folder --debug Enable debug --no-header Disable the header --no-cookies Disable cookies --no-file-uploads Disable file uploads --concurrent-requests[=CONCURRENT-REQUESTS] Limit of concurrent requests [default: 512] --request-body-buffer[=REQUEST-BODY-BUFFER] Limit of the buffer used for the Request body. In KiB. [default: 2048] --adapter[=ADAPTER] Server Adapter [default: "drift"] --allowed-loop-stops[=ALLOWED-LOOP-STOPS] Number of allowed loop stops [default: 0] --workers[=WORKERS] Number of workers. Use -1 to get as many workers as physical thread available for your system. Maximum of 128 workers. Option disabled for watch command. [default: 1] -q, --quiet Do not output any message