{"id":33966312,"url":"https://github.com/ellipsephp/dispatcher","last_synced_at":"2025-12-12T23:06:54.254Z","repository":{"id":56976843,"uuid":"80728808","full_name":"ellipsephp/dispatcher","owner":"ellipsephp","description":"Psr-15 middleware dispatcher implementation","archived":false,"fork":false,"pushed_at":"2018-03-19T15:41:27.000Z","size":136,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-26T19:46:48.792Z","etag":null,"topics":["dispatcher","middleware","psr-15","request-handler"],"latest_commit_sha":null,"homepage":"https://github.com/ellipsephp/dispatcher","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/ellipsephp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-02T13:46:02.000Z","updated_at":"2019-06-01T20:58:45.000Z","dependencies_parsed_at":"2022-08-21T08:10:49.080Z","dependency_job_id":null,"html_url":"https://github.com/ellipsephp/dispatcher","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"purl":"pkg:github/ellipsephp/dispatcher","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ellipsephp%2Fdispatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ellipsephp%2Fdispatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ellipsephp%2Fdispatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ellipsephp%2Fdispatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ellipsephp","download_url":"https://codeload.github.com/ellipsephp/dispatcher/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ellipsephp%2Fdispatcher/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27694499,"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-12-12T02:00:06.775Z","response_time":129,"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":["dispatcher","middleware","psr-15","request-handler"],"created_at":"2025-12-12T23:06:53.524Z","updated_at":"2025-12-12T23:06:54.249Z","avatar_url":"https://github.com/ellipsephp.png","language":"PHP","readme":"# Dispatcher\n\nThis package provides a [Psr-15](https://www.php-fig.org/psr/psr-15/) dispatcher implementation.\n\n**Require** php \u003e= 7.0\n\n**Installation** `composer require ellipse/dispatcher`\n\n**Run tests** `./vendor/bin/kahlan`\n\n- [Using a dispatcher](#using-a-dispatcher)\n- [Middleware and request handler resolving](#middleware-and-request-handler-resolving)\n\n## Using a dispatcher\n\nThis package provides an `Ellipse\\Dispatcher` class allowing to process a Psr-7 request through a Psr-15 middleware queue (First in first out order) before handling it with a Psr-15 request handler in order to create a Psr-7 response.\n\nIt is basically a request handler decorator wrapping a middleware queue around a request handler. Its constructor takes two parameters:\n\n- a request handler object implementing `Psr\\Http\\Server\\RequestHandlerInterface`\n- an array containing middleware objects implementing `Psr\\Http\\Server\\MiddlewareInterface`\n\nThe `Dispatcher` itself implements `RequestHandlerInterface` so a response is produced by using its `-\u003ehandle()` method with a request. It also means it can be used as the request handler of another `Dispatcher`. Also, The same `Dispatcher` can be used multiple times to handle as many requests as needed.\n\nFinally when a value of the given middleware queue is not an implementation of `MiddlewareInterface` an `Ellipse\\Dispatcher\\Exceptions\\MiddlewareTypeException` is thrown. [Factory decorators](#middleware-and-request-handler-resolving) can be used to resolve some type of values as middleware.\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Ellipse\\Dispatcher;\n\n// Create a dispatcher using two middleware and a request handler.\n$dispatcher = new Dispatcher(new SomeRequestHandler, [\n    new SomeMiddleware1,\n    new SomeMiddleware2,\n]);\n\n// Here the request goes through SomeMiddleware1, SomeMiddleware2 and SomeRequestHandler.\n$response = $dispatcher-\u003ehandle($request);\n\n// It can be used as the request handler of another dispatcher.\n// Here the request goes through SomeMiddleware3, SomeMiddleware1, SomeMiddleware2 and SomeRequestHandler\n(new Dispatcher($dispatcher, [new SomeMiddleware3]))-\u003ehandle($request);\n\n// Here a MiddlewareTypeException is thrown because 'something' is not a Psr-15 middleware.\nnew Dispatcher(new SomeRequestHandler, [new SomeMiddleware, 'something']);\n```\n\nThe `Dispatcher` class also has a `-\u003ewith()` method taking a `MiddlewareInterface` as parameter. It returns a new dispatcher with the given middleware wrapped around the current dispatcher. The new middleware will be the first processed by the new dispatcher:\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Ellipse\\Dispatcher;\n\n// Create a dispatcher with two middleware.\n$dispatcher = new Dispatcher(new SomeRequestHandler, [\n    new SomeMiddleware1,\n    new SomeMiddleware2,\n]);\n\n// Create a new dispatcher with a new middleware on the top of the middleware queue.\n$dispatcher = $dispatcher-\u003ewith(new SomeMiddleware3);\n\n// Here the request goes through SomeMiddleware3, SomeMiddleware1, SomeMiddleware2 and SomeRequestHandler.\n$response = $dispatcher-\u003ehandle($request);\n\n// It allows to create dispatchers from the outside-in if you like.\n$dispatcher = new Dispatcher(new SomeRequestHandler);\n\n$dispatcher = $dispatcher-\u003ewith(new SomeMiddleware3);\n$dispatcher = $dispatcher-\u003ewith(new SomeMiddleware2);\n$dispatcher = $dispatcher-\u003ewith(new SomeMiddleware1);\n\n// Here the request goes through SomeMiddleware1, SomeMiddleware2, SomeMiddleware3 and SomeRequestHandler.\n$response = $dispatcher-\u003ehandle($request);\n```\n\n## Middleware and request handler resolving\n\nA common practice is to allow callables and class names registered in a container to be used as regular middleware/request handler.\n\nFor this purpose this package also provides an `Ellipse\\DispatcherFactory` class implementing `Ellipse\\DispatcherFactoryInterface`, allowing to produce `Dispatcher` instances. Its `__invoke()` method takes any value as request handler and an optional middleware queue. An `Ellipse\\Dispatcher\\Exceptions\\RequestHandlerTypeException` is thrown when the given request handler is not an implementation of `RequestHandlerInterface`.\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Ellipse\\DispatcherFactory;\n\n// Get a dispatcher factory.\n$factory = new DispatcherFactory;\n\n// Use the factory to create a new Dispatcher.\n$dispatcher = $factory(new SomeRequestHandler, [new SomeMiddleware]);\n\n// Here a RequestHandlerTypeException is thrown because 'something' is not a Psr-15 request handler.\n$dispatcher = $factory('something', [new SomeMiddleware]);\n\n// Here a MiddlewareTypeException is thrown by the Dispatcher class because 'something' is not a Psr-15 middleware.\n$dispatcher = $factory(new SomeRequestHandler, [new SomeMiddleware, 'something']);\n```\n\nThis class is not very useful by itself. The point of `DispatcherFactory` is to be decorated by other factories resolving the given values as Psr-15 implementations before delegating it the dispatcher creation. It is a starting point for such factory decorators (also called resolvers) which ensure the dispatcher creation fails nicely when any value is not resolved as a Psr-15 implementation by any decorator.\n\nHere is an example of callable resolving using the `Ellipse\\Dispatcher\\CallableResolver` class from the [ellipse/dispatcher-callable](https://github.com/ellipsephp/dispatcher-callable) package:\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Ellipse\\DispatcherFactory;\nuse Ellipse\\Dispatcher\\CallableResolver;\n\n// Get a decorated dispatcher factory.\n$factory = new CallableResolver(new DispatcherFactory);\n\n// A dispatcher using both callables and Psr-15 implementations can now be created.\n$middleware = function ($request, $handler) {\n\n    // This callable behave like a Psr-15 middleware.\n\n};\n\n$handler = function ($request) {\n\n    // This callable behave like a Psr-15 request handler.\n\n};\n\n// This works.\n$response = $factory($handler, [$middleware, new SomeMiddleware])-\u003ehandle($request);\n```\n\nHere is some ellipse packages providing resolvers for common resolving scenario:\n\n- [ellipse/dispatcher-callable](https://github.com/ellipsephp/dispatcher-callable) allowing to use callables as Psr-15 middleware/request handlers\n- [ellipse/dispatcher-container](https://github.com/ellipsephp/dispatcher-container) allowing to use Psr-15 middleware/request handler class names using a [Psr-11](https://www.php-fig.org/psr/psr-11/) container\n- [ellipse/dispatcher-controller](https://github.com/ellipsephp/dispatcher-controller) allowing to use controller actions as Psr-15 request handlers using a [Psr-11](https://www.php-fig.org/psr/psr-11/) container\n\nHere is an example of a class implementing `DispatcherFactoryInterface` in case you need to create a custom one:\n\n```php\n\u003c?php\n\nnamespace App;\n\nuse Ellipse\\Dispatcher;\nuse Ellipse\\DispatcherFactoryInterface;\n\nclass MyResolver implements DispatcherFactoryInterface\n{\n    private $delegate;\n\n    public function __construct(DispatcherFactoryInterface $delegate)\n    {\n        $this-\u003edelegate = $delegate;\n    }\n\n    public function __invoke($handler, array $middleware = []): Dispatcher\n    {\n        // Replace the handler with a ResolvedRequestHandler when the request handler should be resolved.\n        $handler = $this-\u003eshouldResolveHandler($handler)\n            : new ResolvedRequestHandler($handler)\n            ? $handler;\n\n        // Replace middleware with a ResolvedMiddleware when a middleware should be resolved.\n        $middleware = array_map(function ($middleware) {\n\n            return $this-\u003eshouldResolveMiddleware($middleware)\n                ? new ResolvedMiddleware($middleware)\n                : $middleware;\n\n        }, $middleware);\n\n        // Delegate the dispatcher creation to the decorated factory.\n        return ($this-\u003edelegate)($handler, $middleware);\n    }\n\n    private shouldResolveHandler($handler): bool\n    {\n        // ...\n    }\n\n    private shouldResolveMiddleware($middleware): bool\n    {\n        // ...\n    }\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fellipsephp%2Fdispatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fellipsephp%2Fdispatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fellipsephp%2Fdispatcher/lists"}