Dependency injection

Shopware DIC

The B2B-Suite registers with the DIC from Shopware. Be sure you are familiar with the basic usage patterns and practices. Especially service decoration is an equally important extension point.

Dependency Injection Extension B2B

The B2B-Suite provides an abstract DependencyInjectionConfiguration class, that is used throughout the Suite as an initializer of DI-Contents across all components.

<?php

namespace Shopware\B2B\Common;

abstract class DependencyInjectionConfiguration
{
    /**
     * @return string[] array of service xml files
     */
    abstract public function getServiceFiles(): array;

    /**
     * @return Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface[]
     */
    abstract public function getCompilerPasses(): array;

    /**
     * @return DependencyInjectionConfiguration[] child components required by this component
     */
    abstract public function getDependingConfigurations(): array;
}

Every macro layer of every component defines its own dependencies. That way you can just require the up most components you want to use and every other dependency is injected automatically.

For example this code will enable the contact component your own plugin.

<?php

namespace MyB2bPlugin;

use Shopware\B2B\Common\B2BContainerBuilder;
use Shopware\Components\Plugin;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class MyB2bPlugin extends Plugin
{
    [...]

    /**
     * {@inheritdoc}
     */
    public function build(ContainerBuilder $container)
    {
        $containerBuilder = B2BContainerBuilder::create();
        $containerBuilder->addConfiguration(new Shopware\B2B\Contact\Framework\DependencyInjection\ContactFrameworkConfiguration());
        $containerBuilder->registerConfigurations($container);
    }
}

Tags

Additionally the B2B-Suite makes heavy use of service tags as a more modern replacement for collect events. They are used to help you extend central B2B services with custom logic. Please take a look at the example plugins and there usage of that extension mechanism. Be sure you know the basics.

Top