1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Onurb\Doctrine\ORMMetadataGrapher; |
21
|
|
|
|
22
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
23
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\AnnotationParser; |
24
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\ClassStore; |
25
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\ColorManager; |
26
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\NotesManager; |
27
|
|
|
use Onurb\Doctrine\ORMMetadataGrapher\YumlMetadataGrapher\StringGenerator; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Utility to generate yUML compatible strings from metadata graphs |
31
|
|
|
* |
32
|
|
|
* @license MIT |
33
|
|
|
* @link http://www.doctrine-project.org/ |
34
|
|
|
* @author Marco Pivetta <[email protected]> |
35
|
|
|
* @author Bruno Heron <<[email protected]> |
36
|
|
|
*/ |
37
|
|
|
class YUMLMetadataGrapher implements YUMLMetadataGrapherInterface |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var ClassStore |
41
|
|
|
*/ |
42
|
|
|
private $classStore; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var StringGenerator |
46
|
|
|
*/ |
47
|
|
|
private $stringGenerator; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $str = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ColorManager |
56
|
|
|
*/ |
57
|
|
|
private $colorManager; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var AnnotationParser |
61
|
|
|
*/ |
62
|
|
|
private $annotationParser; |
63
|
|
|
|
64
|
|
|
|
65
|
32 |
|
public function __construct() |
66
|
|
|
{ |
67
|
32 |
|
$this->annotationParser = new AnnotationParser(); |
68
|
32 |
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Generate a yUML compatible `dsl_text` to describe a given array of entities |
73
|
|
|
* |
74
|
|
|
* @param ClassMetadata[] $metadata |
75
|
|
|
* @param boolean $showFieldsDescription |
76
|
|
|
* @param array $colors |
77
|
|
|
* @param array $notes |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
32 |
|
public function generateFromMetadata( |
81
|
|
|
array $metadata, |
82
|
|
|
$showFieldsDescription = false, |
83
|
|
|
$colors = array(), |
84
|
|
|
$notes = array() |
85
|
|
|
) { |
86
|
32 |
|
$this->classStore = new ClassStore($metadata); |
87
|
32 |
|
$this->stringGenerator = new StringGenerator($this->classStore); |
88
|
32 |
|
$this->colorManager = new ColorManager($this->stringGenerator, $this->classStore); |
89
|
|
|
|
90
|
32 |
|
$annotations = $this->annotationParser->getAnnotations($metadata); |
91
|
|
|
|
92
|
31 |
|
$colors = array_merge($colors, $annotations['colors']); |
93
|
31 |
|
$notes = array_merge($notes, $annotations['notes']); |
94
|
|
|
|
95
|
31 |
|
foreach ($metadata as $class) { |
96
|
31 |
|
$this->writeParentAssociation($class); |
97
|
31 |
|
$this->dispatchStringWriter($class, $class->getAssociationNames()); |
98
|
31 |
|
} |
99
|
|
|
|
100
|
31 |
|
$this->addColors($metadata, $colors); |
101
|
31 |
|
$this->addNotes($notes); |
102
|
|
|
|
103
|
31 |
|
return implode(',', $this->str); |
104
|
|
|
} |
105
|
|
|
|
106
|
31 |
|
private function addColors($metadata, $colors) |
107
|
|
|
{ |
108
|
31 |
|
if (!empty($colors)) { |
109
|
5 |
|
$return = $this->colorManager->getColorStrings($metadata, $colors); |
110
|
5 |
|
$this->str = array_merge($this->str, $return); |
111
|
5 |
|
} |
112
|
31 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param ClassMetadata $class |
116
|
|
|
* @param $associations |
117
|
|
|
*/ |
118
|
31 |
|
private function dispatchStringWriter(ClassMetadata $class, $associations) |
119
|
|
|
{ |
120
|
31 |
|
if (empty($associations)) { |
121
|
15 |
|
$this->writeSingleClass($class); |
122
|
15 |
|
} else { |
123
|
19 |
|
$this->writeClassAssociations($class, $associations); |
124
|
|
|
} |
125
|
31 |
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param ClassMetadata $class |
129
|
|
|
*/ |
130
|
31 |
|
private function writeParentAssociation(ClassMetadata $class) |
131
|
|
|
{ |
132
|
31 |
|
if ($parent = $this->classStore->getParent($class)) { |
133
|
4 |
|
$this->str[] = $this->stringGenerator->getClassString($parent) . '^' |
134
|
4 |
|
. $this->stringGenerator->getClassString($class); |
135
|
4 |
|
} |
136
|
31 |
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param ClassMetadata $class |
140
|
|
|
*/ |
141
|
15 |
|
private function writeSingleClass(ClassMetadata $class) |
142
|
|
|
{ |
143
|
15 |
|
if (!$this->stringGenerator->getAssociationLogger()->isVisitedAssociation($class->getName())) { |
144
|
10 |
|
$this->str[] = $this->stringGenerator->getClassString($class); |
145
|
10 |
|
} |
146
|
15 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param ClassMetadata $class |
150
|
|
|
* @param array $associations |
151
|
|
|
*/ |
152
|
19 |
|
private function writeClassAssociations(ClassMetadata $class, $associations) |
153
|
|
|
{ |
154
|
19 |
|
$inheritanceAssociations = $this->getInheritanceAssociations($class); |
155
|
|
|
|
156
|
19 |
|
foreach ($associations as $associationName) { |
157
|
19 |
|
if (in_array($associationName, $inheritanceAssociations)) { |
158
|
1 |
|
continue; |
159
|
|
|
} |
160
|
|
|
|
161
|
19 |
|
$this->writeAssociation($class, $associationName); |
162
|
19 |
|
} |
163
|
19 |
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param ClassMetadata $class |
167
|
|
|
* @param string $association |
168
|
|
|
*/ |
169
|
19 |
|
private function writeAssociation(ClassMetadata $class, $association) |
170
|
|
|
{ |
171
|
19 |
|
if ($this->stringGenerator->getAssociationLogger() |
172
|
19 |
|
->visitAssociation($class->getName(), $association) |
173
|
19 |
|
) { |
174
|
19 |
|
$this->str[] = $this->stringGenerator->getAssociationString($class, $association); |
175
|
19 |
|
} |
176
|
19 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Recursive function to get all associations in inheritance |
180
|
|
|
* |
181
|
|
|
* @param ClassMetadata $class |
182
|
|
|
* @param array $associations |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
19 |
|
private function getInheritanceAssociations(ClassMetadata $class, $associations = array()) |
186
|
|
|
{ |
187
|
19 |
|
if ($parent = $this->classStore->getParent($class)) { |
188
|
1 |
|
foreach ($parent->getAssociationNames() as $association) { |
189
|
1 |
|
if (!in_array($association, $associations)) { |
190
|
1 |
|
$associations[] = $association; |
191
|
1 |
|
} |
192
|
1 |
|
} |
193
|
1 |
|
$associations = $this->getInheritanceAssociations($parent, $associations); |
194
|
1 |
|
} |
195
|
|
|
|
196
|
19 |
|
return $associations; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param array $notes |
201
|
|
|
*/ |
202
|
31 |
|
private function addNotes($notes) |
203
|
|
|
{ |
204
|
31 |
|
if (!empty($notes)) { |
205
|
4 |
|
$notesManager = new NotesManager($this->classStore, $this->stringGenerator); |
206
|
4 |
|
$this->str = array_merge($this->str, $notesManager->getNotesStrings($notes)); |
207
|
4 |
|
} |
208
|
31 |
|
} |
209
|
|
|
} |
210
|
|
|
|