Completed
Push — 2.x ( 359f6e )
by Sullivan
14:45
created

AddTemplatesCompilerPass::mergeMethodCall()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
rs 8.8571
cc 5
eloc 10
nc 5
nop 3
1
<?php
2
3
/*
4
 * This file is part of the Sonata project.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\DoctrineORMAdminBundle\DependencyInjection\Compiler;
13
14
use Symfony\Component\DependencyInjection\Definition;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17
18
/*
19
 *
20
 * @author Thomas Rabaix <[email protected]>
21
 */
22
class AddTemplatesCompilerPass implements CompilerPassInterface
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    public function process(ContainerBuilder $container)
28
    {
29
        $overwrite = $container->getParameter('sonata.admin.configuration.admin_services');
30
        $templates = $container->getParameter('sonata_doctrine_orm_admin.templates');
31
32
        foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
33
34
            if (!isset($attributes[0]['manager_type']) || $attributes[0]['manager_type'] != 'orm') {
35
                continue;
36
            }
37
38
            $definition = $container->getDefinition($id);
39
40
            if (!$definition->hasMethodCall('setFormTheme')) {
41
                $definition->addMethodCall('setFormTheme', array($templates['form']));
42
            }
43
44
            if (isset($overwrite[$id]['templates']['form'])) {
45
                $this->mergeMethodCall($definition, 'setFormTheme', $overwrite[$id]['templates']['form']);
46
            }
47
48
            if (!$definition->hasMethodCall('setFilterTheme')) {
49
                $definition->addMethodCall('setFilterTheme', array($templates['filter']));
50
            }
51
52
            if (isset($overwrite[$id]['templates']['filter'])) {
53
                $this->mergeMethodCall($definition, 'setFilterTheme', $overwrite[$id]['templates']['filter']);
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param Definition $definition
60
     * @param string     $name
61
     * @param mixed      $value
62
     */
63
    public function mergeMethodCall(Definition $definition, $name, $value)
64
    {
65
        $methodCalls = $definition->getMethodCalls();
66
67
        foreach ($methodCalls as &$calls) {
68
            foreach($calls as &$call) {
69
                if (is_string($call)) {
70
                    if ($call !== $name) {
71
                        continue 2;
72
                    }
73
                    continue 1;
74
                }
75
76
                $call = array(array_merge($call[0], $value));
77
            }
78
        }
79
80
        $definition->setMethodCalls($methodCalls);
81
    }
82
}
83