-
Notifications
You must be signed in to change notification settings - Fork 18
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
Add ReusableRelay #9
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
namespace Relay; | ||
|
||
class RelayFactory | ||
{ | ||
public function __construct(array $queue, $resolver = null) | ||
{ | ||
$this->queue = $queue; | ||
$this->resolver = $resolver; | ||
} | ||
|
||
public function newInstance() | ||
{ | ||
return new Relay($this->queue, $this->resolver); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
namespace Relay; | ||
|
||
use Psr\Http\Message\RequestInterface as Request; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
|
||
class ReusableRelay | ||
{ | ||
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. Missing 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. Fixed. |
||
public function __construct(RelayFactory $relayFactory) | ||
{ | ||
$this->relayFactory = $relayFactory; | ||
} | ||
|
||
public function __invoke(Request $request, Response $response) | ||
{ | ||
$relay = $this->relayFactory->newInstance(); | ||
return $relay($request, $response); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
namespace Relay; | ||
|
||
use ArrayObject; | ||
use InvalidArgumentException; | ||
|
||
class ReusableRelayBuilder | ||
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. Having the 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. The "reusable" indicates it's building a "reusable relay" (as vs a single-use relay). Note that Relay will become Runner in the next iteration, and ReusableRelay will become Relay. 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. Ah, okay... one step at a time. |
||
{ | ||
protected $resolver; | ||
|
||
public function __construct($resolver = null) | ||
{ | ||
$this->resolver = $resolver; | ||
} | ||
|
||
public function newInstance($queue) | ||
{ | ||
return new ReusableRelay(new RelayFactory( | ||
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 worry about the |
||
$this->getArray($queue), | ||
$this->resolver | ||
)); | ||
} | ||
|
||
protected function getArray($queue) | ||
{ | ||
if (is_array($queue)) { | ||
return $queue; | ||
} | ||
|
||
$getArrayCopy = $queue instanceof GetArrayCopyInterface | ||
|| $queue instanceof ArrayObject; | ||
|
||
if ($getArrayCopy) { | ||
return $queue->getArrayCopy(); | ||
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. Would be more useful to use protected function getArray($queue)
{
if (is_array($queue)) {
return $queue;
}
return iterator_to_array($queue);
} This way you can bypass the 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. Ah so! I like that idea, though it does not apply specifically to the reusability of the relay; that logic pre-exists in the 1.x branch in RelayBuilder. Let's take a look at that separately from this PR. 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. 👍 |
||
} | ||
|
||
throw new InvalidArgumentException(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
namespace Relay; | ||
|
||
use Zend\Diactoros\ServerRequestFactory; | ||
use Zend\Diactoros\Response; | ||
|
||
class ReusableRelayTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function test() | ||
{ | ||
FakeMiddleware::$count = 0; | ||
|
||
$queue[] = new FakeMiddleware(); | ||
$queue[] = new FakeMiddleware(); | ||
$queue[] = new FakeMiddleware(); | ||
|
||
$builder = new ReusableRelayBuilder(); | ||
$relay = $builder->newInstance($queue); | ||
|
||
// relay once | ||
$response = $relay( | ||
ServerRequestFactory::fromGlobals(), | ||
new Response() | ||
); | ||
$actual = (string) $response->getBody(); | ||
$this->assertSame('123456', $actual); | ||
|
||
// relay again | ||
$response = $relay( | ||
ServerRequestFactory::fromGlobals(), | ||
new Response() | ||
); | ||
$actual = (string) $response->getBody(); | ||
$this->assertSame('789101112', $actual); | ||
} | ||
} |
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.
I think it would be good to have
@var
declarations for the$queue
and$resolver
.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.
Fixed.