Completed
Push — master ( e6c9d7...cc6bd7 )
by Grégoire
12:23
created

ModelManagerCompilerPass   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 2
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 21 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\DependencyInjection\Compiler;
15
16
use Sonata\AdminBundle\Command\GenerateAdminCommand;
17
use Sonata\AdminBundle\Model\ModelManagerInterface;
18
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
19
use Symfony\Component\DependencyInjection\ContainerBuilder;
20
21
/**
22
 * This class injects available model managers to services which depend on them.
23
 *
24
 * @author Gaurav Singh Faudjdar <[email protected]>
25
 */
26
final class ModelManagerCompilerPass implements CompilerPassInterface
27
{
28
    public function process(ContainerBuilder $container): void
29
    {
30
        $availableManagers = [];
31
32
        foreach ($container->getServiceIds() as $id) {
33
            if (0 !== strpos($id, 'sonata.admin.manager.') || !is_subclass_of($container->getDefinition($id)->getClass(), ModelManagerInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Sonata\AdminBundle\Mode...ManagerInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
34
                continue;
35
            }
36
37
            $availableManagers[$id] = $container->getDefinition($id);
38
        }
39
40
        $bundles = $container->getParameter('kernel.bundles');
41
        if (isset($bundles['MakerBundle'])) {
42
            $adminMakerDefinition = $container->getDefinition('sonata.admin.maker');
43
            $adminMakerDefinition->replaceArgument(1, $availableManagers);
44
        }
45
46
        $generateAdminCommandDefinition = $container->getDefinition(GenerateAdminCommand::class);
47
        $generateAdminCommandDefinition->replaceArgument(1, $availableManagers);
48
    }
49
}
50