@@ 11-39 (lines=29) @@ | ||
8 | * Set default values for the request headers. |
|
9 | * If a given header already exists the value wont be replaced |
|
10 | */ |
|
11 | class HeaderDefaultsPlugin implements Plugin |
|
12 | { |
|
13 | ||
14 | private $headers = []; |
|
15 | ||
16 | /** |
|
17 | * @param array $headers headers to set to the request |
|
18 | */ |
|
19 | public function __construct(array $headers) |
|
20 | { |
|
21 | $this->headers = $headers; |
|
22 | } |
|
23 | ||
24 | ||
25 | /** |
|
26 | * {@inheritdoc} |
|
27 | */ |
|
28 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
29 | { |
|
30 | foreach ($this->headers as $header => $headerValue) { |
|
31 | if(!$request->hasHeader($header)){ |
|
32 | $request = $request->withHeader($header, $headerValue); |
|
33 | } |
|
34 | } |
|
35 | ||
36 | ||
37 | return $next($request); |
|
38 | } |
|
39 | } |
|
40 |
@@ 10-36 (lines=27) @@ | ||
7 | /** |
|
8 | * Removes headers from the request. |
|
9 | */ |
|
10 | class HeaderRemovePlugin implements Plugin |
|
11 | { |
|
12 | ||
13 | private $headers = []; |
|
14 | ||
15 | /** |
|
16 | * @param array $headers headers to remove from the request |
|
17 | */ |
|
18 | public function __construct(array $headers) |
|
19 | { |
|
20 | $this->headers = $headers; |
|
21 | } |
|
22 | ||
23 | ||
24 | /** |
|
25 | * {@inheritdoc} |
|
26 | */ |
|
27 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
28 | { |
|
29 | foreach ($this->headers as $header) { |
|
30 | if ($request->hasHeader($header)) { |
|
31 | $request = $request->withoutHeader($header); |
|
32 | } |
|
33 | } |
|
34 | return $next($request); |
|
35 | } |
|
36 | } |
|
37 |
@@ 11-37 (lines=27) @@ | ||
8 | * Set headers to the request. |
|
9 | * If the header does not exist it wil be set, if the header already exists it will be replaced. |
|
10 | */ |
|
11 | class HeaderSetPlugin implements Plugin |
|
12 | { |
|
13 | ||
14 | private $headers = []; |
|
15 | ||
16 | /** |
|
17 | * @param array $headers headers to set to the request |
|
18 | */ |
|
19 | public function __construct(array $headers) |
|
20 | { |
|
21 | $this->headers = $headers; |
|
22 | } |
|
23 | ||
24 | ||
25 | /** |
|
26 | * {@inheritdoc} |
|
27 | */ |
|
28 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
29 | { |
|
30 | foreach ($this->headers as $header => $headerValue) { |
|
31 | $request = $request->withHeader($header, $headerValue); |
|
32 | } |
|
33 | ||
34 | ||
35 | return $next($request); |
|
36 | } |
|
37 | } |
|
38 |