{"id":13711781,"url":"https://github.com/oscarotero/middleland","last_synced_at":"2025-04-05T10:08:32.136Z","repository":{"id":55936204,"uuid":"67400237","full_name":"oscarotero/middleland","owner":"oscarotero","description":"Simple PSR-15 middleware dispatcher","archived":false,"fork":false,"pushed_at":"2025-01-06T20:15:54.000Z","size":64,"stargazers_count":36,"open_issues_count":0,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T09:09:35.814Z","etag":null,"topics":["dispatcher","http","middleware","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oscarotero.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-09-05T08:00:19.000Z","updated_at":"2025-02-01T07:00:58.000Z","dependencies_parsed_at":"2022-08-15T09:50:38.303Z","dependency_job_id":null,"html_url":"https://github.com/oscarotero/middleland","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fmiddleland","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fmiddleland/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fmiddleland/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oscarotero%2Fmiddleland/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oscarotero","download_url":"https://codeload.github.com/oscarotero/middleland/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247318744,"owners_count":20919484,"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","http","middleware","psr-15"],"created_at":"2024-08-02T23:01:11.559Z","updated_at":"2025-04-05T10:08:32.104Z","avatar_url":"https://github.com/oscarotero.png","language":"PHP","funding_links":[],"categories":["Packages"],"sub_categories":["Dispatcher"],"readme":"# Middleland\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE)\n\nSimple (but powerful)\n[PSR-15](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-15-request-handlers.md)\nmiddleware dispatcher:\n\n## Requirements\n\n- PHP 7\n- A [PSR-7 Message implementation](http://www.php-fig.org/psr/psr-7/), for\n  example [laminas-diactoros](https://github.com/laminas/laminas-diactoros)\n- Optionally, a [PSR-11 container](https://github.com/php-fig/container)\n  implementation to create the middleware components on demand.\n\n## Example\n\n```php\nuse Middleland\\Dispatcher;\n\n$middleware = [\n    new Middleware1(),\n    new Middleware2(),\n    new Middleware3(),\n\n    // A dispatcher can be used to group middlewares\n    new Dispatcher([\n        new Middleware4(),\n        new Middleware5(),\n    ]),\n\n    // You can use closures\n    function ($request, $next) {\n        $response = $next-\u003ehandle($request);\n        return $response-\u003ewithHeader('X-Foo', 'Bar');\n    },\n\n    // Or use a string to create the middleware on demand using a PSR-11 container\n    'middleware6'\n\n    // USE AN ARRAY TO ADD CONDITIONS:\n\n    // This middleware is processed only in paths starting by \"/admin\"\n    ['/admin', new MiddlewareAdmin()],\n\n    // This is processed in DEV\n    [ENV === 'DEV', new MiddlewareAdmin()],\n\n    // Use callables to create other conditions\n    [\n        function ($request) {\n            return $request-\u003egetUri()-\u003egetScheme() === 'https';\n        },\n        new MiddlewareHttps()\n    ],\n\n    // There are some matchers included in this library to create conditions\n    [\n        new Pattern('*.png'),\n        new MiddlewareForPngFiles()\n    ],\n\n    //And use several for each middleware\n    [\n        ENV === 'DEV',\n        new Pattern('*.png'),\n        new MiddlewareForPngFilesInDev()\n    ],\n];\n\n$dispatcher = new Dispatcher($middleware, new Container());\n\n$response = $dispatcher-\u003edispatch(new Request());\n```\n\n## Matchers\n\nAs you can see in the example above, you can use an array of \"matchers\" to\nfilter the requests that receive middlewares. You can use callables, instances\nof `Middleland\\Matchers\\MatcherInterface` or booleans, but for comodity, the\nstring values are also used to create `Middleland\\Matchers\\Path` instances. The\navailable matchers are:\n\n| Name      | Description                                                                 | Example                                              |\n| --------- | --------------------------------------------------------------------------- | ---------------------------------------------------- |\n| `Path`    | Filter requests by base path. Use exclamation mark for negative matches     | `new Path('/admin')`, `new Path('!/not-admin')`      |\n| `Pattern` | Filter requests by path pattern. Use exclamation mark for negative matches  | `new Pattern('*.png')` `new Pattern('!*.jpg')`       |\n| `Accept`  | Filter requests by Accept header. Use exclamation mark for negative matches | `new Accept('text/html')` `new Accept('!image/png')` |\n\n## How to create matchers\n\nJust use a callable or an instance of the\n`Middleland\\Matchers\\MatcherInterface`. Example:\n\n```php\nuse Middleland\\Matchers\\MatcherInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\n\nclass IsAjax implements MatcherInterface\n{\n    public function __invoke(ServerRequestInterface $request): bool\n    {\n    \treturn $request-\u003egetHeaderLine('X-Requested-With') === 'xmlhttprequest';\n\t}\n}\n```\n\n---\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about recent changes.\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/oscarotero/middleland.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[link-packagist]: https://packagist.org/packages/oscarotero/middleland\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fmiddleland","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foscarotero%2Fmiddleland","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foscarotero%2Fmiddleland/lists"}