ServiceRepositoryCompilerPass.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
  3. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  4. use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
  5. use Symfony\Component\DependencyInjection\ContainerBuilder;
  6. use Symfony\Component\DependencyInjection\Reference;
  7. use function array_combine;
  8. use function array_keys;
  9. use function array_map;
  10. final class ServiceRepositoryCompilerPass implements CompilerPassInterface
  11. {
  12. public const REPOSITORY_SERVICE_TAG = 'doctrine.repository_service';
  13. public function process(ContainerBuilder $container): void
  14. {
  15. // when ORM is not enabled
  16. if (! $container->hasDefinition('doctrine.orm.container_repository_factory')) {
  17. return;
  18. }
  19. $locatorDef = $container->getDefinition('doctrine.orm.container_repository_factory');
  20. $repoServiceIds = array_keys($container->findTaggedServiceIds(self::REPOSITORY_SERVICE_TAG));
  21. $repoReferences = array_map(static function ($id) {
  22. return new Reference($id);
  23. }, $repoServiceIds);
  24. $ref = ServiceLocatorTagPass::register($container, array_combine($repoServiceIds, $repoReferences));
  25. $locatorDef->replaceArgument(0, $ref);
  26. }
  27. }