diff --git a/DependencyInjection/Services/MenuComposer.php b/DependencyInjection/Services/MenuComposer.php index 895c82740..8b50b9eac 100644 --- a/DependencyInjection/Services/MenuComposer.php +++ b/DependencyInjection/Services/MenuComposer.php @@ -3,6 +3,7 @@ namespace CL\Chill\MainBundle\DependencyInjection\Services; use Symfony\Component\Routing\RouterInterface; +use Symfony\Component\Routing\RouteCollection; /** * This class permit to build menu from the routing information @@ -12,41 +13,64 @@ use Symfony\Component\Routing\RouterInterface; * * @author julien */ -class MenuComposer { - +class MenuComposer +{ + /** * * @var \Symfony\Component\Routing\RouteCollection; */ private $routeCollection; - - - public function __construct(RouterInterface $router) { - $this->routeCollection = $router->getRouteCollection(); + + public function __construct(RouterInterface $router) + { + //see remark in MenuComposer::setRouteCollection + $this->setRouteCollection($router->getRouteCollection()); } - - public function getRoutesFor($menuId, array $parameters = array()) { + + /** + * Set the route Collection + * This function is needed for testing purpose: routeCollection is not + * available as a service (RouterInterface is provided as a service and + * added to this class as paramater in __construct) + * + * @param RouteCollection $routeCollection + */ + public function setRouteCollection(RouteCollection $routeCollection) + { + $this->routeCollection = $routeCollection; + } + + public function getRoutesFor($menuId, array $parameters = array()) + { $routes = array(); - + foreach ($this->routeCollection->all() as $key => $route) { - if ($route->getOption('menu') === $menuId) { - $a['route'] = $key; - $a['label'] = $route->getOption('label'); - - if ($route->hasOption('helper')) { - $a['helper'] = $route->getOption('helper'); - } else { - $a['helper'] = ''; + if ($route->hasOption('menus')) { + foreach ($route->getOption('menus') as $key => $params) { + if ($menuId === $key) { + $route = array(); + $route['route'] = $key; + $route['label'] = $params['label']; + $route['helper'] = + (isset($params['helper'])) ? $params['helper'] : ''; + + //add route to the routes array, avoiding duplicate 'order' + // to erase previously added + if (!isset($routes[$params['order']])) { + $routes[$params['order']] = $route; + } else { + $routes[$params['order'] + 1 ] = $route; + } + + } } - - $routes[$route->getOption('order')] = $a; } } - + ksort($routes); - + return $routes; - } - + } diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index feeeb5d8a..cd3d3c3aa 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -4,6 +4,11 @@ root: _controller: FrameworkBundle:Redirect:urlRedirect path: /hello permanent: true + options: + menus: + main: + order: 10 + label: homepage cl_chill_main_homepage: pattern: /hello @@ -12,3 +17,30 @@ cl_chill_main_homepage: chill_main_admin_central: pattern: /admin defaults: { _controller: CLChillMainBundle:Admin:index } + options: + menus: + main: + order: 20 + label: homepage + + +chill_main_dummy_0: + pattern: /dummy + defaults: { _controller: CLChillMainBundle:Default:index } + options: + menus: + dummy0: + order: 50 + label: 'test dummy 0' + dummy1: + order: 50 + label: test dummy 1 + +chill_main_dummy_1: + pattern: /dummy1 + defaults: { _controller: CLChillMainBundle:Default:index } + options: + menus: + dummy0: + order: 51 + label: 'test dummy 1' diff --git a/Tests/Services/MenuComposer.php b/Tests/Services/MenuComposer.php new file mode 100644 index 000000000..66be61954 --- /dev/null +++ b/Tests/Services/MenuComposer.php @@ -0,0 +1,68 @@ + + */ +class MenuComposer extends KernelTestCase +{ + + /** + * + * @var \Symfony\Bundle\FrameworkBundle\Routing\DelegatingLoader; + */ + private $loader; + + /** + * + * @var \CL\Chill\MainBundle\DependencyInjection\Services\MenuComposer; + */ + private $menuComposer; + + public function setUp() + { + self::bootKernel(); + $this->loader = static::$kernel->getContainer() + ->get('routing.loader'); + $this->menuComposer = static::$kernel->getContainer() + ->get('chill_main.menu_composer'); + } + + public function testMenuComposer() + { + $collection = new RouteCollection(); + $collection->add($this->loader->load(__DIR__.'dummy_menu_composer.yml', 'yaml')); + + $routes = $this->menuComposer->getRoutesFor('dummy0'); + + $this->assertInternalType('array', $routes); + $this->assertCount(2, $routes); + //check that the keys are sorted + $orders = array_keys($routes); + foreach ($orders as $key => $order){ + if (array_key_exists($key + 1, $orders)) { + $this->assertGreaterThan($order, $orders[$key + 1], + 'Failing to assert that routes are ordered'); + } + } + $this->assertSame(array( + 50 => array( + 'key' => 'chill_main_dummy_0', + 'label' => 'test0', + ), + 51 => array( + 'key' => 'chill_main_dummy_1', + 'label' => 'test1', + 'helper'=> 'great helper' + ) + + ), $routes); + } +} diff --git a/Tests/Services/dummy_menu_composer.yml b/Tests/Services/dummy_menu_composer.yml new file mode 100644 index 000000000..655fff471 --- /dev/null +++ b/Tests/Services/dummy_menu_composer.yml @@ -0,0 +1,21 @@ +chill_main_dummy_0: + pattern: /dummy + defaults: { _controller: CLChillMainBundle:Default:index } + options: + menus: + dummy0: + order: 50 + label: 'test0' + dummy1: + order: 50 + label: test dummy 1 + +chill_main_dummy_1: + pattern: /dummy1 + defaults: { _controller: CLChillMainBundle:Default:index } + options: + menus: + dummy0: + order: 50 + label: 'test1' + helper: 'great helper' \ No newline at end of file diff --git a/Tests/bootstrap.php b/Tests/bootstrap.php new file mode 100644 index 000000000..9211155e5 --- /dev/null +++ b/Tests/bootstrap.php @@ -0,0 +1,8 @@ + + + + + + ./Tests + + + + + ./ + + ./Resources + ./Tests + ./vendor + + + +