0
votes

I'm using symfony and yaml to configure something like this:

foo_menu:
    groups:
        main:
            items: 
               # ...
        side:
            items: 
               # ...
        footer:
            items: 
               # ...

My idea was that each tree defined under foo_menu.groups should have an own service definition. Each service is in instance of a MenuService and has all defined items in it. Each service should be accesable by name, e.g. foo.menu.main, foo.menu.side, ...

The menu building works perfectly, but I don't know how and where to register the service names.

I tried to use a compiler pass, but it won't work:

final class MenuCompilerPass implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        // ...

        foreach ($names as $name) {
            $id = sprintf('foo.menu.group.%s', $name);

            $menu = $factory->getMenu($name);
            $container->set($id, $menu);
        }
    }
}
1
Are you using the full Symfony framework? If so then you will want the compiler pass to create service definitions, not the service itself. Take a look at some of the other bundles for examples. If you are doing something different then some idea of what "won't work" means might help. - Cerad
Thanks, that was the clue! - Christian Gripp

1 Answers

0
votes

Got it working. It was easier than I expected

    foreach ($groups as $name => $items) {
        $id = sprintf('foo.menu.group.%s', $name);
        $definition = new Definition(MyBuilder::class, array($items));

        $container->setDefinition($id, $definition);
    }