| @@ 14-42 (lines=29) @@ | ||
| 11 | * |
|
| 12 | * @author Soufiane Ghzal <[email protected]> |
|
| 13 | */ |
|
| 14 | final class HeaderDefaultsPlugin implements Plugin |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var array |
|
| 18 | */ |
|
| 19 | private $headers = []; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param array $headers headers to set to the request |
|
| 23 | */ |
|
| 24 | public function __construct(array $headers) |
|
| 25 | { |
|
| 26 | $this->headers = $headers; |
|
| 27 | } |
|
| 28 | ||
| 29 | /** |
|
| 30 | * {@inheritdoc} |
|
| 31 | */ |
|
| 32 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 33 | { |
|
| 34 | foreach ($this->headers as $header => $headerValue) { |
|
| 35 | if (!$request->hasHeader($header)) { |
|
| 36 | $request = $request->withHeader($header, $headerValue); |
|
| 37 | } |
|
| 38 | } |
|
| 39 | ||
| 40 | return $next($request); |
|
| 41 | } |
|
| 42 | } |
|
| 43 | ||
| @@ 13-41 (lines=29) @@ | ||
| 10 | * |
|
| 11 | * @author Soufiane Ghzal <[email protected]> |
|
| 12 | */ |
|
| 13 | final class HeaderRemovePlugin implements Plugin |
|
| 14 | { |
|
| 15 | /** |
|
| 16 | * @var array |
|
| 17 | */ |
|
| 18 | private $headers = []; |
|
| 19 | ||
| 20 | /** |
|
| 21 | * @param array $headers headers to remove from the request |
|
| 22 | */ |
|
| 23 | public function __construct(array $headers) |
|
| 24 | { |
|
| 25 | $this->headers = $headers; |
|
| 26 | } |
|
| 27 | ||
| 28 | /** |
|
| 29 | * {@inheritdoc} |
|
| 30 | */ |
|
| 31 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 32 | { |
|
| 33 | foreach ($this->headers as $header) { |
|
| 34 | if ($request->hasHeader($header)) { |
|
| 35 | $request = $request->withoutHeader($header); |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| 39 | return $next($request); |
|
| 40 | } |
|
| 41 | } |
|
| 42 | ||
| @@ 14-40 (lines=27) @@ | ||
| 11 | * |
|
| 12 | * @author Soufiane Ghzal <[email protected]> |
|
| 13 | */ |
|
| 14 | final class HeaderSetPlugin implements Plugin |
|
| 15 | { |
|
| 16 | /** |
|
| 17 | * @var array |
|
| 18 | */ |
|
| 19 | private $headers = []; |
|
| 20 | ||
| 21 | /** |
|
| 22 | * @param array $headers headers to set to the request |
|
| 23 | */ |
|
| 24 | public function __construct(array $headers) |
|
| 25 | { |
|
| 26 | $this->headers = $headers; |
|
| 27 | } |
|
| 28 | ||
| 29 | /** |
|
| 30 | * {@inheritdoc} |
|
| 31 | */ |
|
| 32 | public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
| 33 | { |
|
| 34 | foreach ($this->headers as $header => $headerValue) { |
|
| 35 | $request = $request->withHeader($header, $headerValue); |
|
| 36 | } |
|
| 37 | ||
| 38 | return $next($request); |
|
| 39 | } |
|
| 40 | } |
|
| 41 | ||