Completed
Push — master ( dba66b...b8eb92 )
by Fabien
03:50
created

ServiceRepositoryCompilerPass   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 5
dl 0
loc 27
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 22 3
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
        $locatorDef = $container->getDefinition('doctrine.orm.container_repository_factory');
33
34
        $repoServiceIds = array_keys($container->findTaggedServiceIds(self::REPOSITORY_SERVICE_TAG));
35
36
        // Symfony 3.2 and lower sanity check
37
        if (!class_exists(ServiceLocatorTagPass::class)) {
38
            if (!empty($repoServiceIds)) {
39
                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)));
40
            }
41
42
            return;
43
        }
44
45
        $repoReferences = array_map(function ($id) {
46
            return new Reference($id);
47
        }, $repoServiceIds);
48
49
        $ref = ServiceLocatorTagPass::register($container, array_combine($repoServiceIds, $repoReferences));
50
        $locatorDef->replaceArgument(0, $ref);
51
    }
52
}
53