| @@ 10-38 (lines=29) @@ | ||
| 7 | /** |
|
| 8 | * Adds headers to the request only if the request does not already own it. |
|
| 9 | */ |
|
| 10 | class HeaderAddPlugin implements Plugin |
|
| 11 | { |
|
| 12 | ||
| 13 | private $headers = []; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param array $headers headers to add to 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 => $headerValue) { |
|
| 30 | if (!$request->hasHeader($header)) { |
|
| 31 | $request = $request->withHeader($header, $headerValue); |
|
| 32 | } |
|
| 33 | } |
|
| 34 | ||
| 35 | ||
| 36 | return $next($request); |
|
| 37 | } |
|
| 38 | } |
|
| 39 | ||
| @@ 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 | ||
| @@ 10-36 (lines=27) @@ | ||
| 7 | /** |
|
| 8 | * Set headers to the request. Will override values if a header is already set |
|
| 9 | */ |
|
| 10 | class HeaderSetPlugin implements Plugin |
|
| 11 | { |
|
| 12 | ||
| 13 | private $headers = []; |
|
| 14 | ||
| 15 | /** |
|
| 16 | * @param array $headers headers to set to 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 => $headerValue) { |
|
| 30 | $request = $request->withHeader($header, $headerValue); |
|
| 31 | } |
|
| 32 | ||
| 33 | ||
| 34 | return $next($request); |
|
| 35 | } |
|
| 36 | } |
|
| 37 | ||