Completed
Pull Request — master (#727)
by
unknown
02:10
created

ServiceRepositoryCompilerPass::process()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Doctrine Bundle
5
 *
6
 * The code was originally distributed inside the Symfony framework.
7
 *
8
 * (c) Fabien Potencier <[email protected]>
9
 * (c) Doctrine Project, Benjamin Eberlei <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
16
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
use Symfony\Component\DependencyInjection\Reference;
21
22
/**
23
 * @author Ryan Weaver <[email protected]>
24
 */
25
class ServiceRepositoryCompilerPass implements CompilerPassInterface
26
{
27
    const REPOSITORY_SERVICE_TAG = 'doctrine.repository_service';
28
29
    public function process(ContainerBuilder $container)
30
    {
31
        if (!$container->hasDefinition('doctrine.orm.container_repository_factory')) {
32
            return;
33
        }
34
35
        $locatorDef = $container->getDefinition('doctrine.orm.container_repository_factory');
36
37
        $repoServiceIds = array_keys($container->findTaggedServiceIds(self::REPOSITORY_SERVICE_TAG));
38
        $repoReferences = array_map(function($id) {
39
            return new Reference($id);
40
        }, $repoServiceIds);
41
42
        $ref = ServiceLocatorTagPass::register($container, array_combine($repoServiceIds, $repoReferences));
43
        $locatorDef->replaceArgument(0, $ref);
44
    }
45
}