/
MiddlewareStack.php
136 lines (113 loc) · 3.62 KB
/
MiddlewareStack.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
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace Concrete\Core\Http\Middleware;
use Concrete\Core\Application\ApplicationAwareInterface;
use Concrete\Core\Application\ApplicationAwareTrait;
use Concrete\Core\Http\MiddlewareFrame;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* The default stack used to keep track of middleware and process requests
* @package Concrete\Core\Http\Middleware
*/
final class MiddlewareStack implements StackInterface, ApplicationAwareInterface
{
use ApplicationAwareTrait;
/** @var array[] A list of lists of middlewares ordered by priority*/
protected $middleware = [];
/**
* @var \Concrete\Core\Http\Middleware\DispatcherFrame|Mock_BlockController_7fcd43c1|Mock_TrackableBlockController_a75af608
*/
protected $dispatcher;
/**
* MiddlewareStack constructor.
* Dispat
*/
public function __construct(DelegateInterface $dispatcher = null)
{
$this->dispatcher = $dispatcher;
}
/**
* Get a stack with the given dispatcher
* @param \Concrete\Core\Http\Middleware\DelegateInterface $dispatcher
* @return self
*/
public function withDispatcher(DelegateInterface $dispatcher)
{
$stack = clone $this;
$stack->dispatcher = $dispatcher;
return $stack;
}
/**
* @inheritdoc
*/
public function withMiddleware(MiddlewareInterface $middleware, $priority = 10)
{
$stack = clone $this;
if (!isset($stack->middleware[$priority])) {
$stack->middleware[$priority] = [];
}
$stack->middleware[$priority][] = $middleware;
return $stack;
}
/**
* @inheritdoc
*/
public function withoutMiddleware(MiddlewareInterface $middleware)
{
$stack = clone $this;
$stack->middleware = array_map(function($priorityGroup) use ($middleware) {
return array_map(function($stackMiddleware) use ($middleware) {
return $middleware === $stackMiddleware ? null : $stackMiddleware;
}, $priorityGroup);
}, $stack->middleware);
return $stack;
}
/**
* @inheritdoc
*/
public function process(Request $request)
{
$stack = $this->getStack();
return $stack->next($request);
}
/**
* Reduce middleware into a stack of functions that each call the next
* @return callable
*/
private function getStack()
{
$processed = [];
foreach ($this->middlewareGenerator() as $middleware) {
$processed[] = $middleware;
}
$middleware = array_reverse($processed);
$stack = array_reduce($middleware, $this->getZipper(), $this->dispatcher);
return $stack;
}
/**
* Get the function used to zip up the middleware
* This function runs as part of the array_reduce routine and reduces the list of middlewares into a single delegate
* @return callable
*/
private function getZipper()
{
$app = $this->app;
return function($last, MiddlewareInterface $middleware) use ($app) {
return $app->make(DelegateInterface::class, ['middleware' => $middleware, 'nextDelegate' => $last]);
};
}
/**
* Get a generator that converts the stored priority array into a sorted flat list
* @return \Generator
*/
private function middlewareGenerator()
{
$middlewares = $this->middleware;
ksort($middlewares);
foreach ($middlewares as $priorityGroup) {
foreach ($priorityGroup as $middleware) {
yield $middleware;
}
}
}
}