Skip to content

Commit

Permalink
Quickly configure middle (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Feb 21, 2024
1 parent 371d6e5 commit 7e95841
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/ConnectorConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Fansipan\Middleware\RetryRequests;
use Fansipan\Retry\Delay;
use Fansipan\Retry\GenericRetryStrategy;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

/**
* @template T of ConnectorInterface
Expand Down Expand Up @@ -38,6 +40,21 @@ final public function configure(ConnectorInterface $connector): ConnectorInterfa
return $clone;
}

/**
* Register a middleware configuration handler.
*
* @param callable(RequestInterface, callable): ResponseInterface $middleware
* @return static
*/
final public function middleware(callable $middleware, string $name = '')
{
return $this->register(static function (ConnectorInterface $connector) use ($middleware, $name) {
$connector->middleware()->unshift(
$middleware, $name
);
});
}

/**
* Register a configuration handler.
*
Expand Down Expand Up @@ -84,13 +101,11 @@ public function followRedirects(
bool $strict = false,
bool $referer = false
) {
return $this->register(static function (ConnectorInterface $connector) use ($max, $protocols, $strict, $referer) {
$connector->middleware()->unshift(new FollowRedirects(
$max,
$protocols,
$strict,
$referer
), 'follow_redirects');
});
return $this->middleware(new FollowRedirects(
$max,
$protocols,
$strict,
$referer
), 'follow_redirects');
}
}

0 comments on commit 7e95841

Please sign in to comment.