|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @see https://github.com/zendframework/zend-expressive for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Zend\Expressive; |
| 11 | + |
| 12 | +use Psr\Http\Message\ServerRequestInterface as Request; |
| 13 | +use Psr\Http\Message\ResponseInterface as Response; |
| 14 | +use Zend\Stratigility\FinalHandler; |
| 15 | +use Zend\Stratigility\MiddlewarePipe; |
| 16 | +use Zend\Stratigility\Next; |
| 17 | + |
| 18 | +/** |
| 19 | + * MiddlewarePipe implementation that acts as error middleware. |
| 20 | + * |
| 21 | + * Normal MiddlewarePipe implementations implement Zend\Stratigility\MiddlewareInterface, |
| 22 | + * which can be consumed as normal middleware, but not as error middleware, as |
| 23 | + * the signature for error middleware differs. |
| 24 | + * |
| 25 | + * This class wraps a MiddlewarePipe, and consumes its internal pipeline |
| 26 | + * within a functor signature that works for error middleware. |
| 27 | + * |
| 28 | + * It is not implemented as an extension of MiddlewarePipe, as that class |
| 29 | + * implements the MiddlewareInterface, which prevents its use as error |
| 30 | + * middleware. |
| 31 | + */ |
| 32 | +class ErrorMiddlewarePipe |
| 33 | +{ |
| 34 | + /** |
| 35 | + * @var MiddlewarePipe |
| 36 | + */ |
| 37 | + private $pipeline; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param MiddlewarePipe $pipe |
| 41 | + */ |
| 42 | + public function __construct(MiddlewarePipe $pipeline) |
| 43 | + { |
| 44 | + $this->pipeline = $pipeline; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Handle an error request. |
| 49 | + * |
| 50 | + * This is essentially a version of the MiddlewarePipe that acts as a pipeline |
| 51 | + * for solely error middleware; it's primary use case is to allow configuring |
| 52 | + * arrays of error middleware as a single pipeline. |
| 53 | + * |
| 54 | + * Operation is identical to MiddlewarePipe, with the single exception that |
| 55 | + * $next is called with the $error argument. |
| 56 | + * |
| 57 | + * @param mixed $error |
| 58 | + * @param Request $request |
| 59 | + * @param Response $response |
| 60 | + * @param callable $out |
| 61 | + * @return Response |
| 62 | + */ |
| 63 | + public function __invoke($error, Request $request, Response $response, callable $out = null) |
| 64 | + { |
| 65 | + $pipeline = $this->getInternalPipeline(); |
| 66 | + $done = $out ?: new FinalHandler([], $response); |
| 67 | + $next = new Next($pipeline, $done); |
| 68 | + $result = $next($request, $response, $error); |
| 69 | + |
| 70 | + return ($result instanceof Response ? $result : $response); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Retrieve the internal pipeline from the composed MiddlewarePipe. |
| 75 | + * |
| 76 | + * Uses reflection to retrieve the internal pipeline from the composed |
| 77 | + * MiddlewarePipe, in order to allow using it to create a Next instance. |
| 78 | + * |
| 79 | + * @return \SplQueue |
| 80 | + */ |
| 81 | + private function getInternalPipeline() |
| 82 | + { |
| 83 | + $r = new ReflectionProperty($this->pipeline, 'pipeline'); |
| 84 | + $r->setAccessible(); |
| 85 | + return $r->getValue($this->pipeline); |
| 86 | + } |
| 87 | +} |
0 commit comments