Completed
Push — master ( 31c85f...67dec4 )
by San
16s
created

lib/DependencyInjection/Compiler/ModelPass.php (1 issue)

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
 * This file is part of the PommProject/PommBundle package.
4
 *
5
 * (c) 2014 - 2016 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\PommBundle\DependencyInjection\Compiler;
11
12
use Symfony\Component\DependencyInjection as DI;
13
14
/**
15
 * Class PoolerPass
16
 * @package PommProject\PommBundle\DependencyInjection\Compiler
17
 * @author  Miha Vrhovnik
18
 */
19
class ModelPass implements DI\Compiler\CompilerPassInterface
20
{
21
    public function process(DI\ContainerBuilder $container)
22
    {
23
        $this->addTagged($container, 'pomm.model', 'pomm.pooler.model', 'getModel');
24
25
        $this->addTagged($container, 'pomm.model_layer', 'pomm.pooler.model_layer', 'getModelLayer');
26
    }
27
28
    private function addTagged(DI\ContainerBuilder $container, $tag, $defaultServiceId, $method)
29
    {
30
        /** @var DI\Definition[] $definitions */
31
        $definitions = [];
32
33
        // find all service IDs with the appropriate tag
34
        $taggedServices = $container->findTaggedServiceIds($tag);
35
36
        foreach ($taggedServices as $id => $tags) {
37
            $class = $container->getDefinition($id)
38
                ->getClass()
39
            ;
40
41
            $serviceId = isset($tags[0]['pooler']) ? $tags[0]['pooler'] : $defaultServiceId;
42
            $sessionId = isset($tags[0]['session']) ? $tags[0]['session'] : 'pomm.default_session';
43
44
            if (!array_key_exists($serviceId, $definitions)) {
45
                if ($container->hasDefinition($serviceId)) {
46
                    $definitions[$serviceId] = $container->getDefinition($serviceId);
47
48
                    $interface = 'PommProject\PommBundle\Model\ServiceMapInterface';
49
                    if (!in_array($interface, class_implements($definitions[$serviceId]->getClass()), true)) {
50
                        throw new \RuntimeException(sprintf('Your pooler should implement %s.', $interface));
51
                    }
52
                } else {
53
                    throw new \RuntimeException(sprintf('There is no pooler service with id %s.', $serviceId));
54
                }
55
            }
56
57
            $definitions[$serviceId]->addMethodCall('addModelToServiceMapping', [$class, $id . '.pomm.inner']);
58
59
            $old = $container->getDefinition($id);
60
            $old->setPublic(true);
61
            $container->removeDefinition($id);
62
            $container->addDefinitions([$id . '.pomm.inner' => $old]);
63
64
            $container->register($id, $old->getClass())
0 ignored issues
show
Deprecated Code introduced by
The method Symfony\Component\Depend...n::setAutowiringTypes() has been deprecated with message: since version 3.3, to be removed in 4.0.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
65
                ->setFactory([new DI\Reference($sessionId), $method])
66
                ->addArgument($old->getClass())
67
                ->setAutowiringTypes([$old->getClass()]) //set this one as a default for autowire
68
            ;
69
        }
70
    }
71
}
72