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
{
/** @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)
{

View File

@ -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);
}
/**