Skip to content

Commit e5dd20d

Browse files
committed
Manually apply and close php-http/plugins#65
1 parent 08b4ad9 commit e5dd20d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace spec\Http\Client\Common\Plugin;
4+
5+
use Http\Client\Common\Plugin;
6+
use Http\Message\RequestMatcher;
7+
use Http\Promise\Promise;
8+
use Psr\Http\Message\RequestInterface;
9+
use PhpSpec\ObjectBehavior;
10+
use Prophecy\Argument;
11+
12+
class RequestMatcherPluginSpec extends ObjectBehavior
13+
{
14+
function let(RequestMatcher $requestMatcher, Plugin $plugin)
15+
{
16+
$this->beConstructedWith($requestMatcher, $plugin);
17+
}
18+
19+
function it_is_initializable()
20+
{
21+
$this->shouldHaveType('Http\Client\Common\Plugin\RequestMatcherPlugin');
22+
}
23+
24+
function it_is_a_plugin()
25+
{
26+
$this->shouldImplement('Http\Client\Common\Plugin');
27+
}
28+
29+
function it_matches_a_request_and_delegates_to_plugin(
30+
RequestInterface $request,
31+
RequestMatcher $requestMatcher,
32+
Plugin $plugin
33+
) {
34+
$requestMatcher->matches($request)->willReturn(true);
35+
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldBeCalled();
36+
37+
$this->handleRequest($request, function () {}, function () {});
38+
}
39+
40+
function it_does_not_match_a_request(
41+
RequestInterface $request,
42+
RequestMatcher $requestMatcher,
43+
Plugin $plugin,
44+
Promise $promise
45+
) {
46+
$requestMatcher->matches($request)->willReturn(false);
47+
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldNotBeCalled();
48+
49+
$next = function (RequestInterface $request) use($promise) {
50+
return $promise->getWrappedObject();
51+
};
52+
53+
$this->handleRequest($request, $next, function () {})->shouldReturn($promise);
54+
}
55+
}

src/Plugin/RequestMatcherPlugin.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Http\Client\Common\Plugin;
4+
5+
use Http\Client\Common\Plugin;
6+
use Http\Message\RequestMatcher;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
/**
10+
* Apply a delegated plugin based on a request match.
11+
*
12+
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
13+
*/
14+
final class RequestMatcherPlugin implements Plugin
15+
{
16+
/**
17+
* @var RequestMatcher
18+
*/
19+
private $requestMatcher;
20+
21+
/**
22+
* @var Plugin
23+
*/
24+
private $delegatedPlugin;
25+
26+
/**
27+
* @param RequestMatcher $requestMatcher
28+
* @param Plugin $delegatedPlugin
29+
*/
30+
public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)
31+
{
32+
$this->requestMatcher = $requestMatcher;
33+
$this->delegatedPlugin = $delegatedPlugin;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function handleRequest(RequestInterface $request, callable $next, callable $first)
40+
{
41+
if ($this->requestMatcher->matches($request)) {
42+
return $this->delegatedPlugin->handleRequest($request, $next, $first);
43+
}
44+
45+
return $next($request);
46+
}
47+
}

0 commit comments

Comments
 (0)