2021-11-30 13:54:58 +01:00

104 lines
2.7 KiB
PHP

<?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\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
/**
* Add the filter 'chill_menu'.
*/
class MenuTwig extends AbstractExtension implements ContainerAwareInterface
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* the default parameters for chillMenu.
*
* @var mixed[]
*/
private $defaultParams = [
'layout' => '@ChillMain/Menu/defaultMenu.html.twig',
'args' => [],
'activeRouteKey' => null,
];
/**
* @var MenuComposer
*/
private $menuComposer;
public function __construct(MenuComposer $menuComposer)
{
$this->menuComposer = $menuComposer;
}
/**
* Render a Menu corresponding to $menuId.
*
* Expected params :
* - args: the arguments to build the path (i.e: if pattern is /something/{bar}, args must contain {'bar': 'foo'}
* - layout: the layout. Absolute path needed (i.e.: ChillXyzBundle:section:foo.html.twig)
* - activeRouteKey : the key active, will render the menu differently.
*
* @deprecated link: see https://redmine.champs-libres.coop/issues/179 for more informations
*
* @param string $menuId
* @param mixed[] $params
*/
public function chillMenu(Environment $env, $menuId, array $params = [])
{
$resolvedParams = array_merge($this->defaultParams, $params);
$layout = $resolvedParams['layout'];
unset($resolvedParams['layout']);
if ($this->menuComposer->hasLocalMenuBuilder($menuId) === false) {
$resolvedParams['routes'] = $this->menuComposer->getRoutesFor($menuId, $resolvedParams);
return $env->render($layout, $resolvedParams);
}
$resolvedParams['menus'] = $this->menuComposer->getMenuFor($menuId, $resolvedParams);
return $env->render($layout, $resolvedParams);
}
public function getFunctions()
{
return [new TwigFunction(
'chill_menu',
[$this, 'chillMenu'],
[
'is_safe' => ['html'],
'needs_environment' => true,
]
),
];
}
public function getName()
{
return 'chill_menu';
}
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
}