diff --git a/spec/AddHostPluginSpec.php b/spec/AddHostPluginSpec.php new file mode 100644 index 0000000..c02b05d --- /dev/null +++ b/spec/AddHostPluginSpec.php @@ -0,0 +1,86 @@ +beConstructedWith($uri); + } + + function it_is_initializable(UriInterface $uri) + { + $uri->getHost()->shouldBeCalled()->willReturn('example.com'); + $this->beConstructedWith($uri); + $this->shouldHaveType('Http\Client\Plugin\AddHostPlugin'); + } + + function it_is_a_plugin(UriInterface $uri) + { + $uri->getHost()->shouldBeCalled()->willReturn('example.com'); + $this->beConstructedWith($uri); + + $this->shouldImplement('Http\Client\Plugin\Plugin'); + } + + function it_adds_domain( + RequestInterface $request, + UriInterface $host, + UriInterface $uri + ) { + $host->getScheme()->shouldBeCalled()->willReturn('http://'); + $host->getHost()->shouldBeCalled()->willReturn('example.com'); + + $request->getUri()->shouldBeCalled()->willReturn($uri); + $request->withUri($uri)->shouldBeCalled()->willReturn($request); + + $uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); + $uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); + $uri->getHost()->shouldBeCalled()->willReturn(''); + + $this->beConstructedWith($host); + $this->handleRequest($request, function () {}, function () {}); + } + + function it_replaces_domain( + RequestInterface $request, + UriInterface $host, + UriInterface $uri + ) { + $host->getScheme()->shouldBeCalled()->willReturn('http://'); + $host->getHost()->shouldBeCalled()->willReturn('example.com'); + + $request->getUri()->shouldBeCalled()->willReturn($uri); + $request->withUri($uri)->shouldBeCalled()->willReturn($request); + + $uri->withScheme('http://')->shouldBeCalled()->willReturn($uri); + $uri->withHost('example.com')->shouldBeCalled()->willReturn($uri); + + + $this->beConstructedWith($host, ['replace'=>true]); + $this->handleRequest($request, function () {}, function () {}); + } + + function it_does_nothing_when_domain_exists( + RequestInterface $request, + UriInterface $host, + UriInterface $uri + ) { + $request->getUri()->shouldBeCalled()->willReturn($uri); + $uri->getHost()->shouldBeCalled()->willReturn('default.com'); + + $this->beConstructedWith($host); + $this->handleRequest($request, function () {}, function () {}); + } +} diff --git a/src/AddHostPlugin.php b/src/AddHostPlugin.php new file mode 100644 index 0000000..025ef80 --- /dev/null +++ b/src/AddHostPlugin.php @@ -0,0 +1,73 @@ + + */ +class AddHostPlugin implements Plugin +{ + /** + * @var UriInterface + */ + private $host; + + /** + * @var bool + */ + private $replace; + + /** + * @param UriInterface $host + * @param array $config { + * + * @var bool $replace True will replace all hosts, false will only add host when none is specified. + * } + */ + public function __construct(UriInterface $host, array $config = []) + { + if ($host->getHost() === '') { + throw new \LogicException('Host can not be empty'); + } + + $this->host = $host; + + $resolver = new OptionsResolver(); + $this->configureOptions($resolver); + $options = $resolver->resolve($config); + + $this->replace = $options['replace']; + } + + /** + * {@inheritdoc} + */ + public function handleRequest(RequestInterface $request, callable $next, callable $first) + { + if ($this->replace || $request->getUri()->getHost() === '') { + $uri = $request->getUri()->withHost($this->host->getHost()); + $uri = $uri->withScheme($this->host->getScheme()); + + $request = $request->withUri($uri); + } + + return $next($request); + } + + /** + * @param OptionsResolver $resolver + */ + private function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'replace' => false, + ]); + $resolver->setAllowedTypes('replace', 'bool'); + } +}