| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Http\Client\Common\Plugin; |
| 6 |
|
| 7 |
use Http\Client\Common\Plugin; |
| 8 |
use Http\Promise\Promise; |
| 9 |
use Psr\Http\Message\RequestInterface; |
| 10 |
use Psr\Http\Message\UriInterface; |
| 11 |
|
| 12 |
/** |
| 13 |
* Combines the AddHostPlugin and AddPathPlugin. |
| 14 |
* |
| 15 |
* @author Sullivan Senechal <soullivaneuh@gmail.com> |
| 16 |
*/ |
| 17 |
final class BaseUriPlugin implements Plugin |
| 18 |
{ |
| 19 |
/** |
| 20 |
* @var AddHostPlugin |
| 21 |
*/ |
| 22 |
private $addHostPlugin; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var AddPathPlugin|null |
| 26 |
*/ |
| 27 |
private $addPathPlugin = null; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param UriInterface $uri Has to contain a host name and can have a path |
| 31 |
* @param array $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions |
| 32 |
*/ |
| 33 |
public function __construct(UriInterface $uri, array $hostConfig = []) |
| 34 |
{ |
| 35 |
$this->addHostPlugin = new AddHostPlugin($uri, $hostConfig); |
| 36 |
|
| 37 |
if (rtrim($uri->getPath(), '/')) { |
| 38 |
$this->addPathPlugin = new AddPathPlugin($uri); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* {@inheritdoc} |
| 44 |
*/ |
| 45 |
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise |
| 46 |
{ |
| 47 |
$addHostNext = function (RequestInterface $request) use ($next, $first) { |
| 48 |
return $this->addHostPlugin->handleRequest($request, $next, $first); |
| 49 |
}; |
| 50 |
|
| 51 |
if ($this->addPathPlugin) { |
| 52 |
return $this->addPathPlugin->handleRequest($request, $addHostNext, $first); |
| 53 |
} |
| 54 |
|
| 55 |
return $addHostNext($request); |
| 56 |
} |
| 57 |
} |
| 58 |
|