Skip to content

Commit

Permalink
Merge pull request #17 from dmaicher/twig_runtime
Browse files Browse the repository at this point in the history
use twig runtime
  • Loading branch information
dmaicher authored May 18, 2021
2 parents 2f0370a + 40264e2 commit 1e35d39
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 99 deletions.
8 changes: 1 addition & 7 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@ class Configuration implements ConfigurationInterface
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('dama_menu');
// Keep compatibility with symfony/config < 4.2
if (!method_exists($treeBuilder, 'getRootNode')) {
$rootNode = $treeBuilder->root('dama_menu');
} else {
$rootNode = $treeBuilder->getRootNode();
}

$rootNode
$treeBuilder->getRootNode()
->addDefaultsIfNotSet()
->children()
->scalarNode('node_factory')->defaultValue('dama_menu.node_factory')->end()
Expand Down
84 changes: 44 additions & 40 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
services:
_defaults:
public: false

dama_menu.twig.menu_extension:
class: DAMA\MenuBundle\Twig\MenuExtension
arguments: ["@service_container"]
tags:
- { name: twig.extension }

dama_menu.node_factory:
class: DAMA\MenuBundle\Node\NodeFactory

dama_menu.menu_factory:
class: DAMA\MenuBundle\Menu\MenuFactory
arguments: ["@dama_menu.menu_config_provider", "@dama_menu.menu_tree_traverser"]
public: true

dama_menu.menu_config_provider:
class: DAMA\MenuBundle\MenuConfig\MenuConfigProvider
public: true

dama_menu.menu_tree_traverser:
class: DAMA\MenuBundle\MenuTree\MenuTreeTraverser

dama_menu.node_visitor.filter:
class: DAMA\MenuBundle\NodeVisitor\NodeFilter
arguments: ["@security.token_storage", "@security.authorization_checker"]
tags:
- { name: dama_menu.node_visitor, priority: 3 }

dama_menu.node_visitor.activator:
class: DAMA\MenuBundle\NodeVisitor\NodeActivator
arguments: ["@request_stack"]
tags:
- { name: dama_menu.node_visitor, priority: 2 }

dama_menu.node_visitor.node_route_propagator:
class: DAMA\MenuBundle\NodeVisitor\NodeRoutePropagator
tags:
- { name: dama_menu.node_visitor, priority: 1 }
dama_menu.twig.menu_extension:
class: DAMA\MenuBundle\Twig\MenuExtension
arguments: [ '@service_container' ]
tags:
- { name: twig.extension }

dama_menu.twig.menu_runtime:
class: DAMA\MenuBundle\Twig\MenuRuntime
arguments:
- '@twig'
- '@dama_menu.menu_factory'
- '@dama_menu.menu_config_provider'
tags:
- { name: twig.runtime }

dama_menu.node_factory:
class: DAMA\MenuBundle\Node\NodeFactory

dama_menu.menu_factory:
class: DAMA\MenuBundle\Menu\MenuFactory
arguments: [ '@dama_menu.menu_config_provider', '@dama_menu.menu_tree_traverser' ]

dama_menu.menu_config_provider:
class: DAMA\MenuBundle\MenuConfig\MenuConfigProvider

dama_menu.menu_tree_traverser:
class: DAMA\MenuBundle\MenuTree\MenuTreeTraverser

dama_menu.node_visitor.filter:
class: DAMA\MenuBundle\NodeVisitor\NodeFilter
arguments: [ '@security.token_storage', '@security.authorization_checker' ]
tags:
- { name: dama_menu.node_visitor, priority: 3 }

dama_menu.node_visitor.activator:
class: DAMA\MenuBundle\NodeVisitor\NodeActivator
arguments: [ '@request_stack' ]
tags:
- { name: dama_menu.node_visitor, priority: 2 }

dama_menu.node_visitor.node_route_propagator:
class: DAMA\MenuBundle\NodeVisitor\NodeRoutePropagator
tags:
- { name: dama_menu.node_visitor, priority: 1 }
55 changes: 3 additions & 52 deletions src/Twig/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,67 +2,18 @@

namespace DAMA\MenuBundle\Twig;

use DAMA\MenuBundle\Node\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TemplateWrapper;
use Twig\TwigFunction;

class MenuExtension extends AbstractExtension
{
/**
* @var ContainerInterface
*/
private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

public function getFunctions(): array
{
return [
new TwigFunction('dama_menu_render', [$this, 'render'], ['is_safe' => ['html']]),
new TwigFunction('dama_menu_section_label', [$this, 'getMenuSectionLabel']),
new TwigFunction('dama_menu_first_active_child', [$this, 'getFirstActiveChild']),
];
}

public function render(string $name, array $options = []): string
{
$menu = $this->container->get('dama_menu.menu_factory')->create($name);

$defaultOptions = [
'collapse' => false,
'nested' => true,
new TwigFunction('dama_menu_render', [MenuRuntime::class, 'render'], ['is_safe' => ['html']]),
new TwigFunction('dama_menu_section_label', [MenuRuntime::class, 'getMenuSectionLabel']),
new TwigFunction('dama_menu_first_active_child', [MenuRuntime::class, 'getFirstActiveChild']),
];

$finalOptions = array_merge($defaultOptions, $options);
$finalOptions['currentNode'] = $menu;

return $this->getTemplate($name)->renderBlock('render_root', $finalOptions);
}

public function getMenuSectionLabel(string $name): ?string
{
$activeChild = $this->getFirstActiveChild($name);

return $activeChild ? $activeChild->getLabel() : '';
}

public function getFirstActiveChild(string $name): ?Node
{
$menu = $this->container->get('dama_menu.menu_factory')->create($name);

return $menu ? $menu->getFirstActiveChild() : null;
}

protected function getTemplate(string $name): TemplateWrapper
{
$menuConfig = $this->container->get('dama_menu.menu_config_provider')->getMenuConfig($name);

return $this->container->get('twig')->load($menuConfig['twig_template']);
}

public function getName(): string
Expand Down
71 changes: 71 additions & 0 deletions src/Twig/MenuRuntime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace DAMA\MenuBundle\Twig;

use DAMA\MenuBundle\Menu\MenuFactory;
use DAMA\MenuBundle\MenuConfig\MenuConfigProvider;
use DAMA\MenuBundle\Node\Node;
use Twig\Environment;
use Twig\Extension\RuntimeExtensionInterface;
use Twig\TemplateWrapper;

class MenuRuntime implements RuntimeExtensionInterface
{
/**
* @var Environment
*/
private $twig;

/**
* @var MenuFactory
*/
private $menuFactory;

/**
* @var MenuConfigProvider
*/
private $menuConfigProvider;

public function __construct(Environment $twig, MenuFactory $menuFactory, MenuConfigProvider $menuConfigProvider)
{
$this->twig = $twig;
$this->menuFactory = $menuFactory;
$this->menuConfigProvider = $menuConfigProvider;
}

public function render(string $name, array $options = []): string
{
$menu = $this->menuFactory->create($name);

$defaultOptions = [
'collapse' => false,
'nested' => true,
];

$finalOptions = array_merge($defaultOptions, $options);
$finalOptions['currentNode'] = $menu;

return $this->getTemplate($name)->renderBlock('render_root', $finalOptions);
}

public function getMenuSectionLabel(string $name): ?string
{
$activeChild = $this->getFirstActiveChild($name);

return $activeChild ? $activeChild->getLabel() : '';
}

public function getFirstActiveChild(string $name): ?Node
{
$menu = $this->menuFactory->create($name);

return $menu ? $menu->getFirstActiveChild() : null;
}

private function getTemplate(string $name): TemplateWrapper
{
$menuConfig = $this->menuConfigProvider->getMenuConfig($name);

return $this->twig->load($menuConfig['twig_template']);
}
}

0 comments on commit 1e35d39

Please sign in to comment.