Skip to content

Commit

Permalink
Add methods to get/set middleware on a route.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jul 16, 2017
1 parent 1838d7a commit 9d9e566
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/Routing/Route/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ class Route
*/
protected $_extensions = [];

/**
* List of middleware that should be applied.
*
* @var array
*/
protected $middleware = [];

/**
* Valid HTTP methods.
*
Expand All @@ -100,6 +107,7 @@ class Route
* ### Options
*
* - `_ext` - Defines the extensions used for this route.
* - `_middleware` - Define the middleware names for this route.
* - `pass` - Copies the listed parameters into params['pass'].
* - `_host` - Define the host name pattern if you want this route to only match
* specific host names. You can use `.*` and to create wildcard subdomains/hosts
Expand All @@ -118,8 +126,10 @@ public function __construct($template, $defaults = [], array $options = [])
unset($defaults['[method]']);
}
$this->defaults = (array)$defaults;
$this->options = $options + ['_ext' => []];
$this->options = $options + ['_ext' => [], '_middleware' => []];
$this->setExtensions((array)$this->options['_ext']);
$this->setMiddleware((array)$this->options['_middleware']);
unset($this->options['_middleware']);
}

/**
Expand Down Expand Up @@ -826,6 +836,30 @@ public function staticPath()
return $this->template;
}

/**
* Set the names of the middleware that should be applied to this route.
*
* @param array $middleware The list of middleware names to apply to this route.
* Middleware names will not be checked until the route is matched.
* @return $this
*/
public function setMiddleware(array $middleware)
{
$this->middleware = $middleware;

return $this;
}

/**
* Get the names of the middleware that should be applied to this route.
*
* @return array
*/
public function getMiddleware()
{
return $this->middleware;
}

/**
* Set state magic method to support var_export
*
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/Routing/Route/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ public function testConstruction()
$this->assertFalse($route->compiled());
}

/**
* Test set middleware in the constructor
*
* @return void
*/
public function testConstructorSetMiddleware()
{
$route = new Route('/:controller/:action/*', [], ['_middleware' => ['auth', 'cookie']]);
$this->assertSame(['auth', 'cookie'], $route->getMiddleware());
}

/**
* Test Route compiling.
*
Expand Down Expand Up @@ -1642,4 +1653,17 @@ public function testSetPersist()
$this->assertSame($result, $route, 'Should return this');
$this->assertEquals(['date'], $route->options['persist']);
}

/**
* Test setting/getting middleware.
*
* @return void
*/
public function testSetMiddleware()
{
$route = new Route('/reviews/:date/:id', ['controller' => 'Reviews', 'action' => 'view']);
$result = $route->setMiddleware(['auth', 'cookie']);
$this->assertSame($result, $route);
$this->assertSame(['auth', 'cookie'], $route->getMiddleware());
}
}

0 comments on commit 9d9e566

Please sign in to comment.