Skip to content

Commit

Permalink
break and use Pipeline dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M. Jones committed Jun 5, 2015
1 parent e1e096c commit 842bca4
Show file tree
Hide file tree
Showing 21 changed files with 91 additions and 296 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"require": {
"aura/di": "~3.0",
"aura/payload": "~3.0",
"aura/router": "~3.0"
"aura/router": "~3.0",
"pipeline/pipeline": "~1.0"
},
"require-dev": {
"zendframework/zend-diactoros": "~1.0"
Expand All @@ -19,7 +20,7 @@
"autoload-dev": {
"psr-4": {
"Radar\\Adr\\": "tests/",
"Aura\\Di\\_Config\\": "vendor/aura/di/tests/_Config"
"Aura\\Di\\": "vendor/aura/di/tests/"
}
},
"minimum-stability": "dev"
Expand Down
25 changes: 8 additions & 17 deletions src/Adr.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,19 @@

class Adr
{
protected $pipelineFactory;
protected $map;
protected $handlers;
protected $middle = [];
protected $rules;
protected $dispatcherFactory;

public function __construct(
Map $map,
RuleIterator $rules,
Handlers $handlers,
callable $dispatcherFactory
PipelineFactory $pipelineFactory
) {
$this->map = $map;
$this->rules = $rules;
$this->handlers = $handlers;
$this->dispatcherFactory = $dispatcherFactory;
$this->pipelineFactory = $pipelineFactory;
}

public function __call($method, $params)
Expand All @@ -37,19 +35,12 @@ public function rules()

public function middle($spec)
{
return $this->handlers->appendMiddle($spec);
return $this->middle[] = $spec;
}

public function exceptionHandler($spec)
public function run(Request $request, Response $response)
{
return $this->handlers->setExceptionHandler($spec);
}

public function run(
Request $request,
Response $response
) {
$dispatcher = call_user_func($this->dispatcherFactory, $this->handlers);
return $dispatcher($request, $response);
$pipeline = $this->pipelineFactory->newInstance($this->middle);
return $pipeline($request, $response);
}
}
2 changes: 1 addition & 1 deletion src/Boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ protected function cachedContainer(array $config)
protected function newContainer(array $config)
{
$config = array_merge(['Radar\Adr\Config'], $config);
return (new ContainerBuilder())->newConfiguredInstance([], $config);
return (new ContainerBuilder())->newConfiguredInstance($config);
}
}
13 changes: 6 additions & 7 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ public function define(Container $di)
*/
$di->params['Radar\Adr\Adr']['map'] = $di->lazyGetCall('radar/adr:router', 'getMap');
$di->params['Radar\Adr\Adr']['rules'] = $di->lazyGetCall('radar/adr:router', 'getRuleIterator');
$di->params['Radar\Adr\Adr']['handlers'] = $di->lazyNew('Radar\Adr\Handlers');
$di->params['Radar\Adr\Adr']['dispatcherFactory'] = $di->lazyNew('Radar\Adr\DispatcherFactory');
$di->params['Radar\Adr\Adr']['pipelineFactory'] = $di->lazyNew('Radar\Adr\PipelineFactory');

/**
* Radar\Adr\PipelineFactory
*/
$di->params['Radar\Adr\PipelineFactory']['resolver'] = $di->lazyGet('radar/adr:resolver');

/**
* Radar\Adr\Handler\ActionHandler
Expand All @@ -49,11 +53,6 @@ public function define(Container $di)
*/
$di->params['Radar\Adr\Handler\SendingHandler']['sender'] = $di->lazyNew('Radar\Adr\Sender');

/**
* Radar\Adr\Handlers
*/
$di->params['Radar\Adr\Handlers']['resolver'] = $di->lazyGet('radar/adr:resolver');

/**
* Radar\Adr\Resolver
*/
Expand Down
29 changes: 0 additions & 29 deletions src/Dispatcher.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/DispatcherFactory.php

This file was deleted.

7 changes: 2 additions & 5 deletions src/Handler/ActionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public function __construct(Resolver $resolver)
$this->resolver = $resolver;
}

public function __invoke(
Request $request,
Response $response,
callable $next
) {
public function __invoke(Request $request, Response $response, callable $next)
{
$route = $request->getAttribute('radar/adr:route');
$request = $request->withoutAttribute('radar/adr:route');
$response = $this->response($route, $request, $response);
Expand Down
17 changes: 9 additions & 8 deletions src/Handler/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ public function __construct(Sender $sender)
$this->sender = $sender;
}

public function __invoke(
Request $request,
Response $response,
Exception $exception
) {
$response = $response->withStatus(500);
$response->getBody()->write($exception->getMessage());
$this->sender->send($response);
public function __invoke(Request $request, Response $response, callable $next)
{
try {
$response = $next($request, $response);
} catch (Exception $e) {
$response = $response->withStatus(500);
$response->getBody()->write($e->getMessage());
$this->sender->send($response);
}
return $response;
}
}
7 changes: 2 additions & 5 deletions src/Handler/RoutingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ public function __construct(Matcher $matcher, Route $route)
$this->route = $route;
}

public function __invoke(
Request $request,
Response $response,
callable $next
) {
public function __invoke(Request $request, Response $response, callable $next)
{
$request = $this->routeRequest($request);
return $next($request, $response);
}
Expand Down
7 changes: 2 additions & 5 deletions src/Handler/SendingHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ public function __construct(Sender $sender)
$this->sender = $sender;
}

public function __invoke(
Request $request,
Response $response,
callable $next
) {
public function __invoke(Request $request, Response $response, callable $next)
{
$this->sender->send($response);
return $next($request, $response);
}
Expand Down
38 changes: 0 additions & 38 deletions src/Handlers.php

This file was deleted.

17 changes: 17 additions & 0 deletions src/PipelineFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
namespace Radar\Adr;

use Pipeline\Pipeline\Pipeline;

class PipelineFactory
{
public function __construct(Resolver $resolver)
{
$this->resolver = $resolver;
}

public function newInstance(array $queue)
{
return new Pipeline($queue, $this->resolver);
}
}
49 changes: 21 additions & 28 deletions tests/AdrTest.php
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
<?php
namespace Radar\Adr;

use Aura\Di\ContainerBuilder;
use Aura\Router\Rule\RuleIterator;
use Radar\Adr\Router\Route;
use Radar\Adr\Fake\FakeMiddleware;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;

class AdrTest extends \PHPUnit_Framework_TestCase
{
protected $adr;

public function setup()
public function setUp()
{
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$resolver = new Resolver($di->getInjectionFactory());

$this->fakeMap = new Fake\FakeMap(new Route());
$this->fakeRules = new RuleIterator();
$this->fakeHandlers = new Fake\FakeHandlers();
$this->fakeDispatcherFactory = function ($handlers) {
return new Fake\FakeDispatcher($handlers);
};
$this->pipelineFactory = new PipelineFactory($resolver);

$this->adr = new Adr(
$this->fakeMap,
$this->fakeRules,
$this->fakeHandlers,
$this->fakeDispatcherFactory
$this->pipelineFactory
);
}

Expand All @@ -38,30 +41,20 @@ public function testProxyToMap()
$this->assertSame($expect, $actual);
}

public function testHandlers()
public function testRun()
{
$this->adr->middle('middle1');
$this->adr->middle('middle2');
$this->adr->middle('middle3');
$this->adr->exceptionHandler('Foo\Bar\ExceptionHandler');
FakeMiddleware::$count = 0;

$expect = 'Foo\Bar\ExceptionHandler';
$this->assertSame($expect, $this->fakeHandlers->exceptionHandler);
$this->adr->middle('Radar\Adr\Fake\FakeMiddleware');
$this->adr->middle('Radar\Adr\Fake\FakeMiddleware');
$this->adr->middle('Radar\Adr\Fake\FakeMiddleware');

$expect = [
'middle1',
'middle2',
'middle3',
];
$this->assertSame($expect, $this->fakeHandlers->middle);
}
$response = $this->adr->run(
ServerRequestFactory::fromGlobals(),
new Response()
);

public function testRun()
{
$request = ServerRequestFactory::fromGlobals();
$response = new Response();
$expect = 'Radar\Adr\Fake\FakeDispatcher::__invoke';
$actual = $this->adr->run($request, $response);
$this->assertSame($expect, $actual);
$actual = (string) $response->getBody();
$this->assertSame('123456', $actual);
}
}
5 changes: 2 additions & 3 deletions tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php
namespace Radar\Adr;

use Aura\Di\_Config\AbstractContainerTest;
use Aura\Di\AbstractContainerConfigTest;

class ConfigTest extends AbstractContainerTest
class ConfigTest extends AbstractContainerConfigTest
{
protected function getConfigClasses()
{
Expand All @@ -30,7 +30,6 @@ public function provideNewInstance()
['Radar\Adr\Handler\ExceptionHandler'],
['Radar\Adr\Handler\RoutingHandler'],
['Radar\Adr\Handler\SendingHandler'],
['Radar\Adr\Handlers'],
['Radar\Adr\Resolver'],
['Radar\Adr\Router\Map'],
];
Expand Down
Loading

0 comments on commit 842bca4

Please sign in to comment.