1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
6
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\StringGeneratorHelper; |
7
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\StringGeneratorHelperInterface; |
8
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLogger; |
9
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator\VisitedAssociationLoggerInterface; |
10
|
|
|
|
11
|
|
|
class StringGenerator implements StringGeneratorInterface |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $classStrings; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var StringGeneratorHelperInterface |
21
|
|
|
*/ |
22
|
|
|
protected $stringHelper; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ClassStoreInterface |
27
|
|
|
*/ |
28
|
|
|
protected $classStore; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var VisitedAssociationLoggerInterface |
32
|
|
|
*/ |
33
|
|
|
protected $associationLogger; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ClassStoreInterface $classStore |
37
|
|
|
*/ |
38
|
11 |
|
public function __construct(ClassStoreInterface $classStore) |
39
|
|
|
{ |
40
|
11 |
|
$this->classStore = $classStore; |
41
|
11 |
|
$this->associationLogger = new VisitedAssociationLogger(); |
42
|
11 |
|
$this->stringHelper = new StringGeneratorHelper(); |
43
|
11 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return VisitedAssociationLoggerInterface |
47
|
|
|
*/ |
48
|
1 |
|
public function getAssociationLogger() |
49
|
|
|
{ |
50
|
1 |
|
return $this->associationLogger; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Build the string representing the single graph item |
55
|
|
|
* |
56
|
|
|
* @param ClassMetadata $class |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
9 |
|
public function getClassString(ClassMetadata $class) |
61
|
|
|
{ |
62
|
9 |
|
$className = $class->getName(); |
63
|
|
|
|
64
|
9 |
|
if (!isset($this->classStrings[$className])) { |
65
|
9 |
|
$this->associationLogger->visitAssociation($className); |
66
|
|
|
|
67
|
9 |
|
$parentFields = $this->getParentFields($class); |
68
|
9 |
|
$fields = $this->getClassFields($class, $parentFields); |
69
|
|
|
|
70
|
9 |
|
$this->classStrings[$className] = $this->stringHelper->getClassText($className, $fields); |
71
|
9 |
|
} |
72
|
|
|
|
73
|
9 |
|
return $this->classStrings[$className]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Recursive function to get all fields in inheritance |
78
|
|
|
* |
79
|
|
|
* @param ClassMetadata $class |
80
|
|
|
* @param array $fields |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
9 |
|
public function getParentFields(ClassMetadata $class, $fields = array()) |
84
|
|
|
{ |
85
|
9 |
|
if ($parent = $this->classStore->getParent($class)) { |
86
|
2 |
|
$parentFields = $parent->getFieldNames(); |
87
|
2 |
|
foreach ($parentFields as $field) { |
88
|
2 |
|
if (!in_array($field, $fields)) { |
89
|
2 |
|
$fields[] = $field; |
90
|
2 |
|
} |
91
|
2 |
|
} |
92
|
2 |
|
$fields = $this->getParentFields($parent, $fields); |
93
|
2 |
|
} |
94
|
|
|
|
95
|
9 |
|
return $fields; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param ClassMetadata $class1 |
100
|
|
|
* @param string $association |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
6 |
|
public function getAssociationString(ClassMetadata $class1, $association) |
104
|
|
|
{ |
105
|
6 |
|
$targetClassName = $class1->getAssociationTargetClass($association); |
106
|
6 |
|
$class2 = $this->classStore->getClassByName($targetClassName); |
107
|
6 |
|
$isInverse = $class1->isAssociationInverseSide($association); |
108
|
6 |
|
$associationCount = $this->getClassCount($class1, $association); |
109
|
|
|
|
110
|
6 |
|
if (null === $class2) { |
111
|
2 |
|
return $this->stringHelper->makeSingleSidedLinkString( |
112
|
2 |
|
$this->getClassString($class1), |
113
|
2 |
|
$isInverse, |
114
|
2 |
|
$association, |
115
|
2 |
|
$associationCount, |
116
|
|
|
$targetClassName |
117
|
2 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
5 |
|
$reverseAssociationName = $this->getClassReverseAssociationName($class1, $association); |
121
|
|
|
|
122
|
5 |
|
$reverseAssociationCount = 0; |
123
|
5 |
|
$bidirectional = false; |
124
|
|
|
|
125
|
5 |
|
if (null !== $reverseAssociationName |
126
|
5 |
|
&& ($isInverse || $class2->isAssociationInverseSide($reverseAssociationName))) { |
127
|
3 |
|
$reverseAssociationCount = $this->getClassCount($class2, $reverseAssociationName); |
128
|
3 |
|
$bidirectional = true; |
129
|
3 |
|
} |
130
|
|
|
|
131
|
5 |
|
$this->associationLogger->visitAssociation($targetClassName, $reverseAssociationName); |
132
|
|
|
|
133
|
5 |
|
return $this->stringHelper->makeDoubleSidedLinkString( |
134
|
5 |
|
$this->getClassString($class1), |
135
|
5 |
|
$this->getClassString($class2), |
136
|
5 |
|
$bidirectional, |
137
|
5 |
|
$isInverse, |
138
|
5 |
|
$reverseAssociationName, |
139
|
5 |
|
$reverseAssociationCount, |
140
|
5 |
|
$association, |
141
|
|
|
$associationCount |
142
|
5 |
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param ClassMetadata $class |
147
|
|
|
* @param string $association |
148
|
|
|
* @return int |
149
|
|
|
*/ |
150
|
6 |
|
private function getClassCount(ClassMetadata $class, $association) |
151
|
|
|
{ |
152
|
6 |
|
return $class->isCollectionValuedAssociation($association) ? 2 : 1; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param ClassMetadata $class |
157
|
|
|
* @param array $parentFields |
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
9 |
|
private function getClassFields(ClassMetadata $class, $parentFields) |
161
|
|
|
{ |
162
|
9 |
|
$fields = array(); |
163
|
|
|
|
164
|
9 |
|
foreach ($class->getFieldNames() as $fieldName) { |
165
|
3 |
|
if (in_array($fieldName, $parentFields)) { |
166
|
2 |
|
continue; |
167
|
|
|
} |
168
|
|
|
|
169
|
3 |
|
$fields[] = $class->isIdentifier($fieldName) ? '+' . $fieldName : $fieldName; |
170
|
9 |
|
} |
171
|
|
|
|
172
|
9 |
|
return $fields; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the $class2 association name for $class1 if reverse related (or null if not) |
177
|
|
|
* |
178
|
|
|
* @param ClassMetadata $class1 |
179
|
|
|
* @param string $association |
180
|
|
|
* |
181
|
|
|
* @return string|null |
182
|
|
|
*/ |
183
|
5 |
|
private function getClassReverseAssociationName(ClassMetadata $class1, $association) |
184
|
|
|
{ |
185
|
5 |
|
if ($class1->getAssociationMapping($association)['isOwningSide']) { |
186
|
5 |
|
return $class1->getAssociationMapping($association)['inversedBy']; |
187
|
|
|
} |
188
|
|
|
|
189
|
3 |
|
return $class1->getAssociationMapping($association)['mappedBy']; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|