Propagate pathVariablesAttributeName from Server to Router

This commit is contained in:
PJ Dietz 2015-05-19 21:21:58 -04:00
parent dedec4ec4e
commit ab05ca0b40
2 changed files with 30 additions and 14 deletions

View File

@ -12,7 +12,7 @@ use WellRESTed\Routing\Route\RouteInterface;
class Router implements RouterInterface 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; private $pathVariablesAttributeName;
/** @var DispatcherInterface */ /** @var DispatcherInterface */
private $dispatcher; private $dispatcher;
@ -30,17 +30,18 @@ class Router implements RouterInterface
/** /**
* Create a new Router. * Create a new Router.
* *
* When the router matches a route with path variables, it will add each * By default, when a route containg path variables matches, the path
* variable as an attribute on the ServerRequestInterface by default. * variables are stored individually as attributes on the
* ServerRequestInterface.
* *
* When $pathVariablesAttributeName is set, the router will set one * When $pathVariablesAttributeName is set, a single attribute will be
* attribute with the passed name to an array containing all of the path * stored with the name. The value will be an array containing all of the
* variables. * path variables.
* *
* @param DispatcherInterface $dispatcher Instance to use for dispatching * @param DispatcherInterface $dispatcher Instance to use for dispatching
* middleware. * middleware.
* @param string $pathVariablesAttributeName Optionally provide all path * @param string|null $pathVariablesAttributeName Attribute name for
* variables as an array stored with this attribute name * matched path variables. A null value sets attributes directly.
*/ */
public function __construct(DispatcherInterface $dispatcher = null, $pathVariablesAttributeName = null) public function __construct(DispatcherInterface $dispatcher = null, $pathVariablesAttributeName = null)
{ {

View File

@ -20,30 +20,45 @@ class Server
/** @var DispatcherInterface */ /** @var DispatcherInterface */
private $dispatcher; private $dispatcher;
/** @var string ServerRequestInterface attribute name for matched path variables */
private $pathVariablesAttributeName;
/** @var mixed[] List array of middleware */ /** @var mixed[] List array of middleware */
private $stack; 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 * @param array $attributes key-value pairs to register as attributes
* with the server request. * with the server request.
* @param DispatcherInterface $dispatcher Dispatches middleware. If no * @param DispatcherInterface $dispatcher Dispatches middleware. If no
* object is passed, the Server will create a * object is passed, the Server will create a
* WellRESTed\Dispatching\Dispatcher * 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) { if ($attributes === null) {
$attributes = []; $attributes = [];
} }
$this->attributes = $attributes; $this->attributes = $attributes;
if ($dispatcher === null) { if ($dispatcher === null) {
$dispatcher = $this->getDispatcher(); $dispatcher = $this->getDispatcher();
} }
$this->dispatcher = $dispatcher; $this->dispatcher = $dispatcher;
$this->pathVariablesAttributeName = $pathVariablesAttributeName;
$this->stack = []; $this->stack = [];
} }
@ -92,7 +107,7 @@ class Server
*/ */
public function createRouter() public function createRouter()
{ {
return new Router($this->dispatcher); return new Router($this->dispatcher, $this->pathVariablesAttributeName);
} }
/** /**