-
Notifications
You must be signed in to change notification settings - Fork 16
Added CacheKeyGenerator #35
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
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5d50b37
Added CacheKeyGenerator
Nyholm b42d061
cs fix
Nyholm b3a09d8
Support PSR-4
Nyholm c116b88
Added test to make sure cache_key_generator works
Nyholm 2690ae1
cs
Nyholm 92e0039
Changes according to feedback.
Nyholm a97aa41
Make plugin final
Nyholm ae018f3
Bugfixes
Nyholm c6dd7d6
Remove final
Nyholm fa46a79
typo
Nyholm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace spec\Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class SimpleGeneratorSpec extends ObjectBehavior | ||
{ | ||
public function it_is_initializable() | ||
{ | ||
$this->shouldHaveType('Http\Client\Common\Plugin\Cache\Generator\SimpleGenerator'); | ||
} | ||
|
||
public function it_is_a_key_generator() | ||
{ | ||
$this->shouldImplement('Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator'); | ||
} | ||
|
||
public function it_generates_cache_from_request(RequestInterface $request) | ||
{ | ||
$request->getMethod()->shouldBeCalled()->willReturn('GET'); | ||
$request->getUri()->shouldBeCalled()->willReturn('http://example.com/foo'); | ||
$request->getBody()->shouldBeCalled()->willReturn('bar'); | ||
|
||
$this->generate($request)->shouldReturn('GET http://example.com/foo bar'); | ||
} | ||
|
||
public function it_generates_cache_from_request_with_no_body(RequestInterface $request) | ||
{ | ||
$request->getMethod()->shouldBeCalled()->willReturn('GET'); | ||
$request->getUri()->shouldBeCalled()->willReturn('http://example.com/foo'); | ||
$request->getBody()->shouldBeCalled()->willReturn(''); | ||
|
||
// No extra space after uri | ||
$this->generate($request)->shouldReturn('GET http://example.com/foo'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* An interface for generate a cache key. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
interface CacheKeyGenerator | ||
{ | ||
/** | ||
* Generate a cache key from a Request. | ||
* | ||
* @param RequestInterface $request | ||
* | ||
* @return string | ||
*/ | ||
public function generate(RequestInterface $request); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Http\Client\Common\Plugin\Cache\Generator; | ||
|
||
use Psr\Http\Message\RequestInterface; | ||
|
||
/** | ||
* Generate a cache key from the request method, URI and body. | ||
* | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class SimpleGenerator implements CacheKeyGenerator | ||
{ | ||
public function generate(RequestInterface $request) | ||
{ | ||
$body = (string) $request->getBody(); | ||
if (!empty($body)) { | ||
$body = ' '.$body; | ||
} | ||
|
||
return $request->getMethod().' '.$request->getUri().$body; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Not sure if I should use null here or if I should instanciate the default generator. Using null will make sure we do not instanceiate unneeded objects.