How to Manage Common Dependencies with Parent Services
Warning: You are browsing the documentation for Symfony 2.x, which is no longer maintained.
Read the updated version of this page for Symfony 7.1 (the current stable version).
As you add more functionality to your application, you may well start to
have related classes that share some of the same dependencies. For example,
you may have multiple repository classes which need the
doctrine.orm.entity_manager
service and an optional logger
service:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// src/AppBundle/Repository/BaseDoctrineRepository.php
namespace AppBundle\Repository;
// ...
abstract class BaseDoctrineRepository
{
protected $objectManager;
protected $logger;
public function __construct(ObjectManager $objectManager)
{
$this->objectManager = $objectManager;
}
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;
}
// ...
}
Your child service classes may look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// src/AppBundle/Repository/DoctrineUserRepository.php
namespace AppBundle\Repository;
use AppBundle\Repository\BaseDoctrineRepository
// ...
class DoctrineUserRepository extends BaseDoctrineRepository
{
// ...
}
// src/AppBundle/Repository/DoctrinePostRepository.php
namespace AppBundle\Repository;
use AppBundle\Repository\BaseDoctrineRepository
// ...
class DoctrinePostRepository extends BaseDoctrineRepository
{
// ...
}
Just as you use PHP inheritance to avoid duplication in your PHP code, the service container allows you to extend parent services in order to avoid duplicated service definitions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
services:
app.base_doctrine_repository:
# as no class is configured, the parent service MUST be abstract
abstract: true
arguments: ['@doctrine.orm.entity_manager']
calls:
- [setLogger, ['@logger']]
app.user_repository:
class: AppBundle\Repository\DoctrineUserRepository
# extend the app.base_doctrine_repository service
parent: app.base_doctrine_repository
app.post_repository:
class: AppBundle\Repository\DoctrinePostRepository
parent: app.base_doctrine_repository
# ...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- as no class is configured, the parent service MUST be abstract -->
<service id="app.base_doctrine_repository" abstract="true">
<argument type="service" id="doctrine.orm.entity_manager" />
<call method="setLogger">
<argument type="service" id="logger" />
</call>
</service>
<!-- extends the app.base_doctrine_repository service -->
<service id="app.user_repository"
class="AppBundle\Repository\DoctrineUserRepository"
parent="app.base_doctrine_repository"
/>
<service id="app.post_repository"
class="AppBundle\Repository\DoctrineUserRepository"
parent="app.base_doctrine_repository"
/>
<!-- ... -->
</services>
</container>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
use AppBundle\Repository\DoctrineUserRepository;
use AppBundle\Repository\DoctrinePostRepository;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
// as no class is configured, the parent service MUST be abstract
$container->register('app.base_doctrine_repository')
->addArgument(new Reference('doctrine.orm.entity_manager'))
->addMethodCall('setLogger', array(new Reference('logger')))
;
// extend the app.base_doctrine_repository service
$definition = new DefinitionDecorator('app.base_doctrine_repository');
$definition->setClass(DoctrineUserRepository::class);
$container->setDefinition('app.user_repository', $definition);
$definition = new DefinitionDecorator('app.base_doctrine_repository');
$definition->setClass(DoctrinePostRepository::class);
$container->setDefinition('app.post_repository', $definition);
// ...
In this context, having a parent
service implies that the arguments
and method calls of the parent service should be used for the child services.
Specifically, the EntityManager
will be injected and setLogger()
will
be called when app.user_repository
is instantiated.
Caution
The scope
, abstract
and tags
attributes are not inherited from
parent services.
Tip
In the examples shown, the classes sharing the same configuration also extend from the same parent class in PHP. This isn't necessary at all. You can just extract common parts of similar service definitions into a parent service without also extending a parent class in PHP.
Overriding Parent Dependencies
There may be times where you want to override what service is injected for one child service only. You can override most settings by simply specifying it in the child class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
services:
# ...
app.user_repository:
class: AppBundle\Repository\DoctrineUserRepository
parent: app.base_doctrine_repository
# overrides the public setting of the parent service
public: false
# appends the '@app.username_checker' argument to the parent
# argument list
arguments: ['@app.username_checker']
app.post_repository:
class: AppBundle\Repository\DoctrinePostRepository
parent: app.base_doctrine_repository
# overrides the first argument (using the special index_N key)
arguments:
index_0: '@doctrine.custom_entity_manager'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<!-- ... -->
<!-- overrides the public setting of the parent service -->
<service id="app.user_repository"
class="AppBundle\Repository\DoctrineUserRepository"
parent="app.base_doctrine_repository"
public="false"
>
<!-- appends the '@app.username_checker' argument to the parent
argument list -->
<argument type="service" id="app.username_checker" />
</service>
<service id="app.post_repository"
class="AppBundle\Repository\DoctrineUserRepository"
parent="app.base_doctrine_repository"
>
<!-- overrides the first argument (using the index attribute) -->
<argument index="0" type="service" id="doctrine.custom_entity_manager" />
</service>
<!-- ... -->
</services>
</container>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
use AppBundle\Repository\DoctrineUserRepository;
use AppBundle\Repository\DoctrinePostRepository;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
// ...
$definition = new DefinitionDecorator('app.base_doctrine_repository');
$definition->setClass(DoctrineUserRepository::class);
// overrides the public setting of the parent service
$definition->setPublic(false);
// appends the '@app.username_checker' argument to the parent argument list
$definition->addArgument(new Reference('app.username_checker'));
$container->setDefinition('app.user_repository', $definition);
$definition = new DefinitionDecorator('app.base_doctrine_repository');
$definition->setClass(DoctrinePostRepository::class);
// overrides the first argument
$definition->replaceArgument(0, new Reference('doctrine.custom_entity_manager'));
$container->setDefinition('app.post_repository', $definition);