Completed
Push — master ( dfdb22...eb88c4 )
by Andreas
08:54 queued 07:05
created

DbalSchemaFilterPass   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 4
dl 0
loc 43
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B process() 0 37 9
1
<?php
2
3
namespace Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler;
4
5
use Doctrine\DBAL\Configuration;
6
use LogicException;
7
use Symfony\Component\DependencyInjection\ChildDefinition;
8
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9
use Symfony\Component\DependencyInjection\ContainerBuilder;
10
use Symfony\Component\DependencyInjection\Reference;
11
12
/**
13
 * Processes the doctrine.dbal.schema_filter
14
 */
15
class DbalSchemaFilterPass implements CompilerPassInterface
16
{
17
    /**
18
     * {@inheritDoc}
19
     */
20
    public function process(ContainerBuilder $container)
21
    {
22
        $filters = $container->findTaggedServiceIds('doctrine.dbal.schema_filter');
23
24
        if (count($filters) > 0 && ! method_exists(Configuration::class, 'setSchemaAssetsFilter')) {
25
            throw new LogicException('The doctrine.dbal.schema_filter tag is only supported when using doctrine/dbal 2.9 or higher.');
26
        }
27
28
        $connectionFilters = [];
29
        foreach ($filters as $id => $tagAttributes) {
30
            foreach ($tagAttributes as $attributes) {
31
                $name = isset($attributes['connection']) ? $attributes['connection'] : $container->getParameter('doctrine.default_connection');
32
33
                if (! isset($connectionFilters[$name])) {
34
                    $connectionFilters[$name] = [];
35
                }
36
37
                $connectionFilters[$name][] = new Reference($id);
38
            }
39
        }
40
41
        foreach ($connectionFilters as $name => $references) {
42
            $configurationId = sprintf('doctrine.dbal.%s_connection.configuration', $name);
43
44
            if (! $container->hasDefinition($configurationId)) {
45
                continue;
46
            }
47
48
            $definition = new ChildDefinition('doctrine.dbal.schema_asset_filter_manager');
49
            $definition->setArgument(0, $references);
50
51
            $id = sprintf('doctrine.dbal.%s_schema_asset_filter_manager', $name);
52
            $container->setDefinition($id, $definition);
53
            $container->findDefinition($configurationId)
54
                ->addMethodCall('setSchemaAssetsFilter', [new Reference($id)]);
55
        }
56
    }
57
}
58