I need some help or rather some advice.
I have a core bundle with some general functionality and several additional bundles with more specific functionality.
For example my core bundle is responsible for rendering a main menu for my application but I want other bundles to be able to add menu items only by registering the bundle. (In the end those menu items will be available menu items not necessarily actually active ones.)
I have a solution for that but I am not really sure if it is a good one.
My CoreBundle also contains a CoreBundleInterface:
namespace CoreBundle;
use CoreBundle\Menu\MenuBuilder;
interface CoreBundleInterface
{
public function getMenuItems(MenuBuilder $menuItem);
}
So any other bundle can implement this interface:
namespace AddressBundle;
use CoreBundle\CoreBundleInterface;
use CoreBundle\Menu\MenuBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AddressBundle extends Bundle implements CoreBundleInterface
{
public function getMenuItems(MenuBuilder $menuBuilder)
{
$menuBuilder->addItem(['name' => 'addresses', 'label' => 'Addresses']);
}
}
My CoreBundle has a MenuBuilder class which walks through the registered bundles and calls the interface methods:
namespace CoreBundle\Menu;
use CoreBundle\CoreBundleInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class MenuBuilder
{
private $items;
public function __construct(ContainerInterface $container)
{
$this->addItem(['name' => 'home', 'label' => 'Home']);
$bundles = $container->get('kernel')->getBundles();
foreach ($bundles as $bundle) {
if (is_callable(array($bundle, 'getMenuItems')) && $bundle instanceof CoreBundleInterface) {
call_user_func(array($bundle, 'getMenuItems'), $this);
}
}
}
...
}
I also tried to visualize what structure I want:
Any hint to a better practice to achieve this structure would be very appreciated.