{"id":19039607,"url":"https://github.com/windwalker-io/middleware","last_synced_at":"2026-03-14T15:48:16.472Z","repository":{"id":57081021,"uuid":"16939016","full_name":"windwalker-io/middleware","owner":"windwalker-io","description":"[READ ONLY] PSR-7 Middleware base classes.","archived":false,"fork":false,"pushed_at":"2021-02-18T07:02:32.000Z","size":69,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-02T06:44:53.203Z","etag":null,"topics":["middleware","psr7-http","psr7-middleware"],"latest_commit_sha":null,"homepage":"https://github.com/ventoviro/windwalker","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/windwalker-io.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}},"created_at":"2014-02-18T06:13:09.000Z","updated_at":"2024-12-16T13:47:40.000Z","dependencies_parsed_at":"2022-08-24T14:42:48.241Z","dependency_job_id":null,"html_url":"https://github.com/windwalker-io/middleware","commit_stats":null,"previous_names":["ventoviro/windwalker-middleware"],"tags_count":83,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Fmiddleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Fmiddleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Fmiddleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Fmiddleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/windwalker-io","download_url":"https://codeload.github.com/windwalker-io/middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240100153,"owners_count":19747639,"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":["middleware","psr7-http","psr7-middleware"],"created_at":"2024-11-08T22:17:53.803Z","updated_at":"2025-12-18T09:05:37.348Z","avatar_url":"https://github.com/windwalker-io.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Windwalker Middleware\n\nWindwalker Middleware is a simple \u0026 elegant PHP Middleware library help you integrating middleware pattern in your project.\n\n## Installation via Composer\n\nAdd this to the require block in your `composer.json`.\n\n``` json\n{\n    \"require\": {\n        \"windwalker/middleware\": \"~3.0\"\n    }\n}\n```\n\n## Getting Started\n\n### Basic Example\n\nThis is a simple way using middleware to wrap your logic.\n\n``` php\nuse Windwalker\\Middleware\\CallbackMiddleware;\nuse Windwalker\\Middleware\\AbstractMiddleware;\n\nclass TestA extends AbstractMiddleware\n{\n\t/**\n\t * call\n\t *\n\t * @return  mixed\n\t */\n\tpublic function call()\n\t{\n\t\techo \"\u003e\u003e\u003e AAAA\\n\";\n\n\t\t$this-\u003enext-\u003ecall();\n\n\t\techo \"\u003c\u003c\u003c AAAA\\n\";\n\t}\n}\n\nclass TestB extends AbstractMiddleware\n{\n\t/**\n\t * call\n\t *\n\t * @return  mixed\n\t */\n\tpublic function call()\n\t{\n\t\techo \"\u003e\u003e\u003e BBBB\\n\";\n\n\t\t$this-\u003enext-\u003ecall();\n\n\t\techo \"\u003c\u003c\u003c BBBB\\n\";\n\t}\n}\n\n$a = new TestA;\n\n$a-\u003esetNext(new TestB);\n\n$a-\u003ecall();\n```\n\nThe result should be:\n\n```\n\u003e\u003e\u003e AAAA\n\u003e\u003e\u003e BBBB\n\u003c\u003c\u003c BBBB\n\u003c\u003c\u003c AAAA\n```\n\n### Callback Middleware\n\nIf you don't want to create a class, you want to set a middleware in runtime, using `CallbackMiddleware`\n\n``` php\n$a = new TestA;\n$b = new TestB;\n\n$a-\u003esetNext($b);\n$b-\u003esetNext(new CallbackMiddleware(\n\tfunction($next)\n\t{\n\t\techo \"\u003e\u003e\u003eCCCC\\n\";\n\t\techo \"\u003c\u003c\u003cCCCC\\n\";\n\t}\n));\n\n$a-\u003ecall();\n```\n\nThe result should be:\n\n```\n\u003e\u003e\u003e AAAA\n\u003e\u003e\u003e BBBB\n\u003e\u003e\u003e CCCC\n\u003c\u003c\u003c CCCC\n\u003c\u003c\u003c BBBB\n\u003c\u003c\u003c AAAA\n```\n\nThe `CallbackMiddleware` support second argument as next in constructor:\n\n``` php\n$ware = new CallbackMiddleware(\n\tfunction($next)\n\t{\n\t\techo \"\u003e\u003e\u003eCCCC\\n\";\n\n\t\t$next-\u003ecall();\n\n\t\techo \"\u003c\u003c\u003cCCCC\\n\";\n\t},\n\tnew NextMiddleware\n)\n```\n\n## End The Chaining\n\nIf a middleware call next, we have to make sure there are a next middleware exists, or we will return error.\n\n``` php\nclass TestB extends Middleware\n{\n\t/**\n\t * call\n\t *\n\t * @return  mixed\n\t */\n\tpublic function call()\n\t{\n\t\techo \"\u003e\u003e\u003e BBBB\\n\";\n\n\t\t$this-\u003enext-\u003ecall();\n\n\t\techo \"\u003c\u003c\u003c BBBB\\n\";\n\t}\n}\n\n$b = new TestB;\n\n$b-\u003ecall();\n\n// Error, next not exists.\n```\n\nBut yes we can set a blackhole middleware in the last element, that will do nothing when previous class call it, using `EndMiddleware`:\n\n``` php\n$b = new TestB;\n\n$b-\u003esetNext(new EndMiddleware);\n\n$b-\u003ecall();\n```\n\nThe result still like below:\n\n```\n\u003e\u003e\u003e BBBB\n\u003c\u003c\u003c BBBB\n```\n\n## Chaining Builder\n\nWe can using `ChainBuilder` to chaining multiple middlewares.\n\n``` php\nuse Windwalker\\Middleware\\Chain\\ChainBuilder;\n\n$chain = new ChainBuilder;\n\n$chain\n    -\u003eadd('TestA')\n    -\u003eadd(new TestB)\n    -\u003eadd(function($next)\n    {\n        echo \"\u003e\u003e\u003eCCCC\\n\";\n        echo \"\u003c\u003c\u003cCCCC\\n\";\n    });\n\n$chain-\u003ecall();\n```\n\nThe result still:\n\n```\n\u003e\u003e\u003e AAAA\n\u003e\u003e\u003e BBBB\n\u003e\u003e\u003e CCCC\n\u003c\u003c\u003c CCCC\n\u003c\u003c\u003c BBBB\n\u003c\u003c\u003c AAAA\n```\n\n## Psr7 Middleware\n\n``` php\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Windwalker\\Middleware\\Chain\\Psr7ChainBuilder;\nuse Windwalker\\Middleware\\Psr7Middleware;\n\nclass MyPsr7Middleware implements \\Windwalker\\Middleware\\Psr7InvokableInterface\n{\n\tpublic function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next = null)\n\t{\n\t\t// Do something\n\n\t\t$result = $next($request, $response);\n\n\t\t// Do something\n\n\t\treturn $result;\n\t}\n}\n\n$mid = new Psr7Middleware(function (ServerRequestInterface $request, ResponseInterface $response, $next = null)\n{\n\t// Do something\n});\n\n$chain = new Psr7ChainBuilder;\n$chain-\u003eadd(new MyPsr7Middleware)\n\t-\u003eadd($mid);\n\n$chain-\u003eexecute(new ServerRequest, new Response);\n```\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindwalker-io%2Fmiddleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwindwalker-io%2Fmiddleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindwalker-io%2Fmiddleware/lists"}