Skip to content

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

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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"php-http/httplug": "^1.0",
"php-http/message-factory": "^1.0.2",
"php-http/client-common": "^1.0",
"php-http/message": "^1.0",
"php-http/message": "^1.1",
"symfony/options-resolver": "^2.6|^3.0"
},
"require-dev": {
Expand Down
55 changes: 55 additions & 0 deletions spec/RequestConditionalPluginSpec.php
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);
}
}
46 changes: 46 additions & 0 deletions src/RequestConditionalPlugin.php
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.
Copy link
Contributor

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.

*
* @author Márk Sági-Kazár <[email protected]>
*/
final class RequestConditionalPlugin implements Plugin
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
ChainPlugin.

Tobias Nyholm [email protected] ezt írta (2016. március 5.,
szombat):

In src/RequestConditionalPlugin.php
#65 (comment):

+{

  • /**
  • \* @var RequestMatcher
    
  • */
    
  • private $requestMatcher;
  • /**
  • \* @var Plugin
    
  • */
    
  • private $delegatedPlugin;
  • /**
  • \* @param RequestMatcher $requestMatcher
    
  • \* @param Plugin         $delegatedPlugin
    
  • */
    
  • public function __construct(RequestMatcher $requestMatcher, Plugin $delegatedPlugin)

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?


Reply to this email directly or view it on GitHub
https://github.com/php-http/plugins/pull/65/files#r55119904.

Copy link
Member

Choose a reason for hiding this comment

The 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);
}
}