diff --git a/src/Server.php b/src/Server.php index db81f9b..2bfae67 100644 --- a/src/Server.php +++ b/src/Server.php @@ -15,6 +15,9 @@ use WellRESTed\Transmission\TransmitterInterface; class Server implements DispatchStackInterface { + /** @var array */ + private $attributes; + /** @var DispatcherInterface */ private $dispatcher; @@ -24,15 +27,24 @@ class Server implements DispatchStackInterface /** * Create a new router. * + * @param array $attributes key-value pairs to register as attributes + * with the server request. * @param DispatcherInterface $dispatcher Dispatches middleware. If no * object is passed, the Server will create a * WellRESTed\Dispatching\Dispatcher */ - public function __construct(DispatcherInterface $dispatcher = null) + public function __construct(array $attributes = null, DispatcherInterface $dispatcher = null) { - if ($dispatcher === null) { - $this->dispatcher = $this->getDispatcher(); + if ($attributes === null) { + $attributes = []; } + $this->attributes = $attributes; + + if ($dispatcher === null) { + $dispatcher = $this->getDispatcher(); + } + $this->dispatcher = $dispatcher; + $this->stack = []; } @@ -104,6 +116,9 @@ class Server implements DispatchStackInterface if ($request === null) { $request = $this->getRequest(); } + foreach ($this->attributes as $name => $value) { + $request = $request->withAttribute($name, $value); + } if ($response === null) { $response = $this->getResponse(); } diff --git a/test/tests/unit/ServerTest.php b/test/tests/unit/ServerTest.php index ad22471..2308381 100644 --- a/test/tests/unit/ServerTest.php +++ b/test/tests/unit/ServerTest.php @@ -21,6 +21,7 @@ class ServerTest extends \PHPUnit_Framework_TestCase { parent::setUp(); $this->request = $this->prophesize('Psr\Http\Message\ServerRequestInterface'); + $this->request->withAttribute(Argument::cetera())->willReturn($this->request->reveal()); $this->response = $this->prophesize('Psr\Http\Message\ResponseInterface'); $this->transmitter = $this->prophesize('WellRESTed\Transmission\TransmitterInterface'); $this->transmitter->transmit(Argument::cetera())->willReturn(); @@ -169,4 +170,22 @@ class ServerTest extends \PHPUnit_Framework_TestCase $next )->shouldHaveBeenCalled(); } + + // ------------------------------------------------------------------------ + // Attributes + + /** + * @covers ::respond + */ + public function testAddsAttributesToRequest() + { + $attributes = [ + "name" => "value" + ]; + + $this->server->__construct($attributes); + $this->server->respond(); + $this->request->withAttribute("name", "value")->shouldHaveBeenCalled(); + } + }