GraphDefinitionBuilder.php 6.37 KB
Newer Older
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?php

/*
 * © 2016 IRSTEA
 * Guillaume Perréal <guillaume.perreal@irstea.fr>
 * Tous droits réservés.
 */

namespace Irstea\PlantUmlBundle\DependencyInjection\Builder;

use Irstea\PlantUmlBundle\Doctrine\EntityFinder;
use Irstea\PlantUmlBundle\Finder\ClassFinder;
use Irstea\PlantUmlBundle\Finder\FilteringFinder;
use Irstea\PlantUmlBundle\Model\ClassVisitor;
use Irstea\PlantUmlBundle\Model\Graph;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\DefinitionDecorator;
use Symfony\Component\DependencyInjection\Reference;

/**
 * Description of GraphDefinitionBuilder
 *
 * @author Guillaume Perréal <guillaume.perreal@irstea.fr>
 */
class GraphDefinitionBuilder
{
    /**
     * @var ContainerBuilder
     */
    private $container;

    /**
     * @var string
     */
    private $baseId;

    /**
     * @var array
     */
    private $config;

    /**
     * @var ClassFilterBuilder
     */
    private $filterBuilder;

    /**
     * @var Reference
     */
    private $entityManager;

    /**
     *
     * @var Definition
     */
    private $definition = null;

    /**
     *
     * @param ContainerBuilder $container
     * @param string $baseId
     * @param array $config
     */
    public function __construct(ContainerBuilder $container, $baseId, array $config, ClassFilterBuilder $filterBuilder)
    {
        $this->container = $container;
        $this->baseId = $baseId;
        $this->config = $config;
        $this->filterBuilder = $filterBuilder;

        $emId = $config['sources']['entity_manager'];
        $this->entityManager = new Reference("doctrine.orm.${emId}_entity_manager");
    }

    /**
     * @return Definition
     */
    public function build()
    {
        if (!$this->definition) {
            return $this->definition = $this->doBuild();
        }
        return $this->definition;
    }

    /**
     * @return Definition
     */
    protected function doBuild()
    {
        list($source, $sourceFilter) = $this->buildSources();
        
        $layoutFilter = $this->filterBuilder->build($this->config['layout']) ?: $sourceFilter;
        $decorator = $this->buildFilteredDecorator();
        $namespace = $this->buildNamespace();

        $visitor = $this->setDefinition("visitor", ClassVisitor::class, $decorator, $layoutFilter, $namespace);

        return new Definition(Graph::class, [$visitor, $source]);
    }

    /**
     * @return Refernce[]
     */
    protected function buildSources()
    {
        $finder = $this->buildFinder();
        $filter = $this->filterBuilder->build($this->config['sources']);

        if ($filter) {
            $filtered = $this->setDefinition("finder", FilteringFinder::class, $finder, $filter);
            return [$filtered, $filter];
        }

        return [$finder, null];
    }

    /**
     * @return Reference
     */
    protected function buildFinder()
    {
        $config = $this->config['sources'];

        switch($config['type']) {
            case 'entities':
                return $this->setDefinition("finder.entities", EntityFinder::class, $this->entityManager);
            case 'classes':
                return $this->setDefinition("finder.classes", ClassFinder::class, $config['directories']);
        }
    }

    /**
     * @return Reference
     */
    protected function buildFilteredDecorator()
    {
        $decorator = $this->buildDecorator();
        if (!$decorator) {
            return $decorator;
        }

        $filter = $this->filterBuilder->build($this->config['decoration']);
        if (!$filter) {
            return $decorator;
        }

        return $this->setDefinitionDecorator('decorator', 'irstea.plant_uml.decorator.filtered.template', $decorator, $filter);
    }

    /**
     * @return Reference
     */
    protected function buildDecorator()
    {
        $config = $this->config['decoration']['decorators'];

        if (empty($config)) {
            return null;
        }

        if (count($config) === 1) {
            return $this->buildTypedDecorator($config[0]);
        }

        $decorators = [];
        foreach ($config as $type) {
            $decorators[] = $this->buildTypedDecorator($type);
        }

        return $this->setDefinitionDecorator(
            'decorator.all',
            'irstea.plant_uml.decorator.composite.template',
            $decorators
        );
    }

    /**
     * @param string $type
     * @return Reference
     */
    protected function buildTypedDecorator($type)
    {
        if (in_array($type, ['entity', 'associations', 'fields'])) {
            return $this->setDefinitionDecorator(
                "decorator.$type",
                "irstea.plant_uml.decorator.$type.template",
                $this->entityManager
            );
        }

        return new Reference("irstea.plant_uml.decorator.$type");
    }

    /**
œ     * @return Reference
     */
    protected function buildNamespace()
    {
        $type = $this->config['layout']['namespaces'];
        
        if ($type === "entities") {
            return $this->setDefinitionDecorator(
                "namespace.$type",
                "irstea.plant_uml.namespaces.$type.template",
                $this->entityManager
            );
        }

        return new Reference("irstea.plant_uml.namespaces.$type");
    }

    /**
     * @param string $localId
     * @param string|Definition $classOrDef
     * @param array ...$arguments
     * @return Reference
     */
    protected function setDefinition($localId, $classOrDef, ...$arguments)
    {
        if ($classOrDef instanceof Definition) {
            $definition = $classOrDef;
        } else {
            $definition = new Definition($classOrDef, $arguments);
        }
        $id = $this->globalId($localId);
        $this->container->setDefinition($id, $definition);
        return new Reference($id);
    }

    /**
     * @param string $localId
     * @param string $templateId
     * @param array ...$arguments
     * @return Reference
     */
    protected function setDefinitionDecorator($localId, $templateId, ...$arguments)
    {
        $def = new DefinitionDecorator($templateId);
        $def->setArguments($arguments);
        return $this->setDefinition($localId, $def);
    }

    /**
     * @param string $localId
     * @return string
     */
    protected function globalId($localId)
    {
        return "{$this->baseId}.$localId";
    }
}