{"id":18929503,"url":"https://github.com/thecodingmachine/middleware-list-universal-module","last_synced_at":"2025-04-15T15:31:05.580Z","repository":{"id":57067893,"uuid":"52968570","full_name":"thecodingmachine/middleware-list-universal-module","owner":"thecodingmachine","description":"Cross-framework module providing a service containing a list of middlewares","archived":false,"fork":false,"pushed_at":"2018-06-05T13:28:24.000Z","size":9,"stargazers_count":3,"open_issues_count":0,"forks_count":3,"subscribers_count":9,"default_branch":"1.1","last_synced_at":"2025-04-11T18:59:49.074Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thecodingmachine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-02T14:21:30.000Z","updated_at":"2018-11-13T20:54:00.000Z","dependencies_parsed_at":"2022-08-24T14:54:08.378Z","dependency_job_id":null,"html_url":"https://github.com/thecodingmachine/middleware-list-universal-module","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fmiddleware-list-universal-module","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fmiddleware-list-universal-module/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fmiddleware-list-universal-module/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thecodingmachine%2Fmiddleware-list-universal-module/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thecodingmachine","download_url":"https://codeload.github.com/thecodingmachine/middleware-list-universal-module/tar.gz/refs/heads/1.1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249097869,"owners_count":21212364,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-08T11:33:06.963Z","updated_at":"2025-04-15T15:31:05.332Z","avatar_url":"https://github.com/thecodingmachine.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# http-interop middleware list universal module\n\nThis package simply providers an http-interop middleware list to any [container-interop](https://github.com/container-interop/service-provider) compatible framework/container.\nThe middleware list is **empty**. Any package can come and add a middleware to the list. \n\n## Installation\n\nThis package is typically used by packages providing middlewares.\n\n```\ncomposer require thecodingmachine/middleware-list-universal-module\n```\n\nIf your container supports autodiscovery by thecodingmachine/discovery, there is nothing more to do.\nOtherwise, you need to register the [`TheCodingMachine\\MiddlewareListServiceProvider`](src/MiddlewareListServiceProvider.php) into your container.\n\nRefer to your framework or container's documentation to learn how to register *service providers*.\n\n## Usage\n\nThe middleware queue is registered under the key `MiddlewareListServiceProvider::MIDDLEWARES_QUEUE`. This is a `\\SPLPriorityQueue`.\n\nDepending on the middleware you are registering, you generally have a fairly good idea of the order it should run compared to other middlewares.\n\nLet's split the middlewares in 4 families:\n\n- **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.\n  In this category, you would put middlewares that compute the response time, middlewares that add geolocation information, middlewares that manage sessions, etc...\n- **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.\n- **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.\n- **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.\n\nBased 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.\n\nThe service provider defines 12 constants you can use to insert a middleware at a given point:\n    \n- `MiddlewareOrder::EXCEPTION_EARLY` (3050)\n- `MiddlewareOrder::EXCEPTION` (3000)\n- `MiddlewareOrder::EXCEPTION_LATE` (2950)\n- `MiddlewareOrder::UTILITY_EARLY` (2050)\n- `MiddlewareOrder::UTILITY` (2000)\n- `MiddlewareOrder::UTILITY_LATE` (1950)\n- `MiddlewareOrder::ROUTER_EARLY` (1050)\n- `MiddlewareOrder::ROUTER` (1000)\n- `MiddlewareOrder::ROUTER_LATE` (950)\n- `MiddlewareOrder::PAGE_NOT_FOUND_EARLY` (50)\n- `MiddlewareOrder::PAGE_NOT_FOUND` (0)\n- `MiddlewareOrder::PAGE_NOT_FOUND_LATE` (-50)\n\nEach \"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...\n\nSo if you want to register a middleware, you would typically write:\n\n```php\n$middlewareQueue = $container-\u003eget(MiddlewareListServiceProvider::MIDDLEWARES_QUEUE);\n/* @var $middlewareQueue \\SplPriorityQueue */\n$middlewareQueue-\u003einsert($myMiddleware, MiddlewareOrder::UTILITY);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fmiddleware-list-universal-module","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthecodingmachine%2Fmiddleware-list-universal-module","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthecodingmachine%2Fmiddleware-list-universal-module/lists"}