CMS Module agnostic with dependency injection (symfony, dependency-injection, container)

49 Views Asked by At

I'm trying to develop a library in PHP. This library will be used in modules for CMS (Wordpress, Drupal, PrestaShop for example).

Each CMS has different specificities and very special ways of doing things: on PrestaShop, for example, we have access to the symfony dependency injection system.

On Wordpress, this system doesn't exist at all and the norm is to create a Singleton and make a big mess.

I'm looking for a simple architecture that I could use in my library because it's becoming complex and a dependency injection/container system would be very useful.

I've focused on PrestaShop because I can use Symfony DI directly in my library, but I've come across a problem: I need to make database calls (something specific to each module) so I need a service that is defined in the PrestaShop container except that I can't find a "clean" way of retrieving this service in my module container..

In the library, the container is defined like that :

private function createContainer()
{
    $container = new ContainerBuilder();

    $configurationLoader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Ressources/config'));
    $configurationLoader->load('services.yml');

    $container->compile();

    return $container;
}

I found I could technically do this to fix my problem, but it no longer works when I want to use PhpDumper and cache my library container:

private function createContainer(WidgetRepositoryInterface $widgetRepository)
{
    $container = new ContainerBuilder();

    $container->set('repository.widget_repository', $widgetRepository);
    $container->setAlias(WidgetRepositoryInterface::class, 'repository.widget_repository');

    $configurationLoader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Ressources/config'));
    $configurationLoader->load('services.yml');

    $container->compile();

    return $container;
}

I think I'm in a bit of a loop where I don't really know how to merge the containers :

  • I've tried extension systems.
  • I looked at the compiler pass on my library.
  • I tried importing the "service.yml" file for my library from the PrestaShop module files.

Thanks in advance,

0

There are 0 best solutions below