mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-22 23:53:50 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,38 +1,43 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\Routing;
|
||||
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Knp\Menu\FactoryInterface;
|
||||
use Knp\Menu\ItemInterface;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_merge;
|
||||
use function array_values;
|
||||
|
||||
/**
|
||||
* This class permit to build menu from the routing information
|
||||
* stored in each bundle.
|
||||
*
|
||||
* how to must come here FIXME
|
||||
*
|
||||
* @author julien
|
||||
*/
|
||||
class MenuComposer
|
||||
{
|
||||
|
||||
private RouterInterface $router;
|
||||
private array $localMenuBuilders = [];
|
||||
|
||||
private FactoryInterface $menuFactory;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
private array $localMenuBuilders = [];
|
||||
|
||||
private RouteCollection $routeCollection;
|
||||
|
||||
private RouterInterface $router;
|
||||
|
||||
function __construct(
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
RouterInterface $router,
|
||||
FactoryInterface $menuFactory,
|
||||
TranslatorInterface $translator
|
||||
@@ -42,35 +47,58 @@ class MenuComposer
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
public function addLocalMenuBuilder(LocalMenuBuilderInterface $menuBuilder, $menuId)
|
||||
{
|
||||
$this->routeCollection = $routeCollection;
|
||||
$this->localMenuBuilders[$menuId][] = $menuBuilder;
|
||||
}
|
||||
|
||||
public function getMenuFor($menuId, array $parameters = [])
|
||||
{
|
||||
$routes = $this->getRoutesFor($menuId, $parameters);
|
||||
$menu = $this->menuFactory->createItem($menuId);
|
||||
|
||||
// build menu from routes
|
||||
foreach ($routes as $order => $route) {
|
||||
$menu->addChild($this->translator->trans($route['label']), [
|
||||
'route' => $route['key'],
|
||||
'routeParameters' => $parameters['args'],
|
||||
'order' => $order,
|
||||
])
|
||||
->setExtras([
|
||||
//'icon' => $route['icon'],
|
||||
// sf4 check: commented to avoid error: `An exception has been thrown during the rendering of a template ("Notice: Undefined index: icon").`
|
||||
'order' => $order,
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->hasLocalMenuBuilder($menuId)) {
|
||||
foreach ($this->localMenuBuilders[$menuId] as $builder) {
|
||||
/* @var $builder LocalMenuBuilderInterface */
|
||||
$builder->buildMenu($menuId, $menu, $parameters['args']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->reorderMenu($menu);
|
||||
|
||||
return $menu;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an array of routes added to $menuId,
|
||||
* The array is aimed to build route with MenuTwig
|
||||
* The array is aimed to build route with MenuTwig.
|
||||
*
|
||||
* @param string $menuId
|
||||
* @param array $parameters see https://redmine.champs-libres.coop/issues/179
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRoutesFor($menuId, array $parameters = array())
|
||||
public function getRoutesFor($menuId, array $parameters = [])
|
||||
{
|
||||
$routes = array();
|
||||
$routes = [];
|
||||
$routeCollection = $this->router->getRouteCollection();
|
||||
|
||||
foreach ($routeCollection->all() as $routeKey => $route) {
|
||||
if ($route->hasOption('menus')) {
|
||||
|
||||
if (array_key_exists($menuId, $route->getOption('menus'))) {
|
||||
$route = $route->getOption('menus')[$menuId];
|
||||
|
||||
@@ -89,54 +117,29 @@ class MenuComposer
|
||||
return $routes;
|
||||
}
|
||||
|
||||
public function getMenuFor($menuId, array $parameters = array())
|
||||
/**
|
||||
* Return true if the menu has at least one builder.
|
||||
*
|
||||
* This function is a helper to determine if the method `getMenuFor`
|
||||
* should be used, or `getRouteFor`. The method `getMenuFor` should be used
|
||||
* if the result is true (it **does** exists at least one menu builder.
|
||||
*
|
||||
* @param string $menuId
|
||||
*/
|
||||
public function hasLocalMenuBuilder($menuId): bool
|
||||
{
|
||||
$routes = $this->getRoutesFor($menuId, $parameters);
|
||||
$menu = $this->menuFactory->createItem($menuId);
|
||||
|
||||
// build menu from routes
|
||||
foreach ($routes as $order => $route) {
|
||||
$menu->addChild($this->translator->trans($route['label']), [
|
||||
'route' => $route['key'],
|
||||
'routeParameters' => $parameters['args'],
|
||||
'order' => $order
|
||||
])
|
||||
->setExtras([
|
||||
//'icon' => $route['icon'],
|
||||
// sf4 check: commented to avoid error: `An exception has been thrown during the rendering of a template ("Notice: Undefined index: icon").`
|
||||
'order' => $order
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
if ($this->hasLocalMenuBuilder($menuId)) {
|
||||
foreach ($this->localMenuBuilders[$menuId] as $builder) {
|
||||
/* @var $builder LocalMenuBuilderInterface */
|
||||
$builder->buildMenu($menuId, $menu, $parameters['args']);
|
||||
}
|
||||
}
|
||||
|
||||
$this->reorderMenu($menu);
|
||||
|
||||
return $menu;
|
||||
return \array_key_exists($menuId, $this->localMenuBuilders);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* 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).
|
||||
*/
|
||||
private function resolveOrder($routes, $order)
|
||||
public function setRouteCollection(RouteCollection $routeCollection)
|
||||
{
|
||||
if (isset($routes[$order])) {
|
||||
return $this->resolveOrder($routes, $order + 1);
|
||||
} else {
|
||||
return $order;
|
||||
}
|
||||
$this->routeCollection = $routeCollection;
|
||||
}
|
||||
|
||||
private function reorderMenu(ItemInterface $menu)
|
||||
@@ -147,8 +150,8 @@ class MenuComposer
|
||||
foreach ($menu->getChildren() as $name => $item) {
|
||||
$order = $item->getExtra('order');
|
||||
|
||||
if ($order !== null) {
|
||||
$ordered[$this->resolveOrder($ordered, $order)] = $name;
|
||||
if (null !== $order) {
|
||||
$ordered[$this->resolveOrder($ordered, $order)] = $name;
|
||||
} else {
|
||||
$unordered = $name;
|
||||
}
|
||||
@@ -156,29 +159,26 @@ class MenuComposer
|
||||
|
||||
ksort($ordered);
|
||||
|
||||
$menus = \array_merge(\array_values($ordered), $unordered);
|
||||
$menus = array_merge(array_values($ordered), $unordered);
|
||||
$menu->reorderChildren($menus);
|
||||
}
|
||||
|
||||
|
||||
public function addLocalMenuBuilder(LocalMenuBuilderInterface $menuBuilder, $menuId)
|
||||
{
|
||||
$this->localMenuBuilders[$menuId][] = $menuBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the menu has at least one builder.
|
||||
* 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.
|
||||
*
|
||||
* This function is a helper to determine if the method `getMenuFor`
|
||||
* should be used, or `getRouteFor`. The method `getMenuFor` should be used
|
||||
* if the result is true (it **does** exists at least one menu builder.
|
||||
* @param array $routes the routes previously added
|
||||
* @param int $order
|
||||
*
|
||||
* @param string $menuId
|
||||
* @return bool
|
||||
* @return int
|
||||
*/
|
||||
public function hasLocalMenuBuilder($menuId): bool
|
||||
private function resolveOrder($routes, $order)
|
||||
{
|
||||
return \array_key_exists($menuId, $this->localMenuBuilders);
|
||||
}
|
||||
if (isset($routes[$order])) {
|
||||
return $this->resolveOrder($routes, $order + 1);
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user