{"id":13828358,"url":"https://github.com/middlewares/utils","last_synced_at":"2026-01-07T23:54:57.250Z","repository":{"id":48940486,"uuid":"69686712","full_name":"middlewares/utils","owner":"middlewares","description":"Common utils used by PSR-15 middlewares","archived":false,"fork":false,"pushed_at":"2023-12-17T13:01:30.000Z","size":183,"stargazers_count":50,"open_issues_count":1,"forks_count":12,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-09-08T06:58:48.203Z","etag":null,"topics":["http","middleware","psr-15","psr-17","psr-7","utils"],"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/middlewares.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2016-09-30T17:19:52.000Z","updated_at":"2024-03-19T11:25:01.000Z","dependencies_parsed_at":"2024-01-18T05:25:27.590Z","dependency_job_id":null,"html_url":"https://github.com/middlewares/utils","commit_stats":{"total_commits":152,"total_committers":15,"mean_commits":"10.133333333333333","dds":"0.29605263157894735","last_synced_commit":"670b135ce0dbd040eadb025a9388f9bd617cc010"},"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Futils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Futils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Futils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/middlewares%2Futils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/middlewares","download_url":"https://codeload.github.com/middlewares/utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225492420,"owners_count":17482869,"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":["http","middleware","psr-15","psr-17","psr-7","utils"],"created_at":"2024-08-04T09:02:42.836Z","updated_at":"2026-01-07T23:54:57.194Z","avatar_url":"https://github.com/middlewares.png","language":"PHP","readme":"# middlewares/utils\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Software License][ico-license]](LICENSE)\n![Testing][ico-ga]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nCommon utilities used by the middlewares' packages:\n\n* [Factory](#factory)\n* [Dispatcher](#dispatcher)\n* [CallableHandler](#callablehandler)\n* [HttpErrorException](#httperrorexception)\n\n## Installation\n\nThis package is installable and autoloadable via Composer as [middlewares/utils](https://packagist.org/packages/middlewares/utils).\n\n```sh\ncomposer require middlewares/utils\n```\n\n## Factory\n\nUsed to create PSR-7 and PSR-17 instances.\nDetects automatically [Diactoros](https://github.com/laminas/laminas-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), [Nyholm/psr7](https://github.com/Nyholm/psr7) and [Sunrise](https://github.com/sunrise-php) but you can register a different factory using the [psr/http-factory](https://github.com/php-fig/http-factory) interface.\n\n```php\nuse Middlewares\\Utils\\Factory;\nuse Middlewares\\Utils\\FactoryDiscovery;\n\n// Create PSR-7 instances\n$request = Factory::createRequest('GET', '/');\n$serverRequest = Factory::createServerRequest('GET', '/');\n$response = Factory::createResponse(200);\n$stream = Factory::createStream('Hello world');\n$uri = Factory::createUri('http://example.com');\n$uploadedFile = Factory::createUploadedFile($stream);\n\n// Get PSR-17 instances (factories)\n$requestFactory = Factory::getRequestFactory();\n$serverRequestFactory = Factory::getServerRequestFactory();\n$responseFactory = Factory::getResponseFactory();\n$streamFactory = Factory::getStreamFactory();\n$uriFactory = Factory::getUriFactory();\n$uploadedFileFactory = Factory::getUploadedFileFactory();\n\n// By default, use the FactoryDiscovery class that detects diactoros, guzzle, slim, nyholm and sunrise (in this order of priority),\n// but you can change it and add other libraries\n\nFactory::setFactory(new FactoryDiscovery(\n    'MyApp\\Psr17Factory',\n    FactoryDiscovery::SLIM,\n    FactoryDiscovery::GUZZLE,\n    FactoryDiscovery::DIACTOROS\n));\n\n//And also register directly an initialized factory\nFactory::getFactory()-\u003esetResponseFactory(new FooResponseFactory());\n\n$fooResponse = Factory::createResponse();\n```\n\n## Dispatcher\n\nMinimalist PSR-15 compatible dispatcher. Used for testing purposes.\n\n```php\nuse Middlewares\\Utils\\Dispatcher;\n\n$response = Dispatcher::run([\n    new Middleware1(),\n    new Middleware2(),\n    new Middleware3(),\n    function ($request, $next) {\n        $response = $next-\u003ehandle($request);\n        return $response-\u003ewithHeader('X-Foo', 'Bar');\n    }\n]);\n```\n\n## CallableHandler\n\nTo resolve and execute a callable. It can be used as a middleware, server request handler or a callable:\n\n```php\nuse Middlewares\\Utils\\CallableHandler;\n\n$callable = new CallableHandler(function () {\n    return 'Hello world';\n});\n\n$response = $callable();\n\necho $response-\u003egetBody(); //Hello world\n```\n\n## HttpErrorException\n\nGeneral purpose exception used to represent HTTP errors.\n\n```php\nuse Middlewares\\Utils\\HttpErrorException;\n\ntry {\n    $context = ['problem' =\u003e 'Something bad happened'];\n    throw HttpErrorException::create(500, $context);\n} catch (HttpErrorException $exception) {\n    $context = $exception-\u003egetContext();\n}\n```\n\n---\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.\n\nThe MIT License (MIT). Please see [LICENSE](LICENSE) for more information.\n\n[ico-version]: https://img.shields.io/packagist/v/middlewares/utils.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-ga]: https://github.com/middlewares/utils/workflows/testing/badge.svg\n[ico-downloads]: https://img.shields.io/packagist/dt/middlewares/utils.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/middlewares/utils\n[link-downloads]: https://packagist.org/packages/middlewares/utils\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Futils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiddlewares%2Futils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiddlewares%2Futils/lists"}