Skip to content

Commit

Permalink
Added ability to define attributes on the route. These will be overwr…
Browse files Browse the repository at this point in the history
…itten by any matching route arguments.
  • Loading branch information
AlexStansfield committed Jul 10, 2015
1 parent 50de533 commit 87d29d7
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 0 deletions.
24 changes: 24 additions & 0 deletions Slim/Interfaces/RouteInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
*/
interface RouteInterface
{
/**
* Get route attributes
*
* @return array
*/
public function getAttributes();

/**
* Get route name
*
Expand All @@ -34,6 +41,23 @@ public function getName();
*/
public function getPattern();

/**
* Set a route attribute
*
* @param string $name
* @param string $value
* @return static
*/
public function setAttribute($name, $value);

/**
* Set route attributes
*
* @param array $attributes
* @return static
*/
public function setAttributes(array $attributes);

/**
* Set route name
*
Expand Down
68 changes: 68 additions & 0 deletions Slim/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ class Route extends Routable implements RouteInterface
*/
protected $methods = [];

/**
* Route default attributes
*
* @var array
*/
protected $attributes = [];

/**
* Route name
*
Expand Down Expand Up @@ -139,6 +146,16 @@ public function getGroups()
return $this->groups;
}

/**
* Get route attributes
*
* @return array
*/
public function getAttributes()
{
return $this->attributes;
}

/**
* Get route name
*
Expand Down Expand Up @@ -188,6 +205,33 @@ protected function setCallable(callable $callable)
$this->callable = $callable;
}

/**
* Set a route attribute
*
* @param string $name
* @param string $value
* @return $this
*/
public function setAttribute($name, $value)
{
$this->attributes[$name] = $value;

return $this;
}

/**
* Set route attributes
*
* @param array $attributes
* @return $this
*/
public function setAttributes(array $attributes)
{
$this->attributes = $attributes;

return $this;
}

/**
* Set route name
*
Expand All @@ -205,6 +249,25 @@ public function setName($name)
return $this;
}

/**
* Add any attributes to the request that were not set earlier
*
* @param ServerRequestInterface $request
* @return ServerRequestInterface
*/
protected function addAttributesToRequest(ServerRequestInterface $request)
{
// Get attributes to set
$attributes = array_diff_key($this->getAttributes(), $request->getAttributes());

// Add the attributes to the route
foreach ($attributes as $k => $v) {
$request = $request->withAttribute($k, $v);
}

return $request;
}

/********************************************************************************
* Route Runner
*******************************************************************************/
Expand All @@ -223,6 +286,11 @@ public function setName($name)
*/
public function run(ServerRequestInterface $request, ResponseInterface $response)
{
// Set any Default Attributes on the request
if (0 !== count($this->getAttributes())) {
$request = $this->addAttributesToRequest($request);
}

// Traverse middleware stack and fetch updated response
return $this->callMiddlewareStack($request, $response);
}
Expand Down
5 changes: 5 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
return $response;
});

$app->get('/hello/world', function ($request, $response, $args) {
$response->write("Hello " . $args['extra']);
return $response;
})->setAttribute('extra', 'world');

$app->get('/hello/{name}', function ($request, $response, $args) {
$response->write("Hello, " . $args['name']);
return $response;
Expand Down
83 changes: 83 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,61 @@ public function testInvokeWithMatchingRoute()
$this->assertEquals('Hello', (string)$res->getBody());
}

public function testInvokeWithMatchingRouteWithAttribute()
{
$app = new App();
$app->get('/foo/bar', function ($req, $res, $args) {
return $res->write("Hello {$args['attribute']}");
})->setAttribute('attribute', 'world!');

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke app
$resOut = $app($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut);
$this->assertEquals('Hello world!', (string)$res->getBody());
}

public function testInvokeWithMatchingRouteWithAttributes()
{
$app = new App();
$app->get('/foo/bar', function ($req, $res, $args) {
return $res->write("Hello {$args['attribute1']} {$args['attribute2']}");
})->setAttributes(['attribute1' => 'there', 'attribute2' => 'world!']);

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/bar',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke app
$resOut = $app($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut);
$this->assertEquals('Hello there world!', (string)$res->getBody());
}

public function testInvokeWithMatchingRouteWithNamedParameter()
{
Expand Down Expand Up @@ -294,6 +349,34 @@ public function testInvokeWithMatchingRouteWithNamedParameterRequestResponseArgS
$this->assertEquals('Hello test!', (string)$res->getBody());
}

public function testInvokeWithMatchingRouteWithNamedParameterOverwritesAttribute()
{
$app = new App();
$app->get('/foo/{name}', function ($req, $res, $args) {
return $res->write("Hello {$args['extra']} {$args['name']}");
})->setAttributes(['extra' => 'there', 'name' => 'world!']);

// Prepare request and response objects
$env = Environment::mock([
'SCRIPT_NAME' => '/index.php',
'REQUEST_URI' => '/foo/test!',
'REQUEST_METHOD' => 'GET',
]);
$uri = Uri::createFromEnvironment($env);
$headers = Headers::createFromEnvironment($env);
$cookies = [];
$serverParams = $env->all();
$body = new Body(fopen('php://temp', 'r+'));
$req = new Request('GET', $uri, $headers, $cookies, $serverParams, $body);
$res = new Response();

// Invoke app
$resOut = $app($req, $res);

$this->assertInstanceOf('\Psr\Http\Message\ResponseInterface', $resOut);
$this->assertEquals('Hello there test!', (string)$res->getBody());
}

public function testInvokeWithoutMatchingRoute()
{
$app = new App();
Expand Down
17 changes: 17 additions & 0 deletions tests/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,23 @@ public function testAddMiddleware()
$this->assertCount(2, $prop->getValue($route));
}

public function testSetAttribute()
{
$route = $this->routeFactory();
$this->assertEquals($route, $route->setAttribute('attribute', 'value'));
$this->assertEquals(['attribute' => 'value'], $route->getAttributes());
$this->assertEquals($route, $route->setAttribute('attribute', 'value2'));
$this->assertEquals($route, $route->setAttribute('attribute2', 'value3'));
$this->assertEquals(['attribute' => 'value2', 'attribute2' => 'value3'], $route->getAttributes());
}

public function testSetAttributes()
{
$route = $this->routeFactory();
$this->assertEquals($route, $route->setAttributes(['attribute' => 'value']));
$this->assertEquals(['attribute' => 'value'], $route->getAttributes());
}

public function testSetName()
{
$route = $this->routeFactory();
Expand Down

0 comments on commit 87d29d7

Please sign in to comment.