for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace Http\Client\Plugin;
use Psr\Http\Message\RequestInterface;
/**
* Manages http headers.
*/
class HeaderPlugin implements Plugin
{
* @var array headers that have fixed value
private $headers = [];
private $removedHeader = [];
* Sets a header with the given value to every requests.
*
* @param string $name name of the header
* @param string $value value of the header
* @param bool $override By default if the header is already set on the request the value wont be replaced
* pass $override to true to make the header value the same for every request
public function withHeader($name, $value, $override = false)
$this->headers[$name] = ['value' => $value, 'override' => $override];
}
* Automatically removes the given header from the request.
* @param string $name name of the header to remove
public function withoutHeader($name)
$this->removedHeader[] = $name;
* {@inheritdoc}
public function handleRequest(RequestInterface $request, callable $next, callable $first)
foreach ($this->headers as $header => $headerData) {
if ($headerData['override'] || !$request->hasHeader($header)) {
$request = $request->withHeader($header, $headerData['value']);
foreach ($this->removedHeader as $header) {
if ($request->hasHeader($header)) {
$request = $request->withoutHeader($header);
return $next($request);