{"id":13395604,"url":"https://github.com/mindplay-dk/middleman","last_synced_at":"2025-04-05T16:10:15.964Z","repository":{"id":48410914,"uuid":"45645249","full_name":"mindplay-dk/middleman","owner":"mindplay-dk","description":"Dead simple PSR-15 / PSR-7 middleware dispatcher","archived":false,"fork":false,"pushed_at":"2024-08-31T09:55:32.000Z","size":98,"stargazers_count":90,"open_issues_count":1,"forks_count":10,"subscribers_count":8,"default_branch":"master","last_synced_at":"2024-10-13T13:09:00.473Z","etag":null,"topics":["dispatcher","middleware","php7","psr-15"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mindplay-dk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-05T23:08:53.000Z","updated_at":"2024-08-31T09:55:35.000Z","dependencies_parsed_at":"2024-05-07T15:30:57.854Z","dependency_job_id":"b4006dfc-57b4-4883-bbe6-6ae9666ee0d3","html_url":"https://github.com/mindplay-dk/middleman","commit_stats":{"total_commits":56,"total_committers":6,"mean_commits":9.333333333333334,"dds":0.3214285714285714,"last_synced_commit":"6bf522aeb6c8bbf70ade466c2d2f08ea57695bb2"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mindplay-dk%2Fmiddleman","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mindplay-dk%2Fmiddleman/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mindplay-dk%2Fmiddleman/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mindplay-dk%2Fmiddleman/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mindplay-dk","download_url":"https://codeload.github.com/mindplay-dk/middleman/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247361695,"owners_count":20926643,"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":["dispatcher","middleware","php7","psr-15"],"created_at":"2024-07-30T18:00:25.732Z","updated_at":"2025-04-05T16:10:15.940Z","avatar_url":"https://github.com/mindplay-dk.png","language":"PHP","readme":"# mindplay/middleman\n\n[![PHP Version](https://img.shields.io/badge/php-7.3_--_8.4%2B-blue.svg)](https://packagist.org/packages/mindplay/middleman)\n[![CI](https://github.com/mindplay-dk/middleman/actions/workflows/ci.yml/badge.svg)](https://github.com/mindplay-dk/middleman/actions/workflows/ci.yml)\n[![Code Coverage](https://scrutinizer-ci.com/g/mindplay-dk/middleman/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/mindplay-dk/middleman/?branch=master)\n\nDead simple PSR-15 / PSR-7 [middleware](#middleware) dispatcher.\n\nProvides (optional) integration with a [variety](https://github.com/container-interop/container-interop#compatible-projects)\nof dependency injection containers compatible with [PSR-11](https://www.php-fig.org/psr/psr-11/).\n\nTo upgrade between major releases, please see [UPGRADING.md](UPGRADING.md).\n\nA growing catalog of PSR-15 middleware-components is available from [github.com/middlewares](https://github.com/middlewares).\n\n## Usage\n\nThe constructor expects an array of PSR-15 `MiddlewareInterface` instances:\n\n```php\nuse mindplay\\middleman\\Dispatcher;\n\n$dispatcher = new Dispatcher([\n    new ErrorHandlerMiddleware(...)\n    new RouterMiddleware(...),\n    new NotFoundMiddleware(...),\n]);\n```\n\nThe `Dispatcher` implements the PSR-15 `RequestHandlerInterface`. This package *only* provides the\nmiddleware stack - to run a PSR-15 handler, for example in your `index.php` file, you need\na [PSR-15 host](https://packagist.org/packages/mindplay/sapi-host) or a similar facility.\n\nNote that the middleware-stack in the `Dispatcher` is immutable - if you need a stack you can manipulate, `array`, `ArrayObject`, `SplStack` etc. are all fine choices.\n\n### Anonymous Functions as Middleware\n\nYou can implement simple middleware \"in place\" by using anonymous functions in a middleware-stack, using a PSR-7/17 implementation such as [`nyholm/psr7`](https://packagist.org/packages/nyholm/psr7):\n\n```php\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse mindplay\\middleman\\Dispatcher;\nuse Nyholm\\Psr7\\Factory\\Psr17Factory;\n\n$factory = new Psr17Factory();\n\n$dispatcher = new Dispatcher([\n    function (ServerRequestInterface $request, callable $next) {\n        return $next($request); // delegate control to next middleware\n    },\n    function (ServerRequestInterface $request) use ($factory) {\n        return $factory-\u003ecreateResponse(200)-\u003ewithBody(...); // abort middleware stack and return the response\n    },\n    // ...\n]);\n\n$response = $dispatcher-\u003ehandle($request);\n```\n\n### Dependency Injection via the Resolver Function\n\nIf you want to integrate with an [IOC container](https://github.com/container-interop/container-interop#compatible-projects)\nyou can use the `ContainerResolver` - a \"resolver\" is a callable which gets applied to every element in your middleware stack,\nwith a signature like:\n\n    function (string $name) : MiddlewareInterface\n\nThe following example obtains middleware components on-the-fly from a DI container:\n\n```php\n$dispatcher = new Dispatcher(\n    [\n        RouterMiddleware::class,\n        ErrorMiddleware::class,\n    ],\n    new ContainerResolver($container)\n);\n```\n\nIf you want the `Dispatcher` to integrate deeply with your framework of choice, you can implement this as a class\nimplementing the magic `__invoke()` function (as `ContainerResolver` does) - or \"in place\", as an anonymous function\nwith a matching signature.\n\nIf you want to understand precisely how this component works, the whole thing is [just one class\nwith a few lines of code](src/Dispatcher.php) - if you're going to base your next\nproject on middleware, you can (and should) understand the whole mechanism.\n\n\u003ca name=\"middleware\"\u003e\u003c/a\u003e\n## Middleware?\n\nMiddleware is a powerful, yet simple control facility.\n\nIf you're new to the concept of middleware, the following section will provide a basic overview.\n\nIn a nutshell, a middleware component is a function (or [MiddlewareInterface](src/MiddlewareInterface.php) instance)\nthat takes an incoming (PSR-7) `RequestInterface` object, and returns a `ResponseInterface` object.\n\nIt does this in one of three ways: by *assuming*, *delegating*, or *sharing* responsibility\nfor the creation of a response object.\n\n#### 1. Assuming Responsibility\n\nA middleware component *assumes* responsibility by creating and returning a response object,\nrather than delegating to the next middleware on the stack:\n\n```php\nuse Zend\\Diactoros\\Response;\n\nfunction ($request, $next) {\n    return (new Response())-\u003ewithBody(...); // next middleware won't be run\n}\n```\n\nMiddleware near the top of the stack has the power to completely bypass middleware\nfurther down the stack.\n\n#### 2. Delegating Responsibility\n\nBy calling `$next`, middleware near the top of the stack may choose to fully delegate the\nresponsibility for the creation of a response to other middleware components\nfurther down the stack:\n\n```php\nfunction ($request, $next) {\n    if ($request-\u003egetMethod() !== 'POST') {\n        return $next($request); // run the next middleware\n    } else {\n        // ...\n    }\n}\n```\n\nNote that exhausting the middleware stack will result in an exception - it's assumed that\nthe last middleware component on the stack always produces a response of some sort, typically\na \"404 not found\" error page.\n\n#### 3. Sharing Responsibility\n\nMiddleware near the top of the stack may choose to delegate responsibility for the creation of\nthe response to middleware further down the stack, and then make additional changes to\nthe returned response before returning it:\n\n```php\nfunction ($request, $next) {\n    $result = $next($request); // run the next middleware\n\n    return $result-\u003ewithHeader(...); // then modify it's response\n}\n```\n\nThe middleware component at the top of the stack ultimately has the most control, as it may\noverride any properties of the response object before returning.\n","funding_links":[],"categories":["PHP","Packages","Middleware dispatcher"],"sub_categories":["Dispatcher"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmindplay-dk%2Fmiddleman","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmindplay-dk%2Fmiddleman","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmindplay-dk%2Fmiddleman/lists"}