Add Dispatching namesapce
This commit is contained in:
parent
560b1e8ff0
commit
bbb138996a
|
|
@ -0,0 +1,7 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Dispatching;
|
||||||
|
|
||||||
|
class DispatchException extends \InvalidArgumentException
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
namespace WellRESTed\Dispatching;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
@ -10,9 +10,12 @@ class DispatchStack implements DispatchStackInterface
|
||||||
private $stack;
|
private $stack;
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
|
||||||
public function __construct()
|
/**
|
||||||
|
* @param DispatcherInterface $dispatcher
|
||||||
|
*/
|
||||||
|
public function __construct(DispatcherInterface $dispatcher)
|
||||||
{
|
{
|
||||||
$this->dispatcher = $this->getDispatcher();
|
$this->dispatcher = $dispatcher;
|
||||||
$this->stack = [];
|
$this->stack = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -59,13 +62,6 @@ class DispatchStack implements DispatchStackInterface
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
||||||
protected function getDispatcher()
|
|
||||||
{
|
|
||||||
return new Dispatcher();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
|
||||||
|
|
||||||
private function getCallableChain()
|
private function getCallableChain()
|
||||||
{
|
{
|
||||||
$dispatcher = $this->dispatcher;
|
$dispatcher = $this->dispatcher;
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
namespace WellRESTed\Dispatching;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use WellRESTed\Routing\MiddlewareInterface;
|
||||||
|
|
||||||
interface DispatchStackInterface extends MiddlewareInterface
|
interface DispatchStackInterface extends MiddlewareInterface
|
||||||
{
|
{
|
||||||
|
|
@ -1,18 +1,20 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Routing;
|
namespace WellRESTed\Dispatching;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use WellRESTed\Routing\MiddlewareInterface;
|
||||||
|
|
||||||
class Dispatcher implements DispatcherInterface
|
class Dispatcher implements DispatcherInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param $middleware
|
* @param mixed $middleware
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
* @param ResponseInterface $response
|
* @param ResponseInterface $response
|
||||||
|
* @param callable $next
|
||||||
* @return ResponseInterface
|
* @return ResponseInterface
|
||||||
* @throws \InvalidArgumentException $middleware is not a valid type.
|
* @throws DispatchException Unable to dispatch $middleware
|
||||||
*/
|
*/
|
||||||
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next)
|
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next)
|
||||||
{
|
{
|
||||||
|
|
@ -26,7 +28,7 @@ class Dispatcher implements DispatcherInterface
|
||||||
} elseif ($middleware instanceof ResponseInterface) {
|
} elseif ($middleware instanceof ResponseInterface) {
|
||||||
return $middleware;
|
return $middleware;
|
||||||
} else {
|
} else {
|
||||||
throw new \InvalidArgumentException("Unable to dispatch middleware.");
|
throw new DispatchException("Unable to dispatch middleware.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace WellRESTed\Dispatching;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
interface DispatcherInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Dispatch middleware and return the response.
|
||||||
|
*
|
||||||
|
* This method MUST pass $request, $response, and $next to the middleware
|
||||||
|
* to be dispatched.
|
||||||
|
*
|
||||||
|
* $middleware comes in a number of varieties (e.g., instance, string,
|
||||||
|
* callable). DispatcherInterface interface exist to unpack the middleware
|
||||||
|
* and dispatch it.
|
||||||
|
*
|
||||||
|
* Implementations MUST be able to dispatch the following:
|
||||||
|
* - An instance implementing MiddlewareInterface
|
||||||
|
* - A string containing the fully qualified class name of a class
|
||||||
|
* implementing MiddlewareInterface
|
||||||
|
* - A callable that returns an instance implementing MiddlewareInterface
|
||||||
|
* - A callable with a signature matching MiddlewareInterface::dispatch
|
||||||
|
*
|
||||||
|
* Implementation MAY dispatch other types of middleware.
|
||||||
|
*
|
||||||
|
* When an implementation recieves a $middware that is not of a type it can
|
||||||
|
* dispatch, it MUST throw a DispatchException.
|
||||||
|
*
|
||||||
|
* @param mixed $middleware
|
||||||
|
* @param ServerRequestInterface $request
|
||||||
|
* @param ResponseInterface $response
|
||||||
|
* @param callable $next
|
||||||
|
* @return ResponseInterface
|
||||||
|
* @throws DispatchException Unable to dispatch $middleware
|
||||||
|
*/
|
||||||
|
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next);
|
||||||
|
}
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: pjdietz
|
|
||||||
* Date: 4/6/15
|
|
||||||
* Time: 8:29 PM
|
|
||||||
*/
|
|
||||||
namespace WellRESTed\Routing;
|
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
|
||||||
|
|
||||||
interface DispatcherInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @param $middleware
|
|
||||||
* @param ServerRequestInterface $request
|
|
||||||
* @param ResponseInterface $response
|
|
||||||
* @param callable $next
|
|
||||||
* @return ResponseInterface
|
|
||||||
*/
|
|
||||||
public function dispatch($middleware, ServerRequestInterface $request, ResponseInterface $response, $next);
|
|
||||||
}
|
|
||||||
|
|
@ -4,6 +4,8 @@ namespace WellRESTed\Routing;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use WellRESTed\Dispatching\Dispatcher;
|
||||||
|
use WellRESTed\Dispatching\DispatcherInterface;
|
||||||
|
|
||||||
class MethodMap implements MiddlewareInterface, MethodMapInterface
|
class MethodMap implements MiddlewareInterface, MethodMapInterface
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -3,18 +3,19 @@
|
||||||
namespace WellRESTed\Test\Unit\Routing;
|
namespace WellRESTed\Test\Unit\Routing;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use WellRESTed\Routing\DispatchStack;
|
use WellRESTed\Dispatching\DispatchStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass WellRESTed\Routing\DispatchStack
|
* @coversDefaultClass WellRESTed\Dispatching\DispatchStack
|
||||||
* @uses WellRESTed\Routing\DispatchStack
|
* @uses WellRESTed\Dispatching\DispatchStack
|
||||||
* @uses WellRESTed\Routing\Dispatcher
|
* @group dispatching
|
||||||
*/
|
*/
|
||||||
class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $request;
|
private $request;
|
||||||
private $response;
|
private $response;
|
||||||
private $next;
|
private $next;
|
||||||
|
private $dispatcher;
|
||||||
|
|
||||||
public function setUp()
|
public function setUp()
|
||||||
{
|
{
|
||||||
|
|
@ -24,15 +25,19 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->next = function ($request, $response) {
|
$this->next = function ($request, $response) {
|
||||||
return $response;
|
return $response;
|
||||||
};
|
};
|
||||||
|
$this->dispatcher = $this->prophesize('WellRESTed\Dispatching\DispatcherInterface');
|
||||||
|
$this->dispatcher->dispatch(Argument::cetera())->will(function ($args) {
|
||||||
|
list($middleware, $request, $response, $next) = $args;
|
||||||
|
return $middleware($request, $response, $next);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::__construct
|
* @covers ::__construct
|
||||||
* @covers ::getDispatcher
|
|
||||||
*/
|
*/
|
||||||
public function testCreatesInstance()
|
public function testCreatesInstance()
|
||||||
{
|
{
|
||||||
$stack = new DispatchStack();
|
$stack = new DispatchStack($this->dispatcher->reveal());
|
||||||
$this->assertNotNull($stack);
|
$this->assertNotNull($stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,19 +46,20 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testAddIsFluid()
|
public function testAddIsFluid()
|
||||||
{
|
{
|
||||||
$stack = new DispatchStack();
|
$stack = new DispatchStack($this->dispatcher->reveal());
|
||||||
$this->assertSame($stack, $stack->add("middleware1"));
|
$this->assertSame($stack, $stack->add("middleware1"));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers ::dispatch
|
* @covers ::dispatch
|
||||||
|
* @covers ::getCallableChain
|
||||||
*/
|
*/
|
||||||
public function testDispachesMiddlewareInOrderAdded()
|
public function testDispachesMiddlewareInOrderAdded()
|
||||||
{
|
{
|
||||||
// Each middelware will add its "name" to this array.
|
// Each middelware will add its "name" to this array.
|
||||||
$callOrder = [];
|
$callOrder = [];
|
||||||
|
|
||||||
$stack = new DispatchStack();
|
$stack = new DispatchStack($this->dispatcher->reveal());
|
||||||
$stack->add(function ($request, $response, $next) use (&$callOrder) {
|
$stack->add(function ($request, $response, $next) use (&$callOrder) {
|
||||||
$callOrder[] = "first";
|
$callOrder[] = "first";
|
||||||
return $next($request, $response);
|
return $next($request, $response);
|
||||||
|
|
@ -70,6 +76,9 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals(["first", "second", "third"], $callOrder);
|
$this->assertEquals(["first", "second", "third"], $callOrder);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::dispatch
|
||||||
|
*/
|
||||||
public function testCallsNextAfterDispatchingStack()
|
public function testCallsNextAfterDispatchingStack()
|
||||||
{
|
{
|
||||||
$nextCalled = false;
|
$nextCalled = false;
|
||||||
|
|
@ -82,7 +91,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
return $next($request, $response);
|
return $next($request, $response);
|
||||||
};
|
};
|
||||||
|
|
||||||
$stack = new DispatchStack();
|
$stack = new DispatchStack($this->dispatcher->reveal());
|
||||||
$stack->add($middleware);
|
$stack->add($middleware);
|
||||||
$stack->add($middleware);
|
$stack->add($middleware);
|
||||||
$stack->add($middleware);
|
$stack->add($middleware);
|
||||||
|
|
@ -102,7 +111,7 @@ class DispatchStackTest extends \PHPUnit_Framework_TestCase
|
||||||
return $response;
|
return $response;
|
||||||
};
|
};
|
||||||
|
|
||||||
$stack = new DispatchStack();
|
$stack = new DispatchStack($this->dispatcher->reveal());
|
||||||
$stack->dispatch($this->request->reveal(), $this->response->reveal(), $next);
|
$stack->dispatch($this->request->reveal(), $this->response->reveal(), $next);
|
||||||
$this->assertTrue($nextCalled);
|
$this->assertTrue($nextCalled);
|
||||||
}
|
}
|
||||||
|
|
@ -1,15 +1,16 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace WellRESTed\Test\Unit\Routing;
|
namespace WellRESTed\Test\Unit\Dispatching;
|
||||||
|
|
||||||
use Prophecy\Argument;
|
use Prophecy\Argument;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use WellRESTed\Routing\Dispatcher;
|
use WellRESTed\Dispatching\Dispatcher;
|
||||||
use WellRESTed\Routing\MiddlewareInterface;
|
use WellRESTed\Routing\MiddlewareInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @covers WellRESTed\Routing\Dispatcher
|
* @covers WellRESTed\Dispatching\Dispatcher
|
||||||
|
* @group dispatching
|
||||||
*/
|
*/
|
||||||
class DispatcherTest extends \PHPUnit_Framework_TestCase
|
class DispatcherTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
@ -75,7 +76,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
|
||||||
/**
|
/**
|
||||||
* @expectedException \InvalidArgumentException
|
* @expectedException \InvalidArgumentException
|
||||||
*/
|
*/
|
||||||
public function testThrowExceptionWhenUnableToDispatch()
|
public function testThrowsExceptionWhenUnableToDispatch()
|
||||||
{
|
{
|
||||||
$middleware = null;
|
$middleware = null;
|
||||||
|
|
||||||
|
|
@ -8,7 +8,7 @@ use WellRESTed\Routing\MethodMap;
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass WellRESTed\Routing\MethodMap
|
* @coversDefaultClass WellRESTed\Routing\MethodMap
|
||||||
* @uses WellRESTed\Routing\MethodMap
|
* @uses WellRESTed\Routing\MethodMap
|
||||||
* @uses WellRESTed\Routing\Dispatcher
|
* @uses WellRESTed\Dispatching\Dispatcher
|
||||||
*/
|
*/
|
||||||
class MethodMapTest extends \PHPUnit_Framework_TestCase
|
class MethodMapTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue