Download the PHP package ellipse/dispatcher without Composer
On this page you can find all versions of the php package ellipse/dispatcher. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ellipse/dispatcher
More information about ellipse/dispatcher
Files in ellipse/dispatcher
Package dispatcher
Short Description Psr-15 middleware dispatcher implementation
License MIT
Homepage https://github.com/ellipsephp/dispatcher
Informations about the package dispatcher
Dispatcher
This package provides a Psr-15 dispatcher implementation.
Require php >= 7.0
Installation composer require ellipse/dispatcher
Run tests ./vendor/bin/kahlan
- Using a dispatcher
- Middleware and request handler resolving
Using a dispatcher
This 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.
It is basically a request handler decorator wrapping a middleware queue around a request handler. Its constructor takes two parameters:
- a request handler object implementing
Psr\Http\Server\RequestHandlerInterface
- an array containing middleware objects implementing
Psr\Http\Server\MiddlewareInterface
The Dispatcher
itself implements RequestHandlerInterface
so a response is produced by using its ->handle()
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.
Finally when a value of the given middleware queue is not an implementation of MiddlewareInterface
an Ellipse\Dispatcher\Exceptions\MiddlewareTypeException
is thrown. Factory decorators can be used to resolve some type of values as middleware.
The Dispatcher
class also has a ->with()
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:
Middleware and request handler resolving
A common practice is to allow callables and class names registered in a container to be used as regular middleware/request handler.
For 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
.
This 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.
Here is an example of callable resolving using the Ellipse\Dispatcher\CallableResolver
class from the ellipse/dispatcher-callable package:
Here is some ellipse packages providing resolvers for common resolving scenario:
- ellipse/dispatcher-callable allowing to use callables as Psr-15 middleware/request handlers
- ellipse/dispatcher-container allowing to use Psr-15 middleware/request handler class names using a Psr-11 container
- ellipse/dispatcher-controller allowing to use controller actions as Psr-15 request handlers using a Psr-11 container
Here is an example of a class implementing DispatcherFactoryInterface
in case you need to create a custom one:
All versions of dispatcher with dependencies
psr/http-message Version ^1.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
ellipse/handlers Version ^1.0
ellipse/type-errors Version ^1.0