|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Flagbit\Plantuml\TokenReflection; |
|
4
|
|
|
|
|
5
|
|
|
use TokenReflection\IReflectionClass; |
|
6
|
|
|
|
|
7
|
|
|
class ClassWriter extends WriterAbstract |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \Flagbit\Plantuml\TokenReflection\ConstantWriter |
|
11
|
|
|
*/ |
|
12
|
|
|
private $constantWriter; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var \Flagbit\Plantuml\TokenReflection\PropertyWriter |
|
16
|
|
|
*/ |
|
17
|
|
|
private $propertyWriter; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var \Flagbit\Plantuml\TokenReflection\MethodWriter |
|
21
|
|
|
*/ |
|
22
|
|
|
private $methodWriter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \Flagbit\Plantuml\TokenReflection\DocContentWriter |
|
26
|
|
|
*/ |
|
27
|
|
|
private $docContentWriter; |
|
28
|
|
|
|
|
29
|
14 |
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
14 |
|
$this->setIndent(''); |
|
32
|
14 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param \Flagbit\Plantuml\TokenReflection\ConstantWriter $constantWriter |
|
36
|
|
|
*/ |
|
37
|
10 |
|
public function setConstantWriter($constantWriter) |
|
38
|
|
|
{ |
|
39
|
10 |
|
$this->constantWriter = $constantWriter; |
|
40
|
10 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param \Flagbit\Plantuml\TokenReflection\MethodWriter $methodWriter |
|
44
|
|
|
*/ |
|
45
|
10 |
|
public function setMethodWriter($methodWriter) |
|
46
|
|
|
{ |
|
47
|
10 |
|
$this->methodWriter = $methodWriter; |
|
48
|
10 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param \Flagbit\Plantuml\TokenReflection\PropertyWriter $propertyWriter |
|
52
|
|
|
*/ |
|
53
|
10 |
|
public function setPropertyWriter($propertyWriter) |
|
54
|
|
|
{ |
|
55
|
10 |
|
$this->propertyWriter = $propertyWriter; |
|
56
|
10 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \Flagbit\Plantuml\TokenReflection\DocContentWriter $docContentWriter |
|
60
|
|
|
*/ |
|
61
|
10 |
|
public function setDocContentWriter($docContentWriter) |
|
62
|
|
|
{ |
|
63
|
10 |
|
$this->docContentWriter = $docContentWriter; |
|
64
|
10 |
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \TokenReflection\IReflectionClass $class |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
14 |
|
public function writeElement(IReflectionClass $class) |
|
72
|
|
|
{ |
|
73
|
14 |
|
$classString = $this->formatLine( |
|
74
|
14 |
|
$this->writeAbstract($class) . $this->writeObjectType($class) . ' ' . $this->formatClassName( |
|
75
|
14 |
|
$class->getName() |
|
76
|
14 |
|
) . ' {' |
|
77
|
14 |
|
); |
|
78
|
|
|
|
|
79
|
14 |
|
if ($this->constantWriter) { |
|
80
|
10 |
|
$constantReflections = $class->getOwnConstantReflections(); |
|
81
|
10 |
|
foreach ($class->getConstantReflections() as $otherConstantReflection) { |
|
82
|
|
|
/* @var $otherConstantReflection \TokenReflection\ReflectionConstant */ |
|
83
|
2 |
|
$otherConstantName = $otherConstantReflection->getName(); |
|
84
|
|
|
|
|
85
|
2 |
|
foreach ($constantReflections as $constantReflection) { |
|
86
|
2 |
|
if ($constantReflection->getName() === $otherConstantName) { |
|
87
|
|
|
// skip constants already defined in our current class |
|
88
|
2 |
|
continue 2; |
|
89
|
|
|
} |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
$constantReflections[] = $otherConstantReflection; |
|
93
|
10 |
|
} |
|
94
|
|
|
|
|
95
|
10 |
|
$classString .= $this->constantWriter->writeElements($constantReflections); |
|
96
|
10 |
|
} |
|
97
|
|
|
|
|
98
|
14 |
|
if ($this->propertyWriter) { |
|
99
|
10 |
|
$classString .= $this->propertyWriter->writeElements($class->getOwnProperties()); |
|
100
|
10 |
|
if($this->docContentWriter) { |
|
101
|
10 |
|
$classString .= $this->docContentWriter->writeProperties($class); |
|
102
|
10 |
|
} |
|
103
|
10 |
|
} |
|
104
|
|
|
|
|
105
|
14 |
|
if ($this->methodWriter) { |
|
106
|
10 |
|
$classString .= $this->methodWriter->writeElements($class->getOwnMethods()); |
|
107
|
10 |
|
if($this->docContentWriter) { |
|
108
|
10 |
|
$classString .= $this->docContentWriter->writeMethods($class); |
|
109
|
10 |
|
} |
|
110
|
10 |
|
} |
|
111
|
|
|
|
|
112
|
14 |
|
$classString .= $this->formatLine('}'); |
|
113
|
|
|
|
|
114
|
14 |
|
if ($class->getParentClassName()) { |
|
115
|
3 |
|
$classString .= $this->formatLine( |
|
116
|
3 |
|
$this->writeObjectType($class) . ' ' . $this->formatClassName($class->getName()) . ' extends ' |
|
117
|
3 |
|
. $this->formatClassName( |
|
118
|
3 |
|
$class->getParentClassName() |
|
119
|
3 |
|
) |
|
120
|
3 |
|
); |
|
121
|
3 |
|
} |
|
122
|
|
|
|
|
123
|
14 |
|
if ($interfaceNames = $class->getOwnInterfaceNames()) { |
|
124
|
1 |
|
foreach ($interfaceNames as $interfaceName) { |
|
125
|
1 |
|
$classString .= $this->formatLine( |
|
126
|
1 |
|
$this->writeObjectType($class) . ' ' . $this->formatClassName($class->getName()) . ' implements ' |
|
127
|
1 |
|
. $this->formatClassName( |
|
128
|
|
|
$interfaceName |
|
129
|
1 |
|
) |
|
130
|
1 |
|
); |
|
131
|
1 |
|
} |
|
132
|
1 |
|
} |
|
133
|
|
|
|
|
134
|
14 |
|
return $classString; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param IReflectionClass $class |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
14 |
|
private function writeAbstract(IReflectionClass $class) |
|
143
|
|
|
{ |
|
144
|
14 |
|
$return = ''; |
|
145
|
14 |
|
if (true === $class->isAbstract() && false === $class->isInterface()) { |
|
146
|
1 |
|
$return = 'abstract '; |
|
147
|
1 |
|
} |
|
148
|
14 |
|
return $return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param IReflectionClass $class |
|
153
|
|
|
* |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
14 |
|
private function writeObjectType(IReflectionClass $class) |
|
157
|
|
|
{ |
|
158
|
14 |
|
$return = 'class'; |
|
159
|
14 |
|
if (true === $class->isInterface()) { |
|
160
|
3 |
|
$return = 'interface'; |
|
161
|
3 |
|
} |
|
162
|
14 |
|
return $return; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|