Completed
Push — 3.x ( 1cd1d0...b03505 )
by Grégoire
16:27 queued 03:11
created

ModelManagerCompilerPass::process()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.2728
c 0
b 0
f 0
cc 5
nc 6
nop 1
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