Skip to content
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

Merged
merged 4 commits into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add ReusableRelay
  • Loading branch information
Paul M. Jones committed Jun 24, 2015
commit c280e0b55250865a867c1b29a2a0e0754d2da581
16 changes: 16 additions & 0 deletions src/RelayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Relay;

class RelayFactory
{
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

public function __construct(array $queue, $resolver = null)
{
$this->queue = $queue;
$this->resolver = $resolver;
}

public function newInstance()
{
return new Relay($this->queue, $this->resolver);
}
}
19 changes: 19 additions & 0 deletions src/ReusableRelay.php
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
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing @var here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
}
}
39 changes: 39 additions & 0 deletions src/ReusableRelayBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
namespace Relay;

use ArrayObject;
use InvalidArgumentException;

class ReusableRelayBuilder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the Reusable prefix here seems redundant with the Builder suffix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

The 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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I worry about the new RelayFactory here because I don't see a way to replace it with a subtype.

$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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be more useful to use iterator_to_array instead of array copying? The code could look like this:

protected function getArray($queue)
{
    if (is_array($queue)) {
        return $queue;
    }
    return iterator_to_array($queue);
}

This way you can bypass the instanceof checks entirely and rely on builtins.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

throw new InvalidArgumentException();
}
}
36 changes: 36 additions & 0 deletions tests/ReusableRelayTest.php
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);
}
}