File size: 1,314 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php

namespace Mautic\CoreBundle\DependencyInjection\Compiler;

use Mautic\CoreBundle\Factory\ModelFactory;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class ModelPass implements CompilerPassInterface
{
    public const TAG = 'mautic.model';

    public function process(ContainerBuilder $container): void
    {
        $modelServices = [];
        foreach ($container->findTaggedServiceIds(self::TAG) as $id => $tags) {
            $modelServices[$id] = new Reference($id);

            // because aliases are not tagged we need to inject them too.
            // @see https://github.com/symfony/symfony/issues/17256
            foreach ($container->getAliases() as $aliasId => $alias) {
                $aliasedId = (string) $alias;
                if ($aliasedId !== $id) {
                    continue;
                }

                $modelServices[$aliasId] = new Reference($aliasedId);
            }
        }

        $myService = $container->findDefinition(ModelFactory::class);

        $myService->addArgument(ServiceLocatorTagPass::register($container, $modelServices));
    }
}