drift/server
Composer 安装命令:
composer require drift/server
包简介
ReactPHP based server for DriftPHP
README 文档
README
This package provides a ReactPHP based async, reactive and non-blocking server for PHP applications working on top of ReactPHP Promises and PSR standards. The server has a small kernel/application abstraction for an optimized integration for several domain implementations. Here some features.
- Handle request based on Promises
- Serve static content in a non-blocking way
- Compress both Responses and Stream data
- Work with different workers, using multiple PHP threads (and CPUs)
- Visualize the usage and check how fast and light your request handles are
- Use the PHP-Watcher (only available if the PHP-Watcher is included in your composer) to automatically update the server if you change some of your code
By default, the server will use the DriftPHP Kernel adapter, but you can change the adapter easily when starting the server (Check the adapters chapter)
Table of content
- Installation
- Start the server
- Build your adapter
- Workers
- Watcher
- Static server
- Symfony bridge
- DriftPHP resources
Installation
You can install the server by adding the dependency in your composer.json
file
"require": { "drift/server": "^0.1" }
Start the server
This package provides an async server for DriftPHP framework based on ReactPHP packages and Promise implementation. The server is distributed with all the Symfony based kernel adapters, and can be easily extended for new Kernel modifications.
To start the server, just type this line. Your project might have a custom bin
folder, so check it.
php vendor/bin/server run 0.0.0.0:8000
You can use as well the short mode, defining only the port and assuming this
host 0.0.0.0.
php vendor/bin/server run 8000
And that's it. You will have a fully working server for your application.
Build your adapter
In order to build your adapter, the only thing you need is to create an
implementation of the interface Drift\Server\Adapter\KernelAdapter. This layer
will allow the server to start your application, handle each request, locate
your static resources and shutdown the application. The ObservableKernel will
provide as well some information about where your code is located, specifically
designed for the watcher feature.
/** * Class KernelAdapter. */ interface KernelAdapter extends ObservableKernel { /** * @param LoopInterface $loop * @param string $rootPath * @param ServerContext $serverContext * @param OutputPrinter $outputPrinter * @param MimeTypeChecker $mimeTypeChecker * @param FilesystemInterface|null $filesystem * * @return PromiseInterface<self> * * @throws KernelException */ public static function create( LoopInterface $loop, string $rootPath, ServerContext $serverContext, OutputPrinter $outputPrinter, MimeTypeChecker $mimeTypeChecker, ?FilesystemInterface $filesystem ): PromiseInterface; /** * @param ServerRequestInterface $request * * @return PromiseInterface<ResponseInterface> */ public function handle(ServerRequestInterface $request): PromiseInterface; /** * Get static folder. * * @return string|null */ public static function getStaticFolder(): ? string; /** * @return PromiseInterface */ public function shutDown(): PromiseInterface; }
When you have your adapter created, the is as easy is this to start serving from your application
php vendor/bin/server run 0.0.0.0:8000 --adapter='My\Namespace\Adapter"
Custom response output
You can internally use the x-server-message header for custom server messages.
The server will remove this server value before returning the response content.
Workers
This server creates a single worker by default. A simple PHP thread that will use one single CPUs. Luckily this server provides you a simple way of creating multiple instances listening the same port, emulating a simple balancer between N threads.
php vendor/bin/server run 0.0.0.0:8000 --workers=8
You can guess the number of physical threads your host has by using the value
-1. By default, a single worker will be used.
This feature is not designed and intended for production environments. We
encourage to use a reversed proxy or a small balancer if you need to balance
between several processes. Furthermore, this feature uses pcntl_fork, so as
the documentation explains it is not available for Windows users.
Watcher
You can use the watcher by installing the seregazhuk/php-watcher dependency
in your composer.
"require-dev": { "seregazhuk/php-watcher": "*" }
After installing the dependency, you will be able to start your server by checking code changes.
php vendor/bin/server watch 0.0.0.0:8000
This feature is for development only.
Static server
This server can serve static files as well located in your project. By default,
an adapter will provide a path where static files should be found (like DriftPHP
statics are located under public/ folder), but you can overwrite this value,
or even override it.
php vendor/bin/server watch 0.0.0.0:8000 --static-folder=/my/own/folder/ php vendor/bin/server watch 0.0.0.0:8000 --no-static-folder
You can create an alias as well if you need it. That can be useful if you want
to mask the internal path with an external one, only exposing this second one.
Both values must be separated by the symbol :, being the first part the alias,
and the second one the internal path.
php vendor/bin/server watch 0.0.0.0:8000 --static-folder=/public/:/internal/public/path
In this example, a file named app.js located under /internal/public/path/
folder will be accessible at http://localhost:8000/public/app.js. By default,
this feature is disabled.
Static server cache
You can define your static folder cache by adding a YAML file wherever you want and referencing it when starting the server.
php vendor/bin/server watch 0.0.0.0:8000 --static-cache=/my/path/static.cache.yml
In this file you can define specific headers for all your static resources (in fact, not only cache ones). You can define as well regular expressions for matching them.
/public/js/.*: Cache-Control: max-age=31536000 /public/css/app.js: Content-Type: application/javascript /public/images/logo.png: Other-Header: Other-Value
By default, this feature is disabled and no extra headers will be added into your static resources.
Important
By default, this package will not install the react/filesystem package. This
means that, if you don't install it by hand in your project, all the disk
operations will be blocking. These operations done synchronously will be much
faster and efficient, but by using large size files could slow down the entire
process.
Symfony bridge
In order to help you from migrating an application from Symfony to DriftPHP, assuming that this means that your whole domain should turn on top of Promises, including your infrastructure layer, this server is distributed with a small Symfony adapter. Use it as a tool, and never use it at production (using a ReactPHP based server in a blocking application is something not recommendable at all in terms of performance and service availability). That adapter will help your migrating from one platform to the other, as will allow this server to work with your Symfony kernel.
php vendor/bin/server watch 0.0.0.0:8000 --adapter=symfony
DriftPHP resources
Some first steps for you!
or
drift/server 适用场景与选型建议
drift/server 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.22k 次下载、GitHub Stars 达 68, 最近一次更新时间为 2019 年 11 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 drift/server 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 drift/server 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 22.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 69
- 点击次数: 6
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-11-07