mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
add service_container to menuComposer instead of router to avoid circular references
This commit is contained in:
parent
85972ca4fe
commit
39477cad14
@ -6,7 +6,7 @@ services:
|
||||
class: CL\Chill\MainBundle\Routing\MenuComposer
|
||||
#must be set in function to avoid circular reference with chill.main.twig.chill_menu
|
||||
calls:
|
||||
- [setRoute, ["@router"]]
|
||||
- [setContainer, ["@service_container"]]
|
||||
|
||||
chill.main.twig.chill_menu:
|
||||
class: CL\Chill\MainBundle\Routing\MenuTwig
|
||||
|
@ -4,6 +4,8 @@ namespace CL\Chill\MainBundle\Routing;
|
||||
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* This class permit to build menu from the routing information
|
||||
@ -13,25 +15,27 @@ use Symfony\Component\Routing\RouteCollection;
|
||||
*
|
||||
* @author julien
|
||||
*/
|
||||
class MenuComposer
|
||||
class MenuComposer implements ContainerAwareInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Routing\RouteCollection;
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $routeCollection;
|
||||
private $container;
|
||||
|
||||
/**
|
||||
*
|
||||
* @internal must be set in function instead of controller to avoid circular reference
|
||||
* with MenuTwig
|
||||
* @param RouterInterface $router
|
||||
* @internal using the service router in container cause circular references
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function setRoute(RouterInterface $router)
|
||||
public function setContainer(ContainerInterface $container = null)
|
||||
{
|
||||
if (NULL === $container) {
|
||||
throw new LogicException('container should not be null');
|
||||
}
|
||||
//see remark in MenuComposer::setRouteCollection
|
||||
$this->routeCollection = $router->getRouteCollection();
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -58,8 +62,9 @@ class MenuComposer
|
||||
public function getRoutesFor($menuId, array $parameters = array())
|
||||
{
|
||||
$routes = array();
|
||||
$routeCollection = $this->container->get('router')->getRouteCollection();
|
||||
|
||||
foreach ($this->routeCollection->all() as $routeKey => $route) {
|
||||
foreach ($routeCollection->all() as $routeKey => $route) {
|
||||
if ($route->hasOption('menus')) {
|
||||
if (array_key_exists($menuId, $route->getOption('menus'))) {
|
||||
$route = $route->getOption('menus')[$menuId];
|
||||
|
Loading…
x
Reference in New Issue
Block a user