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