|
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\Parser; |
|
9
|
|
|
|
|
10
|
|
|
use PhUml\Code\Attribute; |
|
11
|
|
|
use PhUml\Code\ClassDefinition; |
|
12
|
|
|
use PhUml\Code\Definition; |
|
13
|
|
|
use PhUml\Code\InterfaceDefinition; |
|
14
|
|
|
use PhUml\Code\Method; |
|
15
|
|
|
use PhUml\Code\Structure; |
|
16
|
|
|
use PhUml\Code\TypeDeclaration; |
|
17
|
|
|
use PhUml\Code\Variable; |
|
18
|
|
|
use PhUml\Parser\Raw\RawDefinition; |
|
19
|
|
|
use PhUml\Parser\Raw\RawDefinitions; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* It builds a `Structure` from a `RawDefinitions` |
|
23
|
|
|
*/ |
|
24
|
|
|
class StructureBuilder |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var Structure */ |
|
27
|
|
|
private $structure; |
|
28
|
|
|
|
|
29
|
|
|
/** @var DefinitionBuilder */ |
|
30
|
|
|
protected $builder; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(Structure $structure = null, DefinitionBuilder $builder = null) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->structure = $structure ?? new Structure(); |
|
35
|
|
|
$this->builder = $builder ?? new DefinitionBuilder(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function buildFrom(RawDefinitions $definitions): Structure |
|
39
|
|
|
{ |
|
40
|
|
|
foreach ($definitions->all() as $definition) { |
|
41
|
|
|
if ($this->structure->has($definition->name())) { |
|
42
|
|
|
continue; |
|
43
|
|
|
} |
|
44
|
|
|
if ($definition->isClass()) { |
|
45
|
|
|
$this->structure->addClass($this->buildClass($definitions, $definition)); |
|
46
|
|
|
} elseif ($definition->isInterface()) { |
|
47
|
|
|
$this->structure->addInterface($this->buildInterface($definitions, $definition)); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
return $this->structure; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function buildInterface(RawDefinitions $definitions, RawDefinition $interface): InterfaceDefinition |
|
54
|
|
|
{ |
|
55
|
|
|
return new InterfaceDefinition( |
|
56
|
|
|
$interface->name(), |
|
57
|
|
|
$this->builder->methods($interface), |
|
58
|
|
|
$this->resolveRelatedInterface($definitions, $interface->parent()) |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function buildClass(RawDefinitions $definitions, RawDefinition $class): ClassDefinition |
|
63
|
|
|
{ |
|
64
|
|
|
return new ClassDefinition( |
|
65
|
|
|
$class->name(), |
|
66
|
|
|
$this->builder->attributes($class), |
|
67
|
|
|
$this->builder->methods($class), |
|
68
|
|
|
$this->buildInterfaces($definitions, $class->interfaces()), |
|
69
|
|
|
$this->resolveParentClass($definitions, $class->parent()) |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string[] $implements |
|
75
|
|
|
* @return Definition[] |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function buildInterfaces(RawDefinitions $definitions, array $implements): array |
|
78
|
|
|
{ |
|
79
|
|
|
return array_map(function (string $interface) use ($definitions) { |
|
80
|
|
|
return $this->resolveRelatedInterface($definitions, $interface); |
|
81
|
|
|
}, $implements); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function resolveRelatedInterface(RawDefinitions $definitions, ?string $interface): ?Definition |
|
85
|
|
|
{ |
|
86
|
|
|
if ($interface === null) { |
|
87
|
|
|
return null; |
|
88
|
|
|
} |
|
89
|
|
|
if (!$this->structure->has($interface)) { |
|
90
|
|
|
$this->structure->addInterface($this->buildInterface( |
|
91
|
|
|
$definitions, |
|
92
|
|
|
$definitions->get($interface) |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
return $this->structure->get($interface); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected function resolveParentClass(RawDefinitions $definitions, ?string $parent): ?Definition |
|
99
|
|
|
{ |
|
100
|
|
|
if ($parent === null) { |
|
101
|
|
|
return null; |
|
102
|
|
|
} |
|
103
|
|
|
if (!$this->structure->has($parent)) { |
|
104
|
|
|
$this->structure->addClass($this->buildClass($definitions, $definitions->get($parent))); |
|
105
|
|
|
} |
|
106
|
|
|
return $this->structure->get($parent); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|