/
Middleware.php
147 lines (126 loc) · 4.73 KB
/
Middleware.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
137
138
139
140
141
142
143
144
145
146
147
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Extend;
use Flarum\Extension\Extension;
use Illuminate\Contracts\Container\Container;
class Middleware implements ExtenderInterface
{
private $addMiddlewares = [];
private $removeMiddlewares = [];
private $replaceMiddlewares = [];
private $insertBeforeMiddlewares = [];
private $insertAfterMiddlewares = [];
private $frontend;
/**
* @param string $frontend: The name of the frontend.
*/
public function __construct(string $frontend)
{
$this->frontend = $frontend;
}
/**
* Adds a new middleware to the frontend.
*
* @param string $middleware: ::class attribute of the middleware class.
* Must implement \Psr\Http\Server\MiddlewareInterface.
* @return self
*/
public function add(string $middleware): self
{
$this->addMiddlewares[] = $middleware;
return $this;
}
/**
* Replaces an existing middleware of the frontend.
*
* @param string $originalMiddleware: ::class attribute of the original middleware class.
* Or container binding name.
* @param string $newMiddleware: ::class attribute of the middleware class.
* Must implement \Psr\Http\Server\MiddlewareInterface.
* @return self
*/
public function replace(string $originalMiddleware, string $newMiddleware): self
{
$this->replaceMiddlewares[$originalMiddleware] = $newMiddleware;
return $this;
}
/**
* Removes a middleware from the frontend.
*
* @param string $middleware: ::class attribute of the middleware class.
* @return self
*/
public function remove(string $middleware): self
{
$this->removeMiddlewares[] = $middleware;
return $this;
}
/**
* Inserts a middleware before an existing middleware.
*
* @param string $originalMiddleware: ::class attribute of the original middleware class.
* Or container binding name.
* @param string $newMiddleware: ::class attribute of the middleware class.
* Must implement \Psr\Http\Server\MiddlewareInterface.
* @return self
*/
public function insertBefore(string $originalMiddleware, string $newMiddleware): self
{
$this->insertBeforeMiddlewares[$originalMiddleware] = $newMiddleware;
return $this;
}
/**
* Inserts a middleware after an existing middleware.
*
* @param string $originalMiddleware: ::class attribute of the original middleware class.
* Or container binding name.
* @param string $newMiddleware: ::class attribute of the middleware class.
* Must implement \Psr\Http\Server\MiddlewareInterface.
* @return self
*/
public function insertAfter(string $originalMiddleware, string $newMiddleware): self
{
$this->insertAfterMiddlewares[$originalMiddleware] = $newMiddleware;
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
$container->extend("flarum.{$this->frontend}.middleware", function ($existingMiddleware) {
foreach ($this->addMiddlewares as $addMiddleware) {
$existingMiddleware[] = $addMiddleware;
}
foreach ($this->replaceMiddlewares as $originalMiddleware => $newMiddleware) {
$existingMiddleware = array_replace(
$existingMiddleware,
array_fill_keys(
array_keys($existingMiddleware, $originalMiddleware),
$newMiddleware
)
);
}
foreach ($this->insertBeforeMiddlewares as $originalMiddleware => $newMiddleware) {
array_splice(
$existingMiddleware,
array_search($originalMiddleware, $existingMiddleware),
0,
$newMiddleware
);
}
foreach ($this->insertAfterMiddlewares as $originalMiddleware => $newMiddleware) {
array_splice(
$existingMiddleware,
array_search($originalMiddleware, $existingMiddleware) + 1,
0,
$newMiddleware
);
}
$existingMiddleware = array_diff($existingMiddleware, $this->removeMiddlewares);
return $existingMiddleware;
});
}
}