Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #77 from jfsimon/route-voter

Added route parameters inclusion to 'RouteVoter' matching.
  • Loading branch information...
commit 8dd8c07f20f19f50174dc487baa7b14f26e6bb8a 2 parents 5c33817 + 6a6b1d6
@docteurklein docteurklein authored
View
5 src/Knp/Menu/Silex/RouterAwareFactory.php
@@ -25,7 +25,10 @@ protected function buildOptions(array $options = array())
$options['uri'] = $this->generator->generate($options['route'], $params, $absolute);
// adding the item route to the extras under the 'routes' key (for the Silex RouteVoter)
- $options = array_merge_recursive(array('extras' => array('routes' => array($options['route']))), $options);
+ $options = array_merge_recursive(array('extras' => array(
+ 'routes' => array($options['route']),
+ 'routesParameters' => array($options['route']=>$params),
+ )), $options);
}
return parent::buildOptions($options);
View
15 src/Knp/Menu/Silex/Voter/RouteVoter.php
@@ -33,7 +33,20 @@ public function matchItem(ItemInterface $item)
}
$routes = (array) $item->getExtra('routes', array());
- if (in_array($route, $routes)) {
+ $parameters = (array) $item->getExtra('routesParameters', array());
+ foreach ($routes as $testedRoute) {
+ if ($route !== $testedRoute) {
+ break;
+ }
+
+ if (isset($parameters[$route])) {
+ foreach ($parameters[$route] as $name => $value) {
+ if ($this->request->attributes->get($name) !== $value) {
+ return null;
+ }
+ }
+ }
+
return true;
}
View
51 tests/Knp/Menu/Tests/Silex/Voter/RouteVoterTest.php
@@ -27,21 +27,33 @@ public function testMatchingWithoutRequest()
/**
* @param string $route
+ * @param array $parameters
* @param string|array $itemRoutes
+ * @param array $itemsRoutesParameters
* @param boolean $expected
*
* @dataProvider provideData
*/
- public function testMatching($route, $itemRoutes, $expected)
+ public function testMatching($route, array $parameters, $itemRoutes, array $itemsRoutesParameters, $expected)
{
$item = $this->getMock('Knp\Menu\ItemInterface');
$item->expects($this->any())
->method('getExtra')
- ->with($this->equalTo('routes'))
- ->will($this->returnValue($itemRoutes));
+ ->with($this->logicalOr($this->equalTo('routes'), $this->equalTo('routesParameters')))
+ ->will($this->returnCallback(function ($parameter) use ($itemRoutes, $itemsRoutesParameters) {
+ switch ($parameter) {
+ case 'routes':
+ return $itemRoutes;
+ case 'routesParameters':
+ return $itemsRoutesParameters;
+ }
+ }));
$request = new Request();
$request->attributes->set('_route', $route);
+ foreach ($parameters as $name => $value) {
+ $request->attributes->set($name, $value);
+ }
$voter = new RouteVoter();
$voter->setRequest($request);
@@ -52,12 +64,33 @@ public function testMatching($route, $itemRoutes, $expected)
public function provideData()
{
return array(
- 'no request route' => array(null, 'foo', null),
- 'no item route' => array('foo', null, null),
- 'same single route' => array('foo', 'foo', true),
- 'different single route' => array('foo', 'bar', null),
- 'matching mutiple routes' => array('foo', array('foo', 'baz'), true),
- 'different single route' => array('foo', array('bar', 'baz'), null),
+ 'no request route' => array(null, array(), 'foo', array(), null),
+ 'no item route' => array('foo', array(), null, array(), null),
+ 'same single route' => array('foo', array(), 'foo', array(), true),
+ 'different single route' => array('foo', array(), 'bar', array(), null),
+ 'matching mutiple routes' => array('foo', array(), array('foo', 'baz'), array(), true),
+ 'different multiple routes' => array('foo', array(), array('bar', 'baz'), array(), null),
+
+ 'same single route with different parameters' => array(
+ 'foo', array('1' => 'bar'),
+ 'foo', array('foo' => array('1' => 'baz')),
+ null
+ ),
+ 'same single route with same parameters' => array(
+ 'foo', array('1' => 'bar'),
+ 'foo', array('foo' => array('1' => 'bar')),
+ true
+ ),
+ 'same single route with additional parameters' => array(
+ 'foo', array('1' => 'bar'),
+ 'foo', array('foo' => array('1' => 'bar', '2' => 'baz')),
+ null
+ ),
+ 'same single route with less parameters' => array(
+ 'foo', array('1' => 'bar', '2' => 'baz'),
+ 'foo', array('foo' => array('1' => 'bar')),
+ true
+ ),
);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.