Skip to content

Commit

Permalink
Merge pull request #3 from sasezaki/return-type-clear
Browse files Browse the repository at this point in the history
Introduce DispatchResult - Dispatcher must not have Response itself
  • Loading branch information
sasezaki committed Oct 29, 2015
2 parents 388f52e + 0b94c0e commit a23edff
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 50 deletions.
37 changes: 37 additions & 0 deletions src/Backbeard/DispatchResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Backbeard;

use Psr\Http\Message\ResponseInterface as Response;
use LogicException;

class DispatchResult implements DispatchResultInterface
{
private $dispatched;
private $response;

public function __construct($dispatched, $response = null)
{
$this->dispatched = $dispatched;
$this->response = $response;
}

/**
* @return bool
*/
public function isDispatched()
{
return $this->dispatched;
}

/**
* @return Response;
*/
public function getResponse()
{
if ($this->response instanceof Response) {
return $this->response;
}
throw new LogicException("Don't call when dispatch return false");
}
}
16 changes: 16 additions & 0 deletions src/Backbeard/DispatchResultInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Backbeard;

interface DispatchResultInterface
{
/**
* @return bool
*/
public function isDispatched();

/**
* @return \Psr\Http\Message\ResponseInterface
*/
public function getResponse();
}
36 changes: 12 additions & 24 deletions src/Backbeard/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ class Dispatcher
*/
private $templatePathResolver;

/**
* @var \Psr\Http\Message\ResponseInterface
*/
private $actionResponse;

public function __construct(Generator $routing, ViewInterface $view, RouterInterface $router)
{
$this->routing = $routing;
Expand All @@ -45,7 +40,7 @@ public function __construct(Generator $routing, ViewInterface $view, RouterInter
* @param Request $request
* @param Response $response
*
* @return bool
* @return DispatchResultInterface
*/
public function dispatch(Request $request, Response $response)
{
Expand Down Expand Up @@ -89,32 +84,18 @@ public function dispatch(Request $request, Response $response)
continue;
}

$this->actionResponse = $this->handleActionResult($routeResult, $actionResult, $response);
$response = $this->handleActionResult($routeResult, $actionResult, $response);

return true;
return new DispatchResult(true, $response);
}
$this->routing->next();
}

return false;
}

/**
* @throws \LogicException
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function getActionResponse()
{
if ($this->actionResponse instanceof Response) {
return $this->actionResponse;
}

throw new \LogicException("Don't call when dispatch return false");
return new DispatchResult(false);
}

/**
* @return \SfpBackbeard\TemplatePathResolverInterface
* @return \Backbeard\TemplatePathResolverInterface
*/
public function getTemplatePathResolver()
{
Expand All @@ -125,6 +106,10 @@ public function getTemplatePathResolver()
return $this->templatePathResolver;
}

/**
* @return bool
* @throws InvalidArgumentException
*/
protected function dispatchByType($route, Request $request)
{
switch (gettype($route)) {
Expand Down Expand Up @@ -166,6 +151,9 @@ protected function dispatchByType($route, Request $request)
}
}

/**
* @return Response
*/
protected function handleActionResult(RouteMatch $routeMatch, $actionResult, Response $response)
{
if (is_string($actionResult)) {
Expand Down
4 changes: 1 addition & 3 deletions src/Backbeard/RouteMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Backbeard;

/**
* RouteInterface match result.
* Route match result.
*/
class RouteMatch
{
Expand Down Expand Up @@ -35,8 +35,6 @@ public function __construct(array $params)
* Set name of matched route.
*
* @param string $name
*
* @return RouteMatch
*/
public function setMatchedRouteName($name)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Backbeard/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function match($route, $uri)
return false;
}

/**
* @return array
*/
private function buildRegexForRoute($routeData)
{
$regex = '';
Expand Down
3 changes: 3 additions & 0 deletions src/Backbeard/TemplatePathResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ class TemplatePathResolver implements TemplatePathResolverInterface
{
private $suffix = '.phtml';

/**
* @return string
*/
public function resolve(RouteMatch $routeMatch)
{
$name = $routeMatch->getMatchedRouteName();
Expand Down
46 changes: 23 additions & 23 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function testReturnFalseWhenNotMatched()
yield function () {return false;} => function () {};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$this->assertFalse($result);
$dispatcher->getActionResponse();
$this->assertFalse($result->isDispatched());
$result->getResponse();
}

public function testActionScope()
Expand All @@ -52,7 +52,7 @@ public function testActionScope()
};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$this->assertTrue($result);
$this->assertTrue($result->isDispatched());
$this->assertTrue($actionScopeResult['request'] instanceof ServerRequestInterface);
$this->assertTrue($actionScopeResult['response'] instanceof ResponseInterface);
}
Expand All @@ -75,16 +75,16 @@ public function testRoutingStringHandleAsRoute()

$result = $dispatcher->dispatch($request, $response);

$this->assertTrue($result);
$response = $dispatcher->getActionResponse();
$this->assertTrue($result->isDispatched());
$response = $result->getResponse();
$this->assertInstanceof(Response::class, $response);
$this->assertSame('5', (string) $response->getBody());

// not matched
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('http://example.com/bar/6'));
$response = new Response();
$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertInstanceof(Response::class, $response);
$this->assertSame('6', (string) $response->getBody());
}
Expand All @@ -104,16 +104,16 @@ public function testRoutingKeyHandleStringAsPath()
$dispatcher3 = new Dispatcher($gen(), $this->view, $this->router);

$result = $dispatcher1->dispatch($request, $response);
$this->assertFalse($result);
$this->assertFalse($result->isDispatched());

$request = $request->withUri((new Uri())->withPath('/foo'))->withMethod('GET');
$result = $dispatcher2->dispatch($request, $response);
$this->assertTrue($result);
$this->assertTrue($result->isDispatched());
$this->assertSame('hello', (string) $response->getBody());

$request = $request->withUri((new Uri())->withPath('/foo'))->withMethod('POST');
$result = $dispatcher3->dispatch($request, $response);
$this->assertFalse($result);
$this->assertFalse($result->isDispatched());

$gen = function () {
yield ['GET' => '/foo',
Expand All @@ -132,12 +132,12 @@ public function testRoutingKeyHandleStringAsPath()

$request = $request->withUri((new Uri())->withPath('/foo'))->withMethod('GET');
$result = $dispatcher4->dispatch($request, $response);
$this->assertFalse($result);
$this->assertFalse($result->isDispatched());

$request = $request->withUri((new Uri())->withPath('/foo'))->withMethod('GET');
$request = $request->withHeader('User-Agent', 'Mozilla/5.0');
$result = $dispatcher5->dispatch($request, $response);
$this->assertTrue($result);
$this->assertTrue($result->isDispatched());
}

public function testRoutingKeyHandleClosureAsMatcher()
Expand All @@ -149,7 +149,7 @@ public function testRoutingKeyHandleClosureAsMatcher()
yield function () {return true;} => function () {return 'bar';};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();

$this->assertSame('bar', (string) $response->getBody());
}
Expand All @@ -163,7 +163,7 @@ public function testRouterResultArrayShouldPassAction()
yield function () {return ['var1', 'var2'];} => function ($var1, $var2) {return $var1.$var2;};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame('var1var2', (string) $response->getBody());
}

Expand All @@ -176,7 +176,7 @@ public function testRouterResultRouteMatchShouldPassAction()
yield function () {return new RouteMatch(['var1', 'var2']);} => function ($var1, $var2) {return $var1.$var2;};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame('var1var2', (string) $response->getBody());
}

Expand Down Expand Up @@ -206,7 +206,7 @@ public function testActionReturnResponseShouldBeUsed()
};
}), $this->view, $this->router);
$result = $dispatcher->dispatch($request, $response);
$response2 = $dispatcher->getActionResponse();
$response2 = $result->getResponse();
$this->assertNotSame(spl_object_hash($response), spl_object_hash($response2));
}

Expand All @@ -220,7 +220,7 @@ public function testIntActionAsStatusCode()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame(503, $response->getStatusCode());
}

Expand All @@ -234,7 +234,7 @@ public function testActionResultIsIntUsedAsStatusCode()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame(503, $response->getStatusCode());
}

Expand All @@ -251,7 +251,7 @@ public function testActionReturnUnkown()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame('a', (string) $response->getBody());
}

Expand All @@ -266,7 +266,7 @@ public function testRoutenameWouldbeResolveAsTemplateName()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertInstanceof(ResponseInterface::class, $response);
$this->assertSame('var', (string) $response->getBody());
}
Expand All @@ -283,7 +283,7 @@ public function testContinueWhenActionReturnIsFalse()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame('oh matched', (string) $response->getBody());
}

Expand All @@ -299,7 +299,7 @@ public function testContinueWhenActionReturnHasActionContinueInterface()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();

$this->assertSame('bar', (string) $response->getBody());
}
Expand All @@ -325,7 +325,7 @@ public function testValidationErrorShouldContinueRoutingAndHasError()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();

$this->assertSame('foo', (string) $response->getBody());
}
Expand All @@ -350,7 +350,7 @@ public function testValidationThroughWhenNotMatchedRouting()
}), $this->view, $this->router);

$result = $dispatcher->dispatch($request, $response);
$response = $dispatcher->getActionResponse();
$response = $result->getResponse();
$this->assertSame('error is NULL', (string) $response->getBody());
}
}

0 comments on commit a23edff

Please sign in to comment.