This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 92
/
MiddlewareController.php
123 lines (107 loc) · 3.57 KB
/
MiddlewareController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @link http://github.com/zendframework/zend-mvc for the canonical source repository
* @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (http://www.zend.com)
* @license https://github.com/zendframework/zend-mvc/blob/master/LICENSE.md New BSD License
*/
namespace Zend\Mvc\Controller;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\Request;
use Zend\Mvc\Exception\ReachedFinalHandlerException;
use Zend\Mvc\Exception\RuntimeException;
use Zend\Mvc\MvcEvent;
use Zend\Psr7Bridge\Psr7ServerRequest;
use Zend\Router\RouteMatch;
use Zend\Stratigility\Delegate\CallableDelegateDecorator;
use Zend\Stratigility\MiddlewarePipe;
/**
* @internal don't use this in your codebase, or else @ocramius will hunt you
* down. This is just an internal hack to make middleware trigger
* 'dispatch' events attached to the DispatchableInterface identifier.
*
* Specifically, it will receive a @see MiddlewarePipe and a
* @see ResponseInterface prototype, and then dispatch the pipe whilst still
* behaving like a normal controller. That is needed for any events
* attached to the @see \Zend\Stdlib\DispatchableInterface identifier to
* reach their listeners on any attached
* @see \Zend\EventManager\SharedEventManagerInterface
*/
final class MiddlewareController extends AbstractController
{
/**
* @var MiddlewarePipe
*/
private $pipe;
/**
* @var ResponseInterface
*/
private $responsePrototype;
public function __construct(
MiddlewarePipe $pipe,
ResponseInterface $responsePrototype,
EventManagerInterface $eventManager,
MvcEvent $event
) {
$this->eventIdentifier = __CLASS__;
$this->pipe = $pipe;
$this->responsePrototype = $responsePrototype;
$this->setEventManager($eventManager);
$this->setEvent($event);
}
/**
* {@inheritDoc}
*
* @throws RuntimeException
*/
public function onDispatch(MvcEvent $e)
{
$routeMatch = $e->getRouteMatch();
$psr7Request = $this->populateRequestParametersFromRoute(
$this->loadRequest()->withAttribute(RouteMatch::class, $routeMatch),
$routeMatch
);
$result = $this->pipe->process($psr7Request, new CallableDelegateDecorator(
function () {
throw ReachedFinalHandlerException::create();
},
$this->responsePrototype
));
$e->setResult($result);
return $result;
}
/**
* @return \Zend\Diactoros\ServerRequest
*
* @throws RuntimeException
*/
private function loadRequest()
{
$request = $this->request;
if (! $request instanceof Request) {
throw new RuntimeException(sprintf(
'Expected request to be a %s, %s given',
Request::class,
get_class($request)
));
}
return Psr7ServerRequest::fromZend($request);
}
/**
* @param ServerRequestInterface $request
* @param RouteMatch|null $routeMatch
*
* @return ServerRequestInterface
*/
private function populateRequestParametersFromRoute(ServerRequestInterface $request, RouteMatch $routeMatch = null)
{
if (! $routeMatch) {
return $request;
}
foreach ($routeMatch->getParams() as $key => $value) {
$request = $request->withAttribute($key, $value);
}
return $request;
}
}