Completed
Push — 3.x ( 8467a0...771509 )
by Grégoire
02:53
created

ModelManagerCompilerPass::process()   B

Complexity

Conditions 8
Paths 13

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
nc 13
nop 1
dl 0
loc 40
rs 8.0355
c 0
b 0
f 0
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
 * @final since sonata-project/admin-bundle 3.52
25
 *
26
 * @author Gaurav Singh Faudjdar <[email protected]>
27
 */
28
final class ModelManagerCompilerPass implements CompilerPassInterface
29
{
30
    public const MANAGER_TAG = 'sonata.admin.manager';
31
32
    public function process(ContainerBuilder $container): void
33
    {
34
        $availableManagers = [];
35
36
        // NEXT_MAJOR: Replace the `foreach()` clause with the following one.
37
        // foreach ($container->findTaggedServiceIds(self::MANAGER_TAG) as $id => $tags) {
38
        foreach ($container->getServiceIds() as $id) {
39
            $definition = $container->findDefinition($id);
40
41
            // NEXT_MAJOR: Remove this check.
42
            if (!$definition->hasTag(self::MANAGER_TAG) && 0 !== strpos($id, 'sonata.admin.manager.')) {
43
                continue;
44
            }
45
46
            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...
47
                throw new LogicException(sprintf('Service "%s" must implement `%s`.', $id, ModelManagerInterface::class));
48
            }
49
50
            // NEXT_MAJOR: Remove this check.
51
            if (!$definition->hasTag(self::MANAGER_TAG)) {
52
                @trigger_error(sprintf(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
53
                    'Not setting the "%s" tag on the "%s" service is deprecated since sonata-project/admin-bundle 3.x.',
54
                    self::MANAGER_TAG,
55
                    $id
56
                ), E_USER_DEPRECATED);
57
58
                $definition->addTag(self::MANAGER_TAG);
59
            }
60
61
            $availableManagers[$id] = $definition;
62
        }
63
64
        if (!empty($availableManagers)) {
65
            $bundles = $container->getParameter('kernel.bundles');
66
            if (isset($bundles['MakerBundle'])) {
67
                $adminMakerDefinition = $container->getDefinition('sonata.admin.maker');
68
                $adminMakerDefinition->replaceArgument(1, $availableManagers);
69
            }
70
        }
71
    }
72
}
73