1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP version 7.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\Graphviz\Builders; |
9
|
|
|
|
10
|
|
|
use PhUml\Code\ClassDefinition; |
11
|
|
|
use PhUml\Code\Structure; |
12
|
|
|
use PhUml\Graphviz\Edge; |
13
|
|
|
use PhUml\Graphviz\Node; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* It produces the collection of nodes and edges related to a class |
17
|
|
|
* |
18
|
|
|
* - It creates an edge with the class it extends, if any, |
19
|
|
|
* - It creates edges from the the interfaces it implements |
20
|
|
|
* - It creates a node with the class itself |
21
|
|
|
* - It, optionally, discovers associations between classes/interfaces, by inspecting both: |
22
|
|
|
* - The class attributes |
23
|
|
|
* - The class constructor's parameters |
24
|
|
|
*/ |
25
|
|
|
class ClassGraphBuilder |
26
|
|
|
{ |
27
|
|
|
/** @var \PhUml\Graphviz\HasDotRepresentation[] */ |
28
|
|
|
private $dotElements; |
29
|
|
|
|
30
|
|
|
/** @var AssociationsBuilder */ |
31
|
|
|
private $associationsBuilder; |
32
|
|
|
|
33
|
|
|
/** @var NodeLabelBuilder */ |
34
|
|
|
private $labelBuilder; |
35
|
|
|
|
36
|
78 |
|
public function __construct(NodeLabelBuilder $labelBuilder, AssociationsBuilder $associationsBuilder = null) |
37
|
|
|
{ |
38
|
78 |
|
$this->associationsBuilder = $associationsBuilder ?? new NoAssociationsBuilder(); |
39
|
78 |
|
$this->labelBuilder = $labelBuilder; |
40
|
78 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The order in which the nodes and edges are created is as follows |
44
|
|
|
* |
45
|
|
|
* 1. The edges discovered via attributes inspection |
46
|
|
|
* 2. The edges discovered via the constructor parameters |
47
|
|
|
* 3. The node representing the class itself |
48
|
|
|
* 4. The parent class, if any |
49
|
|
|
* 5. The interfaces it implements, if any |
50
|
|
|
* |
51
|
|
|
* @return \PhUml\Graphviz\HasDotRepresentation[] |
52
|
|
|
*/ |
53
|
60 |
|
public function extractFrom(ClassDefinition $class, Structure $structure): array |
54
|
|
|
{ |
55
|
60 |
|
$this->dotElements = []; |
56
|
|
|
|
57
|
60 |
|
$this->addAssociations($this->associationsBuilder->fromAttributes($class, $structure)); |
58
|
60 |
|
$this->addAssociations($this->associationsBuilder->fromConstructor($class, $structure)); |
59
|
|
|
|
60
|
60 |
|
$this->dotElements[] = new Node($class, $this->labelBuilder->forClass($class)); |
61
|
|
|
|
62
|
60 |
|
if ($class->hasParent()) { |
63
|
30 |
|
$this->dotElements[] = Edge::inheritance($class->extends(), $class); |
64
|
|
|
} |
65
|
|
|
|
66
|
60 |
|
foreach ($class->implements() as $interface) { |
67
|
9 |
|
$this->dotElements[] = Edge::implementation($interface, $class); |
68
|
|
|
} |
69
|
|
|
|
70
|
60 |
|
return $this->dotElements; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** @param Edge[] */ |
74
|
60 |
|
private function addAssociations(array $edges): void |
75
|
|
|
{ |
76
|
60 |
|
$this->dotElements = array_merge($this->dotElements, $edges); |
77
|
60 |
|
} |
78
|
|
|
} |
79
|
|
|
|