From ab05ca0b409471f84a12bf5e8cf12e294f578b31 Mon Sep 17 00:00:00 2001 From: PJ Dietz Date: Tue, 19 May 2015 21:21:58 -0400 Subject: [PATCH] Propagate pathVariablesAttributeName from Server to Router --- src/Routing/Router.php | 17 +++++++++-------- src/Server.php | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 01aa911..9ebf8e3 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -12,7 +12,7 @@ use WellRESTed\Routing\Route\RouteInterface; class Router implements RouterInterface { - /** @var string Key to ServerRequestInterface attribute for matched path variables */ + /** @var string ServerRequestInterface attribute name for matched path variables */ private $pathVariablesAttributeName; /** @var DispatcherInterface */ private $dispatcher; @@ -30,17 +30,18 @@ class Router implements RouterInterface /** * Create a new Router. * - * When the router matches a route with path variables, it will add each - * variable as an attribute on the ServerRequestInterface by default. + * By default, when a route containg path variables matches, the path + * variables are stored individually as attributes on the + * ServerRequestInterface. * - * When $pathVariablesAttributeName is set, the router will set one - * attribute with the passed name to an array containing all of the path - * variables. + * When $pathVariablesAttributeName is set, a single attribute will be + * stored with the name. The value will be an array containing all of the + * path variables. * * @param DispatcherInterface $dispatcher Instance to use for dispatching * middleware. - * @param string $pathVariablesAttributeName Optionally provide all path - * variables as an array stored with this attribute name + * @param string|null $pathVariablesAttributeName Attribute name for + * matched path variables. A null value sets attributes directly. */ public function __construct(DispatcherInterface $dispatcher = null, $pathVariablesAttributeName = null) { diff --git a/src/Server.php b/src/Server.php index 624945d..b26233c 100644 --- a/src/Server.php +++ b/src/Server.php @@ -20,30 +20,45 @@ class Server /** @var DispatcherInterface */ private $dispatcher; + /** @var string ServerRequestInterface attribute name for matched path variables */ + private $pathVariablesAttributeName; + /** @var mixed[] List array of middleware */ private $stack; /** - * Create a new router. + * Create a new server. + * + * By default, when a route containg path variables matches, the path + * variables are stored individually as attributes on the + * ServerRequestInterface. + * + * When $pathVariablesAttributeName is set, a single attribute will be + * stored with the name. The value will be an array containing all of the + * path variables. * * @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 + * @param string|null $pathVariablesAttributeName Attribute name for + * matched path variables. A null value sets attributes directly. */ - public function __construct(array $attributes = null, DispatcherInterface $dispatcher = null) - { + public function __construct( + array $attributes = null, + DispatcherInterface $dispatcher = null, + $pathVariablesAttributeName = null + ) { if ($attributes === null) { $attributes = []; } $this->attributes = $attributes; - if ($dispatcher === null) { $dispatcher = $this->getDispatcher(); } $this->dispatcher = $dispatcher; - + $this->pathVariablesAttributeName = $pathVariablesAttributeName; $this->stack = []; } @@ -92,7 +107,7 @@ class Server */ public function createRouter() { - return new Router($this->dispatcher); + return new Router($this->dispatcher, $this->pathVariablesAttributeName); } /**