Skip to content

Instantly share code, notes, and snippets.

@immutef
Created April 30, 2011 09:35
Show Gist options
  • Save immutef/949555 to your computer and use it in GitHub Desktop.
Save immutef/949555 to your computer and use it in GitHub Desktop.
MenuBundle EventDispatcher integration
<?php
namespace Turtle\AcpBundle\Event;
use Symfony\Component\EventDispatcher\Event;
use Knplabs\Bundle\MenuBundle\Menu;
class ConfigureMenuEvent extends Event
{
/**
* @var Knplabs\Bundle\MenuBundle\Menu
*/
protected $menu;
/**
* @param Knplabs\Bundle\MenuBundle\Menu $menu
* @return void
*/
public function __construct(Menu $menu)
{
$this->menu = $menu;
}
/**
* @return Knplabs\Bundle\MenuBundle\Menu
*/
public function getMenu()
{
return $this->menu;
}
}
<?php
namespace Versus\RankedMatchBundle;
use Symfony\Component\Routing\Router;
use Turtle\AcpBundle\Event\ConfigureMenuEvent;
class ConfigureMenuListener
{
/**
* @var Symfony\Component\Routing\Router
*/
protected $router;
/**
* @param Symfony\Component\Routing\Router $router
* @return void
*/
public function __construct(Router $router)
{
$this->router = $router;
}
/**
* @param Turtle\AcpBundle\Event\ConfigureMenuEvent $event
* @return void
*/
public function onMenuConfigure(ConfigureMenuEvent $event)
{
$menu = $event->getMenu();
$menu->addChild('Matches', $this->router->generate('versus_rankedmatch_acp_matches_index'));
$menu->addChild('Participants', $this->router->generate('versus_rankedmatch_acp_participants_index'));
}
}
<?php
namespace Turtle\AcpBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class ConfigureMenuListenersPass implements CompilerPassInterface
{
/**
* {@inheritDoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition('event_dispatcher')) {
return;
}
$listeners = array();
$definition = $container->getDefinition('event_dispatcher');
foreach ($container->findTaggedServiceIds('menu.listener') as $id => $events) {
foreach ($events as $event) {
$priority = isset($event['priority']) ? $event['priority'] : 0;
if (!isset($event['event'])) {
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "menu.listener" tags.', $id));
}
$definition->addMethodCall('addListenerService', array($event['event'], $id, $priority));
}
}
}
}
<?php
namespace Turtle\AcpBundle;
final class Events
{
const onMenuConfigure = 'onMenuConfigure';
}
<?php
namespace Turtle\AcpBundle\Menu;
use Knplabs\Bundle\MenuBundle\Menu;
use Turtle\AcpBundle\Events,
Turtle\AcpBundle\Event\ConfigureMenuEvent;
use Symfony\Component\EventDispatcher\EventDispatcher,
Symfony\Component\HttpFoundation\Request,
Symfony\Component\Routing\Router;
class MainMenu extends Menu
{
/**
* @param Symfony\Component\EventDispatcher\EventDispatcher $dispatcher
* @param Symfony\Component\HttpFoundation\Request $request
* @param Symfony\Component\Routing\Router $router
* @return void
*/
public function __construct(EventDispatcher $dispatcher, Request $request, Router $router)
{
parent::__construct();
$this->setCurrentUri($request->getRequestUri());
$this->addChild('Dashboard', $router->generate('_acp_dashboard'));
$dispatcher->dispatch(Events::onMenuConfigure, new ConfigureMenuEvent($this));
}
}
parameters:
versus.configure_menu_listener.class: Versus\RankedMatchBundle\ConfigureMenuListener
services:
versus.configure_menu_listener:
class: %versus.configure_menu_listener.class%
arguments: [@router]
tags:
- { name: menu.listener, event: onMenuConfigure }
parameters:
menu.main.class: Turtle\AcpBundle\Menu\MainMenu
services:
menu.main:
class: %menu.main.class%
arguments: [@event_dispatcher, @request, @router]
scope: request
tags:
- { name: menu, alias: main }
<?php
namespace Turtle\AcpBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle,
Symfony\Component\DependencyInjection\ContainerBuilder;
use Turtle\AcpBundle\DependencyInjection\Compiler\ConfigureMenuListenersPass;
class TurtleAcpBundle extends Bundle
{
/**
* {@inheritDoc}
*/
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ConfigureMenuListenersPass());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment