Completed
Push — master ( 00777c...4aaab4 )
by
unknown
07:13 queued 10s
created

Compiler/AdminExtensionCompilerPass.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\TranslationBundle\DependencyInjection\Compiler;
15
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
use Symfony\Component\DependencyInjection\ContainerBuilder;
18
use Symfony\Component\DependencyInjection\Reference;
19
20
/**
21
 * @author Nicolas Bastien <[email protected]>
22
 */
23
class AdminExtensionCompilerPass implements CompilerPassInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28
    public function process(ContainerBuilder $container): void
29
    {
30
        $translationTargets = $container->getParameter('sonata_translation.targets');
31
        $adminExtensionReferences = $this->getAdminExtensionReferenceByTypes(array_keys($translationTargets));
32
33
        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
34
            $admin = $container->getDefinition($id);
35
            $modelClass = $container->getParameterBag()->resolveValue($admin->getArgument(1));
36
            if (!$modelClass || !class_exists($modelClass)) {
37
                continue;
38
            }
39
            $modelClassReflection = new \ReflectionClass($modelClass);
40
41
            foreach ($adminExtensionReferences as $type => $reference) {
42
                foreach ($translationTargets[$type]['implements'] as $interface) {
43
                    if ($modelClassReflection->implementsInterface($interface)) {
44
                        $admin->addMethodCall('addExtension', [$reference]);
45
                    }
46
                }
47
                foreach ($translationTargets[$type]['instanceof'] as $class) {
48
                    if ($modelClassReflection->getName() === $class || $modelClassReflection->isSubclassOf($class)) {
0 ignored issues
show
Consider using $modelClassReflection->name. There is an issue with getName() and APC-enabled PHP versions.
Loading history...
49
                        $admin->addMethodCall('addExtension', [$reference]);
50
                    }
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * @return Reference[]
58
     */
59
    protected function getAdminExtensionReferenceByTypes(array $types)
60
    {
61
        $references = [];
62
        foreach ($types as $type) {
63
            $references[$type] = new Reference('sonata_translation.admin.extension.'.$type.'_translatable');
64
        }
65
66
        return $references;
67
    }
68
}
69