Zend\Stratigility\MiddlewarePipe::process PHP Method

process() public method

Executes the internal pipeline, passing $delegate as the "final handler" in cases when the pipeline exhausts itself.
public process ( Psr\Http\Message\ServerRequestInterface $request, Interop\Http\Middleware\DelegateInterface $delegate ) : Psr\Http\Message\ResponseInterface
$request Psr\Http\Message\ServerRequestInterface
$delegate Interop\Http\Middleware\DelegateInterface
return Psr\Http\Message\ResponseInterface
    public function process(Request $request, DelegateInterface $delegate)
    {
        $response = $this->responsePrototype;
        $next = new Next($this->pipeline, $delegate);
        if ($response) {
            $next->setResponsePrototype($response);
        }
        if ($this->raiseThrowables) {
            $next->raiseThrowables();
        }
        $result = $next->process($request);
        return $result instanceof Response ? $result : $response;
    }

Usage Example

 /**
  * @todo Remove for 2.0.0, as error middleware is removed in that version.
  * @group error-handling
  */
 public function testEnablingRaiseThrowablesCausesProcessToThrowExceptions()
 {
     $expected = new RuntimeException('To throw from middleware');
     $pipeline = new MiddlewarePipe();
     $pipeline->raiseThrowables();
     $middleware = $this->prophesize(ServerMiddlewareInterface::class);
     $middleware->process(Argument::type(ServerRequestInterface::class), Argument::type(DelegateInterface::class))->will(function () use($expected) {
         throw $expected;
     });
     $pipeline->pipe($middleware->reveal());
     $done = $this->prophesize(DelegateInterface::class);
     $done->process(Argument::any())->shouldNotBeCalled();
     try {
         $pipeline->process($this->request, $done->reveal());
         $this->fail('Pipeline with middleware that throws did not result in exception!');
     } catch (RuntimeException $e) {
         $this->assertSame($expected, $e);
     } catch (Throwable $e) {
         $this->fail(sprintf('Unexpected throwable raised by pipeline: %s', $e->getMessage()));
     } catch (\Exception $e) {
         $this->fail(sprintf('Unexpected exception raised by pipeline: %s', $e->getMessage()));
     }
 }