{"id":18285517,"url":"https://github.com/bemit/middleware-utils","last_synced_at":"2025-09-11T14:13:07.886Z","repository":{"id":57032773,"uuid":"231956780","full_name":"bemit/middleware-utils","owner":"bemit","description":"Middleware Utils with PHP-DI, universal for PSR-7/17, 15 Middlewares","archived":false,"fork":false,"pushed_at":"2020-01-05T20:43:28.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-09T06:16:52.742Z","etag":null,"topics":[],"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/bemit.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":"2020-01-05T18:09:43.000Z","updated_at":"2020-01-05T20:31:25.000Z","dependencies_parsed_at":"2022-08-24T05:40:44.742Z","dependency_job_id":null,"html_url":"https://github.com/bemit/middleware-utils","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/bemit/middleware-utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Fmiddleware-utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Fmiddleware-utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Fmiddleware-utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Fmiddleware-utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bemit","download_url":"https://codeload.github.com/bemit/middleware-utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bemit%2Fmiddleware-utils/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260148282,"owners_count":22965910,"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":"2024-11-05T13:16:55.556Z","updated_at":"2025-06-16T11:05:18.723Z","avatar_url":"https://github.com/bemit.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Middleware Utils\n\nUtilities and middlewares for PSR-7/17, PSR-15 middleware systems for usage with PHP-DI.\n\n## HasResponseFactory\n\nAdds a response factory to a middleware with various factory methods, relies on DI-injection of `Psr\\Http\\Message\\ResponseFactoryInterface` and `Psr\\Http\\Message\\StreamFactoryInterface`.\n\n```php\n\u003c?php\nuse Orbiter\\MiddlewareUtils\\HasResponseFactory;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Server\\MiddlewareInterface;\nuse Psr\\Http\\Server\\RequestHandlerInterface;\n\nclass SampleMiddleware implements MiddlewareInterface {\n    use HasResponseFactory;\n\n    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {\n        if($request-\u003ehasHeader('error') === 'some-strange-bug') {\n            // Simple Status Response\n            return $this-\u003ecreate500();\n            // Same like `create500`\n            return $this-\u003ecreateResponse(500, 'Internal Server Error');\n        }\n\n        if($request-\u003ehasHeader('error') === 'client-error') {\n            // Creating: 400 Bad Request with JSON Body\n            return $this-\u003erespondJson($this-\u003ecreate400(), ['message' =\u003e 'client-error-msg']);\n        }\n        \n        return $handler-\u003ehandle($request);\n    }\n}\n```\n\n- `createResponse(int $code = 200, string $reasonPhrase = ''): ResponseInterface` - create any empty response\n- `respondJson(ResponseInterface $response, $data): ResponseInterface` - uses the response and adds header and data\n- `create400(): ResponseInterface` - Bad Request\n- `create401(): ResponseInterface` - Unauthorized\n- `create402(): ResponseInterface` - Payment Required\n- `create403(): ResponseInterface` - Forbidden\n- `create404(): ResponseInterface` - Not Found\n- `create405(): ResponseInterface` - Method Not Allowed\n- `create409(): ResponseInterface` - Conflict\n- `create410(): ResponseInterface` - Gone\n- `create413(): ResponseInterface` - Payload Too Large\n- `create415(): ResponseInterface` - Unsupported Media Type\n- `create440(): ResponseInterface` - Login Time-out\n- `create500(): ResponseInterface` - Internal Server Error\n- `create501(): ResponseInterface` - Not Implemented\n- `create502(): ResponseInterface` - Bad Gateway\n- `create503(): ResponseInterface` - Service Unavailable\n\n## ApiError\n\nUnified error body:\n\n```php\n\u003c?php\nuse Psr\\Http\\Message\\ResponseInterface;\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Server\\MiddlewareInterface;\nuse Psr\\Http\\Server\\RequestHandlerInterface;\n\nuse Orbiter\\MiddlewareUtils\\HasResponseFactory;\nuse Orbiter\\MiddlewareUtils\\ApiError;\n\nclass SampleMiddleware implements MiddlewareInterface {\n    use HasResponseFactory;\n\n    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface {\n        if($request-\u003ehasHeader('error') === 'user-not-found') {\n            // Creating: 400 Bad Request with ApiError as JSON Body\n            return $this-\u003erespondJson($this-\u003ecreate404(), new ApiError('User Not Found'));\n        }\n        \n        return $handler-\u003ehandle($request);\n    }\n}\n```\n\n## CorsMiddleware\n\nDead-Simple CORS middleware, support multiple origins.\n\nCreate the middleware with any DI-factory, add to any middleware pipe.\n\n```php\n\u003c?php\nuse Orbiter\\MiddlewareUtils\\CorsMiddleware;\n\n$pipe = new RespondPipe();\n\n/**\n * @var DI\\FactoryInterface $factory\n */\n$pipe-\u003ewith(\n    $factory-\u003emake(CorsMiddleware::class, [\n        'origins_allowed' =\u003e [\n            'http://localhost:3000',\n            'https://admin.example.org',\n        ],\n        'headers_allowed' =\u003e [\n            'Content-Type',\n            'Accept',\n            'AUTHORIZATION',\n            'X-Requested-With',\n            'X_AUTH_TOKEN',\n            'X_AUTH_SIGNATURE',\n            'X_API_OPTION',\n            'remember-me',\n        ],\n        'headers_expose' =\u003e [\n            'Content-Range',\n        ],\n        'max_age' =\u003e 2,\n    ])\n);\n```\n\n### CORSMiddleware Zend-Expressive\n\n```php\n\u003c?php\nuse Zend\\Stratigility\\MiddlewarePipe;\n\nuse DI\\FactoryInterface;\nuse Orbiter\\MiddlewareUtils\\CorsMiddleware;\n\nclass PipelineFactory\n{\n    public function __invoke(FactoryInterface $factory)\n    {\n        $pipeline = new MiddlewarePipe();\n\n        // create CORS Middleware with PHP-DI\n        $pipeline-\u003epipe($factory-\u003emake(CorsMiddleware::class, [\n            'origins_allowed' =\u003e ['http://localhost:3000'],\n            'headers_allowed' =\u003e [\n                'Content-Type',\n                'Accept',\n                'AUTHORIZATION',\n                'X-Requested-With',\n                'X_AUTH_TOKEN',\n                'X_AUTH_SIGNATURE',\n                'X_API_OPTION',\n                'remember-me',\n            ],\n            'headers_expose' =\u003e [\n                'Content-Range',\n            ],\n            'max_age' =\u003e 2,\n        ]));\n\n        $pipeline-\u003epipe(OtherMiddleware::class);\n        // ...\n\n        return $pipeline;\n    }\n}\n```\n\n## License\n\nThis project is free software distributed under the **MIT License**.\n\nSee: [LICENSE](LICENSE).\n\n### Contributors\n\nBy committing your code to the code repository you agree to release the code under the MIT License attached to the repository.\n\n***\n\nMaintained by [Michael Becker](https://mlbr.xyz)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbemit%2Fmiddleware-utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbemit%2Fmiddleware-utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbemit%2Fmiddleware-utils/lists"}