1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This source file is subject to the license that is bundled with this package in the file LICENSE. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PhUml\Processors; |
7
|
|
|
|
8
|
|
|
use PhUml\Code\ClassDefinition; |
9
|
|
|
use PhUml\Code\Codebase; |
10
|
|
|
use PhUml\Code\Definition; |
11
|
|
|
use PhUml\Code\InterfaceDefinition; |
12
|
|
|
use PhUml\Code\TraitDefinition; |
13
|
|
|
use PhUml\Graphviz\Builders\ClassGraphBuilder; |
14
|
|
|
use PhUml\Graphviz\Builders\InterfaceGraphBuilder; |
15
|
|
|
use PhUml\Graphviz\Builders\TraitGraphBuilder; |
16
|
|
|
use PhUml\Graphviz\Digraph; |
17
|
|
|
use PhUml\Graphviz\DigraphPrinter; |
18
|
|
|
use PhUml\Templates\TemplateEngine; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* It creates a digraph from a `Structure` and returns it as a string in DOT format |
22
|
|
|
*/ |
23
|
|
|
final class GraphvizProcessor implements Processor |
24
|
|
|
{ |
25
|
18 |
|
public static function fromConfiguration(GraphvizConfiguration $configuration): GraphvizProcessor |
26
|
|
|
{ |
27
|
18 |
|
$style = $configuration->digraphStyle(); |
28
|
18 |
|
$associationsBuilder = $configuration->associationsBuilder(); |
29
|
|
|
|
30
|
18 |
|
return new GraphvizProcessor( |
31
|
18 |
|
new ClassGraphBuilder($associationsBuilder), |
32
|
18 |
|
new InterfaceGraphBuilder(), |
33
|
18 |
|
new TraitGraphBuilder(), |
34
|
18 |
|
new DigraphPrinter(new TemplateEngine(), $style) |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
18 |
|
private function __construct( |
39
|
|
|
private readonly ClassGraphBuilder $classBuilder, |
40
|
|
|
private readonly InterfaceGraphBuilder $interfaceBuilder, |
41
|
|
|
private readonly TraitGraphBuilder $traitBuilder, |
42
|
|
|
private readonly DigraphPrinter $printer |
43
|
|
|
) { |
44
|
|
|
} |
45
|
|
|
|
46
|
11 |
|
public function name(): string |
47
|
|
|
{ |
48
|
11 |
|
return 'Graphviz'; |
49
|
|
|
} |
50
|
|
|
|
51
|
13 |
|
public function process(Codebase $codebase): OutputContent |
52
|
|
|
{ |
53
|
13 |
|
$digraph = new Digraph(); |
54
|
13 |
|
foreach ($codebase->definitions() as $definition) { |
55
|
11 |
|
$this->extractElements($definition, $codebase, $digraph); |
56
|
|
|
} |
57
|
13 |
|
return new OutputContent($this->printer->toDot($digraph)); |
58
|
|
|
} |
59
|
|
|
|
60
|
11 |
|
private function extractElements( |
61
|
|
|
Definition $definition, |
62
|
|
|
Codebase $codebase, |
63
|
|
|
Digraph $digraph |
64
|
|
|
): void { |
65
|
11 |
|
if ($definition instanceof ClassDefinition) { |
66
|
11 |
|
$digraph->add($this->classBuilder->extractFrom($definition, $codebase)); |
67
|
8 |
|
} elseif ($definition instanceof InterfaceDefinition) { |
68
|
8 |
|
$digraph->add($this->interfaceBuilder->extractFrom($definition, $codebase)); |
69
|
8 |
|
} elseif ($definition instanceof TraitDefinition) { |
70
|
8 |
|
$digraph->add($this->traitBuilder->extractFrom($definition, $codebase)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|