Completed
Push — master ( 57b7f2...4f95ff )
by Grégoire
28:13 queued 01:46
created

ModelManagerCompilerPass::process()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 7
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\Model\ModelManagerInterface;
17
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18
use Symfony\Component\DependencyInjection\ContainerBuilder;
19
use Symfony\Component\DependencyInjection\Exception\LogicException;
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 const MANAGER_TAG = 'sonata.admin.manager';
29
30
    public function process(ContainerBuilder $container): void
31
    {
32
        $availableManagers = [];
33
34
        foreach ($container->findTaggedServiceIds(self::MANAGER_TAG) as $id => $tags) {
35
            $definition = $container->findDefinition($id);
36
37
            if (!is_subclass_of($definition->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...
38
                throw new LogicException(sprintf(
39
                    'Service "%s" must implement `%s`.',
40
                    $id,
41
                    ModelManagerInterface::class
42
                ));
43
            }
44
45
            $availableManagers[$id] = $definition;
46
        }
47
48
        if (!empty($availableManagers)) {
49
            $bundles = $container->getParameter('kernel.bundles');
50
            if (isset($bundles['MakerBundle'])) {
51
                $adminMakerDefinition = $container->getDefinition('sonata.admin.maker');
52
                $adminMakerDefinition->replaceArgument(1, $availableManagers);
53
            }
54
        }
55
    }
56
}
57