update MenuComposer to fit with #179 spec

This commit is contained in:
Julien Fastré 2014-10-11 14:01:14 +02:00
parent 2e8fa3b064
commit 2367a78845
3 changed files with 54 additions and 25 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
namespace CL\Chill\MainBundle\DependencyInjection\Services; namespace CL\Chill\MainBundle\Routing;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
@ -47,23 +47,15 @@ class MenuComposer
foreach ($this->routeCollection->all() as $routeKey => $route) { foreach ($this->routeCollection->all() as $routeKey => $route) {
if ($route->hasOption('menus')) { if ($route->hasOption('menus')) {
foreach ($route->getOption('menus') as $menuKey => $params) { if (array_key_exists($menuId, $route->getOption('menus'))) {
if ($menuId === $menuKey) { $route = $route->getOption('menus')[$menuId];
$route = array();
$route['route'] = $routeKey; $route['key'] = $routeKey;
$route['label'] = $params['label'];
$route['helper'] = $order = $this->resolveOrder($routes, $route['order']);
(isset($params['helper'])) ? $params['helper'] : null; //we do not want to duplicate order information
unset($route['order']);
//add route to the routes array, avoiding duplicate 'order' $routes[$order] = $route;
// to erase previously added
if (!isset($routes[$params['order']])) {
$routes[$params['order']] = $route;
} else {
$routes[$params['order'] + 1 ] = $route;
}
}
} }
} }
} }
@ -72,5 +64,22 @@ class MenuComposer
return $routes; return $routes;
} }
/**
* recursive function to resolve the order of a array of routes.
* If the order chosen in routing.yml is already in used, find the
* first next order available.
*
* @param array $routes the routes previously added
* @param int $order
* @return int
*/
private function resolveOrder($routes, $order){
if (isset($routes[$order])) {
return $this->resolveOrder($routes, $order + 1);
} else {
return $order;
}
}
} }

View File

@ -8,7 +8,8 @@ chill_main_dummy_0:
menus: menus:
dummy0: dummy0:
order: 50 order: 50
label: 'test0' label: test0
otherkey: othervalue
dummy1: dummy1:
order: 50 order: 50
label: test dummy 1 label: test dummy 1

View File

@ -49,18 +49,37 @@ class MenuComposerTest extends KernelTestCase
'Failing to assert that routes are ordered'); 'Failing to assert that routes are ordered');
} }
} }
$this->assertSame(array(
//check that the array are identical, order is not important :
$expected = array(
50 => array( 50 => array(
'route' => 'chill_main_dummy_0', 'key' => 'chill_main_dummy_0',
'label' => 'test0', 'label' => 'test0',
'helper'=> null 'otherkey' => 'othervalue'
), ),
51 => array( 51 => array(
'route' => 'chill_main_dummy_1', 'key' => 'chill_main_dummy_1',
'label' => 'test1', 'label' => 'test1',
'helper'=> 'great helper' 'helper'=> 'great helper'
) ));
foreach ($expected as $order => $route ){
), $routes); }
//compare arrays
foreach($expected as $order => $route) {
//check the key are the one expected
$this->assertTrue(isset($routes[$order]));
if (isset($routes[$order])){ #avoid an exception if routes with order does not exists
//sort arrays. Order matters for phpunit::assertSame
ksort($route);
ksort($routes[$order]);
$this->assertSame($route, $routes[$order]);
}
}
} }
} }