ServiceRepositoryCompilerPass::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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