Completed
Push — master ( b8eb92...8b462d )
by Fabien
12s
created

ServiceRepositoryCompilerPass::process()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 27
rs 8.5806
c 1
b 1
f 0
cc 4
eloc 14
nc 4
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\Exception\RuntimeException;
21
use Symfony\Component\DependencyInjection\Reference;
22
23
/**
24
 * @author Ryan Weaver <[email protected]>
25
 */
26
final class ServiceRepositoryCompilerPass implements CompilerPassInterface
27
{
28
    const REPOSITORY_SERVICE_TAG = 'doctrine.repository_service';
29
30
    public function process(ContainerBuilder $container)
31
    {
32
        // when ORM is not enabled
33
        if (!$container->hasDefinition('doctrine.orm.container_repository_factory')) {
34
            return;
35
        }
36
37
        $locatorDef = $container->getDefinition('doctrine.orm.container_repository_factory');
38
39
        $repoServiceIds = array_keys($container->findTaggedServiceIds(self::REPOSITORY_SERVICE_TAG));
40
41
        // Symfony 3.2 and lower sanity check
42
        if (!class_exists(ServiceLocatorTagPass::class)) {
43
            if (!empty($repoServiceIds)) {
44
                throw new RuntimeException(sprintf('The "%s" tag can only be used with Symfony 3.3 or higher. Remove the tag from the following services (%s) or upgrade to Symfony 3.3 or higher.', self::REPOSITORY_SERVICE_TAG, implode(', ', $repoServiceIds)));
45
            }
46
47
            return;
48
        }
49
50
        $repoReferences = array_map(function ($id) {
51
            return new Reference($id);
52
        }, $repoServiceIds);
53
54
        $ref = ServiceLocatorTagPass::register($container, array_combine($repoServiceIds, $repoReferences));
55
        $locatorDef->replaceArgument(0, $ref);
56
    }
57
}
58