1

I want to create dynamic services, therefore I added a ServiceProvider class to my module.

I have created a base service that my other services will have as parent.

When doing this in my_module.services.yml, I can just use:

parent: service_name

But I can't find out how to do in the ServiceProvider class.

My class looks like this:

class MyModuleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritDoc}
   */
  public function register(ContainerBuilder $container) {
    // Create services for our base normalizer classes.
    $container->register('MY_MODULE_rest.entity_normalizer_base', EntityNormalizerBase::class)
      ->addArgument(new Reference('entity_type.manager'));

    // Create node normalizer service.
    $container->register('MY_MODULE_rest.node_normalizer', NodeNormalizer::class)
      ->addTag('normalizer', ['priority' => 1])
      // How can I set "MY_MODULE_rest.entity_normalizer_base" as the parent for this one?
      ->addArgument(new Reference('path_alias.manager'))
      ->addArgument(new Reference('language_manager'));
  }
}

1 Answer 1

2

You can find your answer in the symfony docs, How to Manage Common Dependencies with Parent Services. In the third snippet we see:

use AppBundle\Repository\BaseDoctrineRepository;
use AppBundle\Repository\DoctrinePostRepository;
use AppBundle\Repository\DoctrineUserRepository;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Reference;

$container->register(BaseDoctrineRepository::class)
    ->setAbstract(true)
    ->addArgument(new Reference('doctrine.orm.entity_manager'))
    ->addMethodCall('setLogger', [new Reference('logger')]);

// extend the AppBundle\Repository\BaseDoctrineRepository service
$definition = new ChildDefinition(BaseDoctrineRepository::class);
$definition->setClass(DoctrineUserRepository::class);
$container->setDefinition(DoctrineUserRepository::class, $definition);

Looking at the docs and your code, my understanding is you'd do something like:

use Symfony\Component\DependencyInjection\ChildDefinition;

class MyModuleServiceProvider extends ServiceProviderBase {

  /**
   * {@inheritDoc}
   */
  public function register(ContainerBuilder $container) {
    // Create services for our base normalizer classes.
    $container->register('MY_MODULE_rest.entity_normalizer_base', EntityNormalizerBase::class)
      ->addArgument(new Reference('entity_type.manager'));

    $node_definition = new ChildDefinition('MY_MODULE_rest.entity_normalizer_base');
    $node_definition->setClass(NodeNormalizer::class);
    $node_definition->addTag('normalizer', ['priority' => 1]);
    $node_definition->addArgument(new Reference('path_alias.manager'));
    $node_definition->addArgument(new Reference('language_manager'));

    // Create node normalizer service.
    $container->setDefinition('MY_MODULE_rest.node_normalizer', $node_definition)

  }
}
2
  • 1
    Thanks, it works. Notice ot works when I use the service name instead of the class for the parent service. So new ChildDefinition('MY_MODULE_rest.entity_normalizer_base'), works!
    – user72672
    Dec 19, 2019 at 19:04
  • Understood, updated.
    – sonfd
    Dec 19, 2019 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.