Skip to content

Add AddPathPlugin always_prepend option #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Added

- Add the `always_prepend` option to `AddPathPlugin`.
- The `BaseUriPlugin` can now passthru options for the underlying `AddPathPlugin`.

### Deprecated

- The `debug_plugins` option for `PluginClient` is deprecated and will be removed in 2.0. Use the decorator design pattern instead like in [ProfilePlugin](https://github.com/php-http/HttplugBundle/blob/de33f9c14252f22093a5ec7d84f17535ab31a384/Collector/ProfilePlugin.php).
Expand Down
19 changes: 19 additions & 0 deletions spec/Plugin/AddPathPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,23 @@ function it_throws_exception_on_empty_path(UriInterface $host)
$this->beConstructedWith($host);
$this->shouldThrow('\LogicException')->duringInstantiation();
}

function it_adds_path_only_if_required(
RequestInterface $request,
UriInterface $host,
UriInterface $uri
) {
$host->getPath()->shouldBeCalled()->willReturn('/api');

$request->getUri()->shouldBeCalled()->willReturn($uri);
$request->withUri($uri)->shouldNotBeCalled();

$uri->withPath('/api/api/users')->shouldNotBeCalled();
$uri->getPath()->shouldBeCalled()->willReturn('/api/users');

$this->beConstructedWith($host, [
'always_prepend' => false,
]);
$this->handleRequest($request, function () {}, function () {});
}
}
18 changes: 18 additions & 0 deletions spec/Plugin/BaseUriPluginSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,22 @@ function it_replaces_domain_and_adds_path(
$this->beConstructedWith($host, ['replace' => true]);
$this->handleRequest($request, function () {}, function () {});
}

function it_adds_path_only_if_required(
RequestInterface $request,
UriInterface $host,
UriInterface $uri
) {
$host->getHost()->shouldBeCalled()->willReturn('example.com');
$host->getPath()->shouldBeCalled()->willReturn('/api');

$request->getUri()->shouldBeCalled()->willReturn($uri);
$request->withUri($uri)->shouldNotBeCalled();

$uri->getHost()->shouldBeCalled()->willReturn('example.com');
$uri->getPath()->shouldBeCalled()->willReturn('/api/users');

$this->beConstructedWith($host, [], ['always_prepend' => false]);
$this->handleRequest($request, function () {}, function () {});
}
}
40 changes: 36 additions & 4 deletions src/Plugin/AddPathPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Prepend a base path to the request URI. Useful for base API URLs like http://domain.com/api.
Expand All @@ -18,10 +19,19 @@ final class AddPathPlugin implements Plugin
*/
private $uri;

/**
* @var bool
*/
private $alwaysPrepend;

/**
* @param UriInterface $uri
* @param array $config {
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the extra blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like StyleCI wants this extra blank line.

* @var bool $alwaysPrepend Set to true to always prepend the path even if the request path start with that path.
* }
*/
public function __construct(UriInterface $uri)
public function __construct(UriInterface $uri, array $config = [])
{
if ($uri->getPath() === '') {
throw new \LogicException('URI path cannot be empty');
Expand All @@ -32,17 +42,39 @@ public function __construct(UriInterface $uri)
}

$this->uri = $uri;

$resolver = new OptionsResolver();
$this->configureOptions($resolver);
$options = $resolver->resolve($config);

$this->alwaysPrepend = $options['always_prepend'];
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
$request = $request->withUri($request->getUri()
->withPath($this->uri->getPath().$request->getUri()->getPath())
);
$prepend = $this->uri->getPath();
$path = $request->getUri()->getPath();

if ($this->alwaysPrepend || substr($path, 0, strlen($prepend)) !== $prepend) {
$request = $request->withUri($request->getUri()
->withPath($prepend.$path)
);
}

return $next($request);
}

/**
* @param OptionsResolver $resolver
*/
private function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'always_prepend' => true,
]);
$resolver->setAllowedTypes('always_prepend', 'bool');
}
}
5 changes: 3 additions & 2 deletions src/Plugin/BaseUriPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ final class BaseUriPlugin implements Plugin
/**
* @param UriInterface $uri Has to contain a host name and cans have a path.
* @param array $hostConfig Config for AddHostPlugin. @see AddHostPlugin::configureOptions
* @param array $pathConfig Config for AddPassPlugin. @see AddPathPlugin::configureOptions
*/
public function __construct(UriInterface $uri, array $hostConfig = [])
public function __construct(UriInterface $uri, array $hostConfig = [], array $pathConfig = [])
{
$this->addHostPlugin = new AddHostPlugin($uri, $hostConfig);

if (rtrim($uri->getPath(), '/')) {
$this->addPathPlugin = new AddPathPlugin($uri);
$this->addPathPlugin = new AddPathPlugin($uri, $pathConfig);
}
}

Expand Down