Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 1.44 KB

005_dependency_provider.md

File metadata and controls

48 lines (39 loc) · 1.44 KB

Back to the index

Dependency Provider

This is the place where you can define the dependencies that a particular module has with other modules.

In this example you can see that in the Calculator we have a service (Adder) which needs the AnotherModuleFacade as a dependency. In this case, we can define the dependency inside the DependencyProvider.

# src/Calculator/Factory.php
final class Factory extends AbstractFactory
{
    public function createAdder(): AdderInterface
    {
        return new Adder(
            $this->getAnotherModuleFacade()
        );
    }
    
    private function getAnotherModuleFacade(): AnotherModuleFacade
    {
        return $this->getProvidedDependency(DependencyProvider::FACADE_ANOTHER_MODULE);
    }
}
# src/Calculator/DependencyProvider.php
final class DependencyProvider extends AbstractDependencyProvider
{
    public const FACADE_ANOTHER_MODULE = 'FACADE_ANOTHER_MODULE';

    public function provideModuleDependencies(Container $container): void
    {
        $this->addFacadeAnotherModule($container);
    }

    private function addFacadeAnotherModule(Container $container): void
    {
        $container->set(self::FACADE_ANOTHER_MODULE, function (Container $container): AnotherModuleFacade {
            return $container->getLocator()->get(AnotherModuleFacade::class);
        });
    }
}

<< Config | Code Generator >>