|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/** |
|
3
|
|
|
* PHP version 8.1 |
|
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
|
|
|
public static function fromConfiguration(GraphvizConfiguration $configuration): GraphvizProcessor |
|
28
|
|
|
{ |
|
29
|
|
|
$style = $configuration->digraphStyle(); |
|
30
|
|
|
$associationsBuilder = $configuration->associationsBuilder(); |
|
31
|
|
|
|
|
32
|
|
|
return new GraphvizProcessor( |
|
33
|
|
|
new ClassGraphBuilder($associationsBuilder), |
|
34
|
|
|
new InterfaceGraphBuilder(), |
|
35
|
|
|
new TraitGraphBuilder(), |
|
36
|
|
|
new DigraphPrinter(new TemplateEngine(), $style) |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function __construct( |
|
41
|
|
|
private readonly ClassGraphBuilder $classBuilder, |
|
|
|
|
|
|
42
|
|
|
private readonly InterfaceGraphBuilder $interfaceBuilder, |
|
43
|
|
|
private readonly TraitGraphBuilder $traitBuilder, |
|
44
|
|
|
private readonly DigraphPrinter $printer |
|
45
|
|
|
) { |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function name(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return 'Graphviz'; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function process(Codebase $codebase): OutputContent |
|
54
|
|
|
{ |
|
55
|
|
|
$digraph = new Digraph(); |
|
56
|
|
|
foreach ($codebase->definitions() as $definition) { |
|
57
|
|
|
$this->extractElements($definition, $codebase, $digraph); |
|
58
|
|
|
} |
|
59
|
|
|
return new OutputContent($this->printer->toDot($digraph)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
private function extractElements( |
|
63
|
|
|
Definition $definition, |
|
64
|
|
|
Codebase $codebase, |
|
65
|
|
|
Digraph $digraph |
|
66
|
|
|
): void { |
|
67
|
|
|
if ($definition instanceof ClassDefinition) { |
|
68
|
|
|
$digraph->add($this->classBuilder->extractFrom($definition, $codebase)); |
|
69
|
|
|
} elseif ($definition instanceof InterfaceDefinition) { |
|
70
|
|
|
$digraph->add($this->interfaceBuilder->extractFrom($definition, $codebase)); |
|
71
|
|
|
} elseif ($definition instanceof TraitDefinition) { |
|
72
|
|
|
$digraph->add($this->traitBuilder->extractFrom($definition, $codebase)); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|