{"id":15458013,"url":"https://github.com/websoftwares/middleware","last_synced_at":"2025-09-06T06:42:17.998Z","repository":{"id":31090418,"uuid":"34649501","full_name":"websoftwares/middleware","owner":"websoftwares","description":"This package lets u manage middleware for a HTTP request and response","archived":false,"fork":false,"pushed_at":"2015-05-31T08:21:25.000Z","size":240,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-01T20:40:05.743Z","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/websoftwares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG","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":"2015-04-27T06:21:46.000Z","updated_at":"2019-08-18T16:43:36.000Z","dependencies_parsed_at":"2022-08-23T18:00:38.434Z","dependency_job_id":null,"html_url":"https://github.com/websoftwares/middleware","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/websoftwares/middleware","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/websoftwares%2Fmiddleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/websoftwares%2Fmiddleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/websoftwares%2Fmiddleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/websoftwares%2Fmiddleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/websoftwares","download_url":"https://codeload.github.com/websoftwares/middleware/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/websoftwares%2Fmiddleware/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273868155,"owners_count":25182423,"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","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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-10-01T22:57:10.888Z","updated_at":"2025-09-06T06:42:17.976Z","avatar_url":"https://github.com/websoftwares.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"#Middleware (v0.0.*)\nThis package lets u manage middleware for a HTTP request and response that implement the [PSR-7](https://github.com/php-fig/fig-standards/blob/master/proposed/http-message.md) HTTP message interfaces\n`Psr\\Http\\Message\\ServerRequestInterface` and `Psr\\Http\\Message\\ResponseInterface`.\n\n[![Build Status](https://api.travis-ci.org/websoftwares/middleware.png)](https://travis-ci.org/websoftwares/middleware)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/websoftwares/middleware/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/websoftwares/middleware/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/websoftwares/middleware/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/websoftwares/middleware/?branch=master)\n\n## Installing via Composer (recommended)\n\nInstall composer in your project:\n```\ncurl -s http://getcomposer.org/installer | php\n```\n\nCreate a composer.json file in your project root:\n```php\n{\n    \"require\": {\n\t\t\"websoftwares/middleware\": ~0.0.1\"\n    }\n}\n```\n\nInstall via composer\n```\nphp composer.phar install\n```\n\n## Usage\nBasic usage of the `MiddlewareRunner` class.\n\n```php\nuse Websoftwares\\Middleware\\MiddlewareRunner;\n\n$middleware = new MiddlewareRunner;\n\n// Some middleware object that is callable through invoke or a closure \n// for consistency u could implement the `Websoftwares\\MiddlewareInterface`.\n\n// Invokable object\n$throttleMiddleware = new ThrotteObject\n\n// request + middelewareOne decoration \u003c= objects are passed by reference\n$middelewareOne = function($request, $response) {\n    // Decorate the foo property\n    $request-\u003efoo = $request-\u003efoo + 1;\n};\n\n// response + middlewareTwo decoration \u003c= objects are passed by reference\n$middlewareTwo = function($request, $response) {\n    // / Decorate the bar property\n    $response-\u003ebar = $response-\u003ebar . ' World';\n};\n\n$middleware-\u003eadd($throttleMiddleware);\n$middleware-\u003eadd($middelewareOne);\n$middleware-\u003eadd($middlewareTwo);\n...\n// Add more middleware\n...\n\n$m = $middleware;\n\n// Call\n$m($request, $response);\n\n```\n\n## Routing example with external package\nTheir are many excellent PHP router packages and in time some will be made compatible with PSR-7.\nIn this basic example we will show u how to use the `MiddlewareRunner` class in conjunction with the latest development version of the [Aura Router package](https://github.com/auraphp/Aura.Router/tree/3.x).\n\n\n```php\nuse Websoftwares\\Middleware\\MiddlewareRunner;\nuse Aura\\Router\\RouterContainer;\n\n$routerContainer = new RouterContainer;\n$map = $routerContainer-\u003egetMap();\n$matcher = $routerContainer-\u003egetMatcher();\n\n$middleware = new MiddlewareRunner;\n\n// response + middlewareOne decoration \u003c= objects are passed by reference\n$middlewareOne = function ($request, $response) {\n    // / Decorate the bar property\n    $response-\u003ebar = $response-\u003ebar.' World';\n};\n\n$routeIndexAction = function($request, $response) {\n    // Awesome sauce\n    return $response;\n};\n\n// Add middleware\n$middleware-\u003eadd($middlewareTwo);\n\n...\n// Add more middleware\n...\n\n// Add route as last one\n$middleware-\u003eadd($routeIndexAction);\n\n$map-\u003eget('index.read', '/',$middleware); // \u003c-- middleware becomes the handler\n\n// We have a matching route\n$route = $matcher-\u003ematch($request);\n$handler = $route-\u003ehandler;\n\n// Call\n$handler($request, $response);\n\n```\n\n## Adapters\nAt the time of writing PSR-7 is ~~almost on the horizon~~ released :-) and their are many well written community supported HTTP orientated packages but most packages are not yet compliant.\n\nTo avoid mass rewrites of all these great packages or waiting for the author and or community to update them or holding out on the advantage of new compliant packages we can make use of the Adapter pattern to make them for example suitable for PSR-7 middleware.\n\n## Adapter RequestAuthenticatorAdapter example\nThe package [acquia/http-hmac-php](https://github.com/acquia/http-hmac-php) is an implementation of the HTTP HMAC Spec in PHP \nWe want to validate the signature throw an exception or continue the middleware stack if it is a valid signature.\n\n```php\nuse Websoftwares\\Middleware\\MiddlewareRunner;\nuse Acquia\\Hmac\\RequestSigner;\nuse Acquia\\Hmac\\RequestAuthenticator;\nuse Websoftwares\\Middleware\\Adapter\\RequestAuthenticatorAdapter;\n\n$middleware = new MiddlewareRunner;\n\n// response + middlewareOne decoration \u003c= objects are passed by reference\n$middlewareOne = function ($request, $response) {\n    // / Decorate the bar property\n    $response-\u003ebar = $response-\u003ebar.' World';\n};\n\n// Add middleware\n$middleware-\u003eadd($middlewareOne);\n\n...\n// Add more middleware\n...\n\n\n$authenticator = new RequestAuthenticator(new RequestSigner(), '+15 minutes');\n\n// $keyLoader implements \\Acquia\\Hmac\\KeyLoaderInterface\n$authenticatorMiddleware = new RequestAuthenticatorAdapter($authenticator, $keyLoader);\n\n$middleware-\u003eadd($authenticatorMiddleware);\n\n// Call\n$m = $middleware;\n\n$m($request, $response);\n\n```\n\n## Changelog\n- v0.0.11: Updated psr-7 psr/http-message to 1.0 and renamed phly/http with zendframework/zend-diactoros\n- v0.0.10: Logic to exit on response added\n- v0.0.9: Added abstract adapter and first implementation \"acquia/http-hmac-php\" package\n\n## Testing\nIn the tests folder u can find several tests.\n\n## Acknowledgement\nInspired by all the great middleware packages\n\n- [Interpose](https://github.com/carbocation/interpose)\n- [StackPHP](http://stackphp.com)\n- [Conduit](https://github.com/bigeasy/conduit)\n- [Conduit-php](https://github.com/phly/conduit)\n- [Rack](https://github.com/rack/rack)\n\n## License\nThe [MIT](http://opensource.org/licenses/MIT \"MIT\") License (MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebsoftwares%2Fmiddleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebsoftwares%2Fmiddleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebsoftwares%2Fmiddleware/lists"}