1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
7
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\FieldGeneratorHelper; |
8
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\StringGeneratorHelper; |
9
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\StringGeneratorHelperInterface; |
10
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLogger; |
11
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLoggerInterface; |
12
|
|
|
|
13
|
|
|
class StringGenerator implements StringGeneratorInterface |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $classStrings; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var StringGeneratorHelperInterface |
23
|
|
|
*/ |
24
|
|
|
private $stringHelper; |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var ClassStoreInterface |
29
|
|
|
*/ |
30
|
|
|
private $classStore; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var VisitedAssociationLoggerInterface |
34
|
|
|
*/ |
35
|
|
|
private $associationLogger; |
36
|
|
|
|
37
|
|
|
private $annotationParser; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ClassStoreInterface $classStore |
41
|
|
|
*/ |
42
|
16 |
|
public function __construct(ClassStoreInterface $classStore) |
43
|
|
|
{ |
44
|
16 |
|
$this->classStore = $classStore; |
45
|
16 |
|
$this->associationLogger = new VisitedAssociationLogger(); |
46
|
16 |
|
$this->stringHelper = new StringGeneratorHelper(); |
47
|
16 |
|
$this->annotationParser = new AnnotationParser(); |
48
|
16 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return VisitedAssociationLoggerInterface |
52
|
|
|
*/ |
53
|
1 |
|
public function getAssociationLogger() |
54
|
|
|
{ |
55
|
1 |
|
return $this->associationLogger; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Build the string representing the single graph item |
60
|
|
|
* |
61
|
|
|
* @param ClassMetadata $class |
62
|
|
|
* @param bool $showFieldsDescription |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
13 |
|
public function getClassString(ClassMetadata $class, $showFieldsDescription = false) |
66
|
|
|
{ |
67
|
13 |
|
$className = $class->getName(); |
68
|
|
|
|
69
|
13 |
|
if (!isset($this->classStrings[$className])) { |
70
|
13 |
|
$this->associationLogger->visitAssociation($className); |
71
|
|
|
|
72
|
13 |
|
$parentFields = $this->getParentFields($class); |
73
|
13 |
|
$fields = $this->getClassFields($class, $parentFields, $showFieldsDescription); |
74
|
|
|
|
75
|
|
|
|
76
|
13 |
|
$methods = $this->annotationParser->getClassMethodsAnnotations($className); |
77
|
|
|
|
78
|
13 |
|
$this->classStrings[$className] = $this->stringHelper->getClassText($className, $fields, $methods); |
79
|
13 |
|
} |
80
|
|
|
|
81
|
13 |
|
return $this->classStrings[$className]; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Recursive function to get all fields in inheritance |
86
|
|
|
* |
87
|
|
|
* @param ClassMetadata $class |
88
|
|
|
* @param array $fields |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
13 |
|
private function getParentFields(ClassMetadata $class, $fields = array()) |
92
|
|
|
{ |
93
|
13 |
|
if ($parent = $this->classStore->getParent($class)) { |
94
|
2 |
|
$parentFields = $parent->getFieldNames(); |
95
|
|
|
|
96
|
2 |
|
foreach ($parentFields as $field) { |
97
|
2 |
|
if (!in_array($field, $fields)) { |
98
|
2 |
|
$fields[] = $field; |
99
|
2 |
|
} |
100
|
2 |
|
} |
101
|
|
|
|
102
|
2 |
|
$fields = $this->getParentFields($parent, $fields); |
103
|
2 |
|
} |
104
|
|
|
|
105
|
13 |
|
return $fields; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param ClassMetadata $class1 |
110
|
|
|
* @param string $association |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
6 |
|
public function getAssociationString(ClassMetadata $class1, $association) |
114
|
|
|
{ |
115
|
6 |
|
$targetClassName = $class1->getAssociationTargetClass($association); |
116
|
6 |
|
$class2 = $this->classStore->getClassByName($targetClassName); |
117
|
6 |
|
$isInverse = $class1->isAssociationInverseSide($association); |
118
|
6 |
|
$associationCount = $this->getClassCount($class1, $association); |
119
|
|
|
|
120
|
6 |
|
if (null === $class2) { |
121
|
2 |
|
return $this->stringHelper->makeSingleSidedLinkString( |
122
|
2 |
|
$this->getClassString($class1), |
123
|
2 |
|
$isInverse, |
124
|
2 |
|
$association, |
125
|
2 |
|
$associationCount, |
126
|
|
|
$targetClassName |
127
|
2 |
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
$reverseAssociationName = $this->getClassReverseAssociationName($class1, $association); |
131
|
|
|
|
132
|
5 |
|
$reverseAssociationCount = 0; |
133
|
5 |
|
$bidirectional = $this->isBidirectional( |
134
|
5 |
|
$reverseAssociationName, |
135
|
5 |
|
$isInverse, |
136
|
|
|
$class2 |
137
|
5 |
|
); |
138
|
|
|
|
139
|
5 |
|
if ($bidirectional) { |
140
|
3 |
|
$reverseAssociationCount = $this->getClassCount($class2, $reverseAssociationName); |
141
|
3 |
|
$bidirectional = true; |
142
|
3 |
|
} |
143
|
|
|
|
144
|
5 |
|
$this->associationLogger->visitAssociation($targetClassName, $reverseAssociationName); |
145
|
|
|
|
146
|
5 |
|
return $this->stringHelper->makeDoubleSidedLinkString( |
147
|
5 |
|
$this->getClassString($class1), |
148
|
5 |
|
$this->getClassString($class2), |
149
|
5 |
|
$bidirectional, |
150
|
5 |
|
$isInverse, |
151
|
5 |
|
$reverseAssociationName, |
152
|
5 |
|
$reverseAssociationCount, |
153
|
5 |
|
$association, |
154
|
|
|
$associationCount |
155
|
5 |
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param boolean $isInverse |
163
|
|
|
* @param string|null $reverseAssociationName |
164
|
|
|
* @param ClassMetadata $class2 |
165
|
|
|
* @return bool |
166
|
|
|
*/ |
167
|
5 |
|
private function isBidirectional( |
168
|
|
|
$reverseAssociationName, |
169
|
|
|
$isInverse, |
170
|
|
|
ClassMetadata $class2 |
171
|
|
|
) { |
172
|
5 |
|
return null !== $reverseAssociationName |
173
|
5 |
|
&& ($isInverse || $class2->isAssociationInverseSide($reverseAssociationName)); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param ClassMetadata $class |
178
|
|
|
* @param string $association |
179
|
|
|
* @return int |
180
|
|
|
*/ |
181
|
6 |
|
private function getClassCount(ClassMetadata $class, $association) |
182
|
|
|
{ |
183
|
6 |
|
return $class->isCollectionValuedAssociation($association) ? 2 : 1; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param ClassMetadata $class |
188
|
|
|
* @param array $parentFields |
189
|
|
|
* @param bool $DisplayAttributesDetails |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
13 |
|
private function getClassFields(ClassMetadata $class, $parentFields, $DisplayAttributesDetails = false) |
193
|
|
|
{ |
194
|
13 |
|
$fields = array(); |
195
|
|
|
|
196
|
13 |
|
foreach ($class->getFieldNames() as $fieldName) { |
197
|
7 |
|
if (in_array($fieldName, $parentFields)) { |
198
|
2 |
|
continue; |
199
|
|
|
} |
200
|
7 |
|
$DisplayAttributesDetails = $this->checkDisplayAnnotations($class->getName(), $DisplayAttributesDetails); |
201
|
|
|
|
202
|
|
|
//$showTypes = $this->getClassAttrPropsDisplay($class->getName(), $showTypes); |
203
|
|
|
|
204
|
7 |
|
$fields[] = $class->isIdentifier($fieldName) ? |
205
|
7 |
|
'+' . $this->makeFieldName($class, $fieldName, $DisplayAttributesDetails) : |
206
|
7 |
|
$this->makeFieldName($class, $fieldName, $DisplayAttributesDetails); |
207
|
13 |
|
} |
208
|
|
|
|
209
|
13 |
|
return $fields; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Returns the $class2 association name for $class1 if reverse related (or null if not) |
214
|
|
|
* |
215
|
|
|
* @param ClassMetadata $class1 |
216
|
|
|
* @param string $association |
217
|
|
|
* |
218
|
|
|
* @return string|null |
219
|
|
|
*/ |
220
|
5 |
|
private function getClassReverseAssociationName(ClassMetadata $class1, $association) |
221
|
|
|
{ |
222
|
|
|
/** |
223
|
|
|
* @var ClassMetadataInfo $class1 |
224
|
|
|
*/ |
225
|
5 |
|
if ($class1->getAssociationMapping($association)['isOwningSide']) { |
226
|
5 |
|
return $class1->getAssociationMapping($association)['inversedBy']; |
227
|
|
|
} |
228
|
|
|
|
229
|
3 |
|
return $class1->getAssociationMapping($association)['mappedBy']; |
230
|
|
|
} |
231
|
|
|
|
232
|
7 |
|
private function makeFieldName(ClassMetadata $class, $fieldName, $showTypes) |
233
|
|
|
{ |
234
|
7 |
|
if ($showTypes) { |
235
|
3 |
|
$helper = new FieldGeneratorHelper(); |
236
|
3 |
|
return $helper->getFullField($class, $fieldName); |
237
|
|
|
} |
238
|
|
|
|
239
|
4 |
|
return $fieldName; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param string $className |
244
|
|
|
* @param boolean $DisplayAttributesDetails |
245
|
|
|
* @return bool |
246
|
|
|
*/ |
247
|
7 |
|
private function checkDisplayAnnotations($className, $DisplayAttributesDetails) |
248
|
|
|
{ |
249
|
7 |
|
$showParams = $this->annotationParser->getClassDisplay($className); |
250
|
7 |
|
if ($DisplayAttributesDetails && $showParams == 'hide') { |
251
|
1 |
|
return false; |
252
|
7 |
|
} elseif (!$DisplayAttributesDetails && $showParams == 'show') { |
253
|
1 |
|
return true; |
254
|
|
|
} |
255
|
|
|
|
256
|
7 |
|
return $DisplayAttributesDetails; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|