{"id":27158886,"url":"https://github.com/activecollab/middlewarestack","last_synced_at":"2026-01-03T14:20:50.371Z","repository":{"id":56940246,"uuid":"72761859","full_name":"activecollab/middlewarestack","owner":"activecollab","description":"Simple middleware stack","archived":false,"fork":false,"pushed_at":"2016-11-30T22:34:03.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T13:03:28.940Z","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/activecollab.png","metadata":{"files":{"readme":"README.md","changelog":null,"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-11-03T15:55:57.000Z","updated_at":"2016-11-03T15:58:13.000Z","dependencies_parsed_at":"2022-08-21T06:20:54.702Z","dependency_job_id":null,"html_url":"https://github.com/activecollab/middlewarestack","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/activecollab%2Fmiddlewarestack","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Fmiddlewarestack/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Fmiddlewarestack/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/activecollab%2Fmiddlewarestack/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/activecollab","download_url":"https://codeload.github.com/activecollab/middlewarestack/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247941718,"owners_count":21022035,"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":"2025-04-08T22:39:18.021Z","updated_at":"2026-01-03T14:20:50.305Z","avatar_url":"https://github.com/activecollab.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Middleware Stack\n\n[![Build Status](https://travis-ci.org/activecollab/middlewarestack.svg?branch=master)](https://travis-ci.org/activecollab/middlewarestack)\n\nThis package lets you build a stack of middlewares and run requests through them to their coresponding responses. Stack is Last In First Out stack (LIFO), meaning that middlewares that are added later are considered to be \"outter\" middlewares, and they are executed first.  \n\nExample:\n\n```php\n$stack = new MiddlewareStack();\n\n$stack-\u003eaddMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {\n   // Route request to the contraoller, execute action, and encode action result to response\n\n   if ($next) {\n       $response = $next($request, $response);\n   }\n   \n   return $response;\n});\n\n$stack-\u003eaddMiddleware(function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {\n   if (!user_is_authenticated($request)) {\n       return $response-\u003ewithStatus(403); // Break here if user is not authenticated\n   }\n\n   if ($next) {\n       $response = $next($request, $response);\n   }\n   \n   return $response;\n});\n\n$request = new ServerRequest();\n$response = $stack-\u003eprocess($request, new Response());\n```\n\nThis example shows a simple authorization check prior to request being sent further down to routing, controller, and result encoding.\n \n## Error Handling\n\nStack exposes a way to handle exceptions (which extend `\\Exception` class) and PHP errors (which are `\\Throwable`, but don't extend `\\Exception` class, available in PHP7 and up):\n\n```php\n$stack-\u003esetExceptionHandler(function (Exception $e, ServerRequestInterface $request, ResponseInterface $response) {\n    $response = $response-\u003ewithStatus(500, 'Exception: ' . $e-\u003egetMessage());\n\n    return $response;\n});\n\n$stack-\u003esetPhpErrorHandler(function (Throwable $e, ServerRequestInterface $request, ResponseInterface $response) {\n    $response = $response-\u003ewithStatus(500, 'PHP error: ' . $e-\u003egetMessage());\n\n    return $response;\n});\n```\n\n## Extension Points\n\nYou can use `MiddlewareStack` implementation as is, or you can extend it to change its behaviour. There are two extra `protected` methods that you can use to hook in your behaviour in the stack execution:\n\n1. `MiddlewareStack` is called in the middle of execution, as just another middleware. Override `__invoke` method if you need to inject extra functionlity there (like routing, with per-route middleware stack for example),\n1. `finalizeProcessing` is called prior to $response being returned by `process` method. Override if you need to do something with response prior to returning it.\n\n## History\n\nMost middleware implementations that we found in November 2016. assumed and did too much, being mini frameworks themselves - they anticipated routing, substacking etc. \n\n[Slim framework](http://www.slimframework.com) has a nice middleware stack implementation, but it was not available as a stand-alone component, something that we needed, so we decided to extract it.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factivecollab%2Fmiddlewarestack","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Factivecollab%2Fmiddlewarestack","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Factivecollab%2Fmiddlewarestack/lists"}