Skip to content

Commit 5b7d34e

Browse files
committed
Merge pull request KnpLabs#64 from bamarni/patch-1
[RouterAwareFactory] pushing the route to the extras
2 parents 4187880 + 1bf3040 commit 5b7d34e

File tree

4 files changed

+28
-2
lines changed

4 files changed

+28
-2
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@
3939
"branch-alias": {
4040
"dev-master": "2.0.x-dev"
4141
}
42-
}
42+
},
43+
"minimum-stability": "dev"
4344
}

src/Knp/Menu/Silex/RouterAwareFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public function createItem($name, array $options = array())
2323
$params = isset($options['routeParameters']) ? $options['routeParameters'] : array();
2424
$absolute = isset($options['routeAbsolute']) ? $options['routeAbsolute'] : false;
2525
$options['uri'] = $this->generator->generate($options['route'], $params, $absolute);
26+
27+
// adding the item route to the extras under the 'routes' key (for the Silex RouteVoter)
28+
$options = array_merge_recursive(array('extras' => array('routes' => array($options['route']))), $options);
2629
}
2730

2831
return parent::createItem($name, $options);

tests/Knp/Menu/Tests/Silex/KnpMenuServiceProviderTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public function testRenderCurrentWithTwig()
6868

6969
$request = Request::create('/twig');
7070
$response = $app->handle($request);
71+
$this->assertEquals('<ul class="nav"><li class="current first"><a href="/twig">Home</a></li><li class="last"><a href="http://knplabs.com">KnpLabs</a></li></ul>', $response->getContent());
72+
73+
$app = $this->bootstrapApp();
74+
75+
$request = Request::create('/other-twig');
76+
$response = $app->handle($request);
7177
$this->assertEquals('<ul class="nav"><li class="first"><a href="/twig">Home</a></li><li class="current last"><a href="http://knplabs.com">KnpLabs</a></li></ul>', $response->getContent());
7278
}
7379

@@ -89,7 +95,7 @@ private function bootstrapApp()
8995

9096
$root = $factory->createItem('root', array('childrenAttributes' => array('class' => 'nav')));
9197
$root->addChild('home', array('route' => 'homepage', 'label' => 'Home'));
92-
$root->addChild('KnpLabs', array('uri' => 'http://knplabs.com', 'extras' => array('routes' => 'homepage')));
98+
$root->addChild('KnpLabs', array('uri' => 'http://knplabs.com', 'extras' => array('routes' => 'other_route')));
9399

94100
return $root;
95101
};
@@ -109,6 +115,10 @@ private function bootstrapApp()
109115
return $app['twig']->render('main', array('renderer' => 'twig'));
110116
})->bind('homepage');
111117

118+
$app->get('/other-twig', function (Application $app) {
119+
return $app['twig']->render('main', array('renderer' => 'twig'));
120+
})->bind('other_route');
121+
112122
$app->get('/list', function (Application $app) {
113123
return $app['twig']->render('main', array('renderer' => 'list'));
114124
})->bind('list');

tests/Knp/Menu/Tests/Silex/RouterAwareFactoryTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,16 @@ public function testCreateItemWithAbsoluteRoute()
5151
$item = $factory->createItem('test_item', array('route' => 'homepage', 'routeAbsolute' => true));
5252
$this->assertEquals('http://php.net', $item->getUri());
5353
}
54+
55+
public function testCreateItemAppendsRouteUnderExtras()
56+
{
57+
$generator = $this->getMock('Symfony\Component\Routing\Generator\UrlGeneratorInterface');
58+
$factory = new RouterAwareFactory($generator);
59+
60+
$item = $factory->createItem('test_item', array('route' => 'homepage'));
61+
$this->assertEquals(array('homepage'), $item->getExtra('routes'));
62+
63+
$item = $factory->createItem('test_item', array('route' => 'homepage', 'extras' => array('routes' => array('other_page'))));
64+
$this->assertContains('homepage', $item->getExtra('routes'));
65+
}
5466
}

0 commit comments

Comments
 (0)