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