-
Notifications
You must be signed in to change notification settings - Fork 6
Add request conditional plugin #65
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Plugin; | ||
|
||
use Http\Client\Plugin\Plugin; | ||
use Http\Message\RequestMatcher; | ||
use Http\Promise\Promise; | ||
use Psr\Http\Message\RequestInterface; | ||
use PhpSpec\ObjectBehavior; | ||
use Prophecy\Argument; | ||
|
||
class RequestConditionalPluginSpec extends ObjectBehavior | ||
{ | ||
function let(RequestMatcher $requestMatcher, Plugin $plugin) | ||
{ | ||
$this->beConstructedWith($requestMatcher, $plugin); | ||
} | ||
|
||
function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Plugin\RequestConditionalPlugin'); | ||
} | ||
|
||
function it_is_a_plugin() | ||
{ | ||
$this->shouldImplement('Http\Client\Plugin\Plugin'); | ||
} | ||
|
||
function it_matches_a_request_and_delegates_to_plugin( | ||
RequestInterface $request, | ||
RequestMatcher $requestMatcher, | ||
Plugin $plugin | ||
) { | ||
$requestMatcher->matches($request)->willReturn(true); | ||
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldBeCalled(); | ||
|
||
$this->handleRequest($request, function () {}, function () {}); | ||
} | ||
|
||
function it_does_not_match_a_request( | ||
RequestInterface $request, | ||
RequestMatcher $requestMatcher, | ||
Plugin $plugin, | ||
Promise $promise | ||
) { | ||
$requestMatcher->matches($request)->willReturn(false); | ||
$plugin->handleRequest($request, Argument::type('callable'), Argument::type('callable'))->shouldNotBeCalled(); | ||
|
||
$next = function (RequestInterface $request) use($promise) { | ||
return $promise->getWrappedObject(); | ||
}; | ||
|
||
$this->handleRequest($request, $next, function () {})->shouldReturn($promise); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Http\Client\Plugin; | ||
|
||
use Http\Message\RequestMatcher; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Apply a delegated plugin based on a request match. | ||
* | ||
* @author Márk Sági-Kazár <[email protected]> | ||
*/ | ||
final class RequestConditionalPlugin implements Plugin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i am not too happy with the name. the name does not really tell how the 3 terms go together. is this a plugin to request a conditional? or to make the request conditional? but unfortunately i have no better name. ConditionalRequestPlugin certainly is worse. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternative could be RequestMatcherPlugin. It's kinda obvious, don't know why I didn't think about it earlier. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, RequestMatcherPlugin is a good name! |
||
{ | ||
/** | ||
* @var RequestMatcher | ||
*/ | ||
private $requestMatcher; | ||
|
||
/** | ||
* @var Plugin | ||
*/ | ||
private $delegatedPlugin; | ||
|
||
/** | ||
* @param RequestMatcher $requestMatcher | ||
* @param Plugin $delegatedPlugin | ||
*/ | ||
public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I maybe want to use more than one plugin. I suggest allowing the second parameter to be an array of Plugins. Would that be possible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was also thinking about that. For that case I would add a separate Tobias Nyholm [email protected] ezt írta (2016. március 5.,
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okey. Cool! |
||
{ | ||
$this->requestMatcher = $requestMatcher; | ||
$this->delegatedPlugin = $delegatedPlugin; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function handleRequest(RequestInterface $request, callable $next, callable $first) | ||
{ | ||
if ($this->requestMatcher->matches($request)) { | ||
return $this->delegatedPlugin->handleRequest($request, $next, $first); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Execute a plugin only if the RequestMatcher matches the request.