thecodingmachine/middleware-list-universal-module
Composer 安装命令:
composer require thecodingmachine/middleware-list-universal-module
包简介
Cross-framework module providing a service containing a list of middlewares
README 文档
README
This package simply providers an http-interop middleware list to any container-interop compatible framework/container. The middleware list is empty. Any package can come and add a middleware to the list.
Installation
This package is typically used by packages providing middlewares.
composer require thecodingmachine/middleware-list-universal-module
If your container supports autodiscovery by thecodingmachine/discovery, there is nothing more to do.
Otherwise, you need to register the TheCodingMachine\MiddlewareListServiceProvider into your container.
Refer to your framework or container's documentation to learn how to register service providers.
Usage
The middleware queue is registered under the key MiddlewareListServiceProvider::MIDDLEWARES_QUEUE. This is a \SPLPriorityQueue.
Depending on the middleware you are registering, you generally have a fairly good idea of the order it should run compared to other middlewares.
Let's split the middlewares in 4 families:
- Utility middlewares: those are typically handled at the beginning of a request. They are used to modify/enrich the response and pass it to other middlewares. In this category, you would put middlewares that compute the response time, middlewares that add geolocation information, middlewares that manage sessions, etc...
- Routers: those are middlewares that are typically used to handle a request and return a response. They respond on specific routes or pass the request along to the next router if they don't know that route.
- Page not found routers: those are middlewares in charge of answering a 404 answer if no middleware has handled the request. This is the last "classical" middleware of the queue.
- Error handling middlewares: Finally, at the very end of the queue, you will find the list of middlewares that handle errors and exceptions. They are in charge of logging or displaying error messages.
Based on those 4 families, the MiddlewareListServiceProvider provides a SPL Priority Queue that one can use to register any middleware at the right point in the queue.
The service provider defines 12 constants you can use to insert a middleware at a given point:
MiddlewareOrder::EXCEPTION_EARLY(3050)MiddlewareOrder::EXCEPTION(3000)MiddlewareOrder::EXCEPTION_LATE(2950)MiddlewareOrder::UTILITY_EARLY(2050)MiddlewareOrder::UTILITY(2000)MiddlewareOrder::UTILITY_LATE(1950)MiddlewareOrder::ROUTER_EARLY(1050)MiddlewareOrder::ROUTER(1000)MiddlewareOrder::ROUTER_LATE(950)MiddlewareOrder::PAGE_NOT_FOUND_EARLY(50)MiddlewareOrder::PAGE_NOT_FOUND(0)MiddlewareOrder::PAGE_NOT_FOUND_LATE(-50)
Each "family" has 3 variants: EARLY, NORMAL and LATE, so you can add more fine grained tuning if you want a utility to be triggered before another one, etc...
So if you want to register a middleware, you would typically write:
$middlewareQueue = $container->get(MiddlewareListServiceProvider::MIDDLEWARES_QUEUE); /* @var $middlewareQueue \SplPriorityQueue */ $middlewareQueue->insert($myMiddleware, MiddlewareOrder::UTILITY);
thecodingmachine/middleware-list-universal-module 适用场景与选型建议
thecodingmachine/middleware-list-universal-module 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 183.52k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2016 年 03 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 thecodingmachine/middleware-list-universal-module 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 thecodingmachine/middleware-list-universal-module 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 183.52k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 16
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-03-02