{"id":13711812,"url":"https://github.com/noglitchyo/middleware-collection-request-handler","last_synced_at":"2026-01-16T00:33:21.466Z","repository":{"id":57028059,"uuid":"193341489","full_name":"noglitchyo/middleware-collection-request-handler","owner":"noglitchyo","description":"  Lightweight \u0026 dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.","archived":false,"fork":false,"pushed_at":"2019-07-15T14:35:25.000Z","size":47,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-08T23:50:00.914Z","etag":null,"topics":["adr","dispatcher","http","middleware","middleware-collection","middleware-dispatchers","psr-15","psr-7","request-handler"],"latest_commit_sha":null,"homepage":"","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/noglitchyo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-23T11:46:46.000Z","updated_at":"2024-04-30T11:15:10.000Z","dependencies_parsed_at":"2022-08-23T16:20:39.506Z","dependency_job_id":null,"html_url":"https://github.com/noglitchyo/middleware-collection-request-handler","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/noglitchyo/middleware-collection-request-handler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noglitchyo%2Fmiddleware-collection-request-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noglitchyo%2Fmiddleware-collection-request-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noglitchyo%2Fmiddleware-collection-request-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noglitchyo%2Fmiddleware-collection-request-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/noglitchyo","download_url":"https://codeload.github.com/noglitchyo/middleware-collection-request-handler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/noglitchyo%2Fmiddleware-collection-request-handler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28421229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T13:30:50.153Z","status":"ssl_error","status_checked_at":"2026-01-14T13:29:08.907Z","response_time":107,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["adr","dispatcher","http","middleware","middleware-collection","middleware-dispatchers","psr-15","psr-7","request-handler"],"created_at":"2024-08-02T23:01:11.882Z","updated_at":"2026-01-16T00:33:21.450Z","avatar_url":"https://github.com/noglitchyo.png","language":"PHP","readme":"# middleware-collection-request-handler\n\nLightweight \u0026 dead simple PSR-15 Server Request Handler implementation to process a collection of middlewares.\n\n![PHP from Packagist](https://img.shields.io/packagist/php-v/noglitchyo/middleware-collection-request-handler.svg)\n[![Build Status](https://travis-ci.org/noglitchyo/middleware-collection-request-handler.svg?branch=master)](https://travis-ci.org/noglitchyo/middleware-collection-request-handler)\n[![codecov](https://codecov.io/gh/noglitchyo/middleware-collection-request-handler/branch/master/graph/badge.svg)](https://codecov.io/gh/noglitchyo/middleware-collection-request-handler)\n![Scrutinizer code quality (GitHub/Bitbucket)](https://img.shields.io/scrutinizer/quality/g/noglitchyo/middleware-collection-request-handler.svg)\n![Packagist](https://img.shields.io/packagist/l/noglitchyo/middleware-collection-request-handler.svg)\n\n### Description\n\nPSR-15 request handler implementing both [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php) and [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php)\nable to manage a collection of middlewares implementing the [MiddlewareInterface](https://github.com/php-fig/http-server-middleware/blob/master/src/MiddlewareInterface.php).\n\nIt can be used either as a RequestHandler or as a Middleware to fit into any implementation.\n\nComes with a set of middleware collections using different strategy (LIFO, FIFO...) on how the middlewares from the collection are provided to the RequestHandler, and also provides a simple MiddlewareCollectionInterface to implement in a glimpse your own strategy.\n\n### Goals\n\n- Simplicity\n- Interoperability\n\n### Getting started\n\n#### Requirements\n\n- PHP \u003e= 7.3\n\n#### Installation\n\n`composer require noglitchyo/middleware-collection-request-handler`\n\n#### Run\n\nCreate a new instance of the request handler class which can be use as a request handler or as a middleware.\n\n##### From the constructor \n\n`RequestHandler::__construct(MiddlewareCollectionInterface $middlewareCollection, RequestHandlerInterface $defaultRequestHandler = null)`\n\n- ***`$middlewareCollection`*** : [MiddlewareCollectionInterface](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/MiddlewareCollectionInterface.php)\n\n    Contains the middlewares and defines the strategy used to store the middlewares and to retrieve the next middleware.\n    Some implementations with common strategies are provided: [stack (LIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplStackMiddlewareCollection.php), [queue (FIFO)](https://github.com/noglitchyo/middleware-collection-request-handler/blob/master/src/Collection/SplQueueMiddlewareCollection.php).\n\n- ***`$defaultRequestHandler = null`*** : [RequestHandlerInterface](https://github.com/php-fig/http-server-handler/blob/master/src/RequestHandlerInterface.php)\n\n    Provides a default response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php) if none of the middlewares in the collection was able to create one.\n\n    Some examples of what could be a \"default request handler\":\n    - with the [ADR pattern](https://en.wikipedia.org/wiki/Action%E2%80%93domain%E2%80%93responder), the default request handler might be your action class.*\n    - with the [MVC pattern](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller), the default request handler might be the action method of your controller.\n\n##### From the factory method\n\n`RequestHandler::fromCallable(callable $callable, MiddlewareCollectionInterface $middlewareCollection)`\n \nIt creates a RequestHandler instance by wrapping the given `callable` inside an anonymous instance of RequestHandlerInterface.\nThe callable is the $defaultRequestHandler. **It MUST returns a response implementing [ResponseInterface](https://github.com/php-fig/http-message/blob/master/src/ResponseInterface.php).**\n\n##### Example\n\nBelow, this is how simple it is to get the middleware handler running:\n\n```php\n\u003c?php\nuse NoGlitchYo\\MiddlewareCollectionRequestHandler\\RequestHandler;\nuse NoGlitchYo\\MiddlewareCollectionRequestHandler\\Collection\\SplStackMiddlewareCollection;\nuse Psr\\Http\\Server\\MiddlewareInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Server\\RequestHandlerInterface;\nuse Psr\\Http\\Message\\ResponseInterface;\n\n// Instantiate a collection of middlewares with an anonymous middleware class.\n// In this example, we are using a \"stack\" implementation of the collection.\n$middlewareCollection = new SplStackMiddlewareCollection([\n    new class implements MiddlewareInterface{\n        public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface{\n            return $handler-\u003ehandle($request);\n        }\n    }\n]);\n\n// Let's add one more middleware in the collection (this time, from a callable)\n$middlewareCollection-\u003eaddFromCallable(function(ServerRequestInterface $request, RequestHandlerInterface $handler){\n    return $handler-\u003ehandle($request);\n});\n\n// Instantiate a new request handler with a default handler and the middleware collection.\n$requestHandler = RequestHandler::fromCallable(\n    function (ServerRequestInterface $serverRequest){\n        return new /* instance of ResponseInterface */;\n    }, \n    $middlewareCollection\n);\n\n// As a RequestHandler:\n// Pass the request to the request handler which will dispatch the request to the middlewares.\n$response = $requestHandler-\u003ehandle(/* ServerRequestInterface */); \n\n```\n\n#### Create a custom MiddlewareCollectionInterface implementation\n\nIt is easy to create a new MiddlewareCollectionInterface implementation if needed. The interface requires only 3 methods:\n```php\n\u003c?php\ninterface MiddlewareCollectionInterface\n{\n    /**\n     * Must return true if there is no middleware in the collection to process.\n     * @return bool\n     */\n    public function isEmpty(): bool;\n\n    /**\n     * Must return the next middleware to process in the collection.\n     * Depending on the implemented strategy, the middleware MAY not be removed from the collection.\n     * @return MiddlewareInterface\n     */\n    public function next(): MiddlewareInterface;\n\n    /**\n     * Add a middleware instance of MiddlewareInterface to the collection.\n     *\n     * @param MiddlewareInterface $middleware\n     *\n     * @return MiddlewareCollectionInterface\n     */\n    public function add(MiddlewareInterface $middleware): MiddlewareCollectionInterface;\n}\n```\n\n#### Tests\n\nWould like to the run the test suite? Go ahead:\n\n`composer test`\n\n### References\n\nhttps://www.php-fig.org/psr/psr-15/\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details.\n","funding_links":[],"categories":["Packages"],"sub_categories":["Dispatcher"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoglitchyo%2Fmiddleware-collection-request-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnoglitchyo%2Fmiddleware-collection-request-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnoglitchyo%2Fmiddleware-collection-request-handler/lists"}