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 |
|
$class1Count = $class1->isCollectionValuedAssociation($association) ? 2 : 1; |
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 |
|
$class1Count, |
116
|
|
|
$targetClassName |
117
|
2 |
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
5 |
|
$class1SideName = $association; |
121
|
5 |
|
$class2SideName = $this->getClassReverseAssociationName($class1, $association); |
122
|
5 |
|
$class2Count = 0; |
123
|
5 |
|
$bidirectional = false; |
124
|
|
|
|
125
|
5 |
|
if (null !== $class2SideName) { |
126
|
3 |
|
if ($isInverse || $class2->isAssociationInverseSide($class2SideName)) { |
127
|
3 |
|
$class2Count = $class2->isCollectionValuedAssociation($class2SideName) ? 2 : 1; |
128
|
3 |
|
$bidirectional = true; |
129
|
3 |
|
} |
130
|
3 |
|
} |
131
|
|
|
|
132
|
5 |
|
$this->associationLogger->visitAssociation($targetClassName, $class2SideName); |
133
|
|
|
|
134
|
5 |
|
return $this->stringHelper->makeDoubleSidedLinkString( |
135
|
5 |
|
$this->getClassString($class1), |
136
|
5 |
|
$this->getClassString($class2), |
137
|
5 |
|
$bidirectional, |
138
|
5 |
|
$isInverse, |
139
|
5 |
|
$class2SideName, |
140
|
5 |
|
$class2Count, |
141
|
5 |
|
$class1SideName, |
142
|
|
|
$class1Count |
143
|
5 |
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param ClassMetadata $class |
148
|
|
|
* @param array $parentFields |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
9 |
|
private function getClassFields(ClassMetadata $class, $parentFields) |
152
|
|
|
{ |
153
|
9 |
|
$fields = array(); |
154
|
|
|
|
155
|
9 |
|
foreach ($class->getFieldNames() as $fieldName) { |
156
|
3 |
|
if (in_array($fieldName, $parentFields)) { |
157
|
2 |
|
continue; |
158
|
|
|
} |
159
|
|
|
|
160
|
3 |
|
$fields[] = $class->isIdentifier($fieldName) ? '+' . $fieldName : $fieldName; |
161
|
9 |
|
} |
162
|
|
|
|
163
|
9 |
|
return $fields; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the $class2 association name for $class1 if reverse related (or null if not) |
168
|
|
|
* |
169
|
|
|
* @param ClassMetadata $class1 |
170
|
|
|
* @param string $association |
171
|
|
|
* |
172
|
|
|
* @return string|null |
173
|
|
|
*/ |
174
|
5 |
|
private function getClassReverseAssociationName(ClassMetadata $class1, $association) |
175
|
|
|
{ |
176
|
5 |
|
if ($class1->getAssociationMapping($association)['isOwningSide']) { |
177
|
5 |
|
return $class1->getAssociationMapping($association)['inversedBy']; |
178
|
|
|
} |
179
|
|
|
|
180
|
3 |
|
return $class1->getAssociationMapping($association)['mappedBy']; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|