1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Migrations; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Migrations\AbstractMigration; |
6
|
|
|
use Doctrine\DBAL\Schema\Schema; |
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Knp\DoctrineBehaviors\Model\Translatable\Translatable; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
use Victoire\Bundle\BusinessEntityBundle\Entity\BusinessProperty; |
12
|
|
|
use Victoire\Bundle\BusinessEntityBundle\EventSubscriber\BusinessEntitySubscriber; |
13
|
|
|
use Victoire\Bundle\CoreBundle\Annotations\BusinessEntity; |
14
|
|
|
use Victoire\Bundle\CoreBundle\Annotations\BusinessProperty as BusinessPropertyAnnotation; |
15
|
|
|
use Victoire\Bundle\CoreBundle\Entity\EntityProxy; |
16
|
|
|
use Victoire\Bundle\CoreBundle\EventSubscriber\WidgetSubscriber; |
17
|
|
|
use Victoire\Bundle\ORMBusinessEntityBundle\Entity\ORMBusinessEntity; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Auto-generated Migration: Please modify to your needs! |
21
|
|
|
*/ |
22
|
|
|
class Version20170217154603 extends AbstractMigration implements ContainerAwareInterface |
23
|
|
|
{ |
24
|
|
|
private $container; |
25
|
|
|
|
26
|
|
|
public function setContainer(ContainerInterface $container = null) |
27
|
|
|
{ |
28
|
|
|
$this->container = $container; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return mixed |
33
|
|
|
*/ |
34
|
|
|
public function getContainer() |
35
|
|
|
{ |
36
|
|
|
return $this->container; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Schema $schema |
41
|
|
|
*/ |
42
|
|
|
public function up(Schema $schema) |
43
|
|
|
{ |
44
|
|
|
// this up() migration is auto-generated, please modify it to your needs |
45
|
|
|
$this->abortIf($this->connection->getDatabasePlatform()->getName() != 'mysql', 'Migration can only be executed safely on \'mysql\'.'); |
46
|
|
|
$this->migrateBusinessEntities(); |
47
|
|
|
$this->migrateEntityProxies(); |
48
|
|
|
$this->migrateWidgetAndViews(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param Schema $schema |
53
|
|
|
*/ |
54
|
|
|
public function down(Schema $schema) |
55
|
|
|
{ |
56
|
|
|
$this->write('Not implemented'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
protected function migrateBusinessEntityName($entity) |
60
|
|
|
{ |
61
|
|
|
/** @var EntityManager $entityManager */ |
62
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
63
|
|
|
$businessEntity = $entityManager->getRepository('VictoireORMBusinessEntityBundle:ORMBusinessEntity') |
64
|
|
|
->createQueryBuilder('proxy') |
65
|
|
|
->where('proxy.name LIKE :prop') |
66
|
|
|
->setParameter(':prop', $entity->getOldBusinessEntityName()) |
67
|
|
|
->getQuery() |
68
|
|
|
->getOneOrNullResult(); |
69
|
|
|
|
70
|
|
|
$entity->setBusinessEntity($businessEntity); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function migrateOldProxies($oldProxy) |
74
|
|
|
{ |
75
|
|
|
/** @var EntityManager $entityManager */ |
76
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
77
|
|
|
|
78
|
|
|
$proxy = new EntityProxy(); |
79
|
|
|
foreach ($oldProxy as $oldProxyPropName => $oldProxyPropValue) { |
80
|
|
|
if ($oldProxyPropName === 'id') { |
81
|
|
|
$proxy->setId($oldProxyPropValue); |
82
|
|
|
} elseif ($oldProxyPropValue !== null) { |
83
|
|
|
$businessEntity = $entityManager->getRepository('VictoireORMBusinessEntityBundle:ORMBusinessEntity') |
84
|
|
|
->createQueryBuilder('proxy') |
85
|
|
|
->where('proxy.name LIKE :prop') |
86
|
|
|
->setParameter(':prop', str_replace('_id', '', $oldProxyPropName)) |
87
|
|
|
->getQuery() |
88
|
|
|
->getOneOrNullResult(); |
89
|
|
|
$proxy->setBusinessEntity($businessEntity); |
90
|
|
|
$proxy->setRessourceId($oldProxyPropValue); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $proxy; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function migrateEntityProxies() |
98
|
|
|
{ |
99
|
|
|
/** @var \Doctrine\DBAL\Connection $conn */ |
100
|
|
|
$conn = $this->getContainer()->get('database_connection'); |
101
|
|
|
/** @var EntityManager $entityManager */ |
102
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
103
|
|
|
|
104
|
|
|
$sql = 'SELECT * FROM vic_entity_proxy'; |
105
|
|
|
$oldProxies = $conn->fetchAll($sql); |
106
|
|
|
|
107
|
|
|
foreach ($oldProxies as $k => $oldProxy) { |
108
|
|
|
$proxy = $this->migrateOldProxies($oldProxy); |
109
|
|
|
$entityManager->persist($proxy); |
110
|
|
|
// Force the ID |
111
|
|
|
$metadata = $entityManager->getClassMetaData(get_class($proxy)); |
112
|
|
|
$metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$entityManager->flush(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function migrateWidgetAndViews() |
119
|
|
|
{ |
120
|
|
|
/** @var \Doctrine\DBAL\Connection $conn */ |
121
|
|
|
$conn = $this->getContainer()->get('database_connection'); |
|
|
|
|
122
|
|
|
/** @var EntityManager $entityManager */ |
123
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
124
|
|
|
|
125
|
|
|
foreach ($entityManager->getRepository('VictoireBusinessPageBundle:BusinessTemplate')->findAll() as $bt) { |
126
|
|
|
$this->migrateBusinessEntityName($bt); |
127
|
|
|
} |
128
|
|
|
foreach ($entityManager->getRepository('VictoireWidgetBundle:Widget')->findAll() as $widget) { |
129
|
|
|
if ($widget->getOldBusinessEntityName()) { |
130
|
|
|
$this->migrateBusinessEntityName($widget); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Do not uselessly regenerate the viewCss threw the onFlush event. It led to an exception |
135
|
|
|
$evm = $entityManager->getEventManager(); |
136
|
|
|
foreach ($entityManager->getEventManager()->getListeners() as $event => $listeners) { |
137
|
|
|
foreach ($listeners as $key => $listener) { |
138
|
|
|
if ($listener instanceof WidgetSubscriber) { |
139
|
|
|
$evm->removeEventListener(['onFlush'], $listener); |
140
|
|
|
} |
141
|
|
|
if ($listener instanceof BusinessEntitySubscriber) { |
142
|
|
|
$evm->removeEventListener(['postUpdate'], $listener); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$entityManager->flush(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Parse the given Class to find some annotations related to BusinessEntities. |
152
|
|
|
*/ |
153
|
|
|
public function parse(\ReflectionClass $class) |
154
|
|
|
{ |
155
|
|
|
$businessEntity = null; |
156
|
|
|
$classPath = dirname($class->getFileName()); |
157
|
|
|
$inPaths = false; |
158
|
|
|
foreach ($this->getContainer()->getParameter('victoire_core.base_paths') as $key => $_path) { |
159
|
|
|
//Check the entity path is in watching paths |
160
|
|
|
if (strpos($classPath, realpath($_path)) === 0) { |
161
|
|
|
$inPaths = true; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
if ($inPaths) { |
165
|
|
|
$classAnnotations = $this->getContainer()->get('annotation_reader')->getClassAnnotations($class); |
166
|
|
View Code Duplication |
if (!empty($classAnnotations)) { |
|
|
|
|
167
|
|
|
foreach ($classAnnotations as $key => $annot) { |
168
|
|
|
if (!is_numeric($key)) { |
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
$classAnnotations[get_class($annot)] = $annot; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
// Evaluate Entity annotation |
176
|
|
|
if (isset($classAnnotations['Victoire\Bundle\CoreBundle\Annotations\BusinessEntity'])) { |
177
|
|
|
/** @var BusinessEntity $annotationObj */ |
178
|
|
|
$annotationObj = $classAnnotations['Victoire\Bundle\CoreBundle\Annotations\BusinessEntity']; |
179
|
|
|
$businessEntity = $this->createBusinessEntity( |
180
|
|
|
$class->getName(), |
|
|
|
|
181
|
|
|
$annotationObj, |
182
|
|
|
$this->loadBusinessProperties($class) |
183
|
|
|
); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return $businessEntity; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* load business properties from ReflectionClass. |
192
|
|
|
* |
193
|
|
|
* @return array |
194
|
|
|
**/ |
195
|
|
|
protected function loadBusinessProperties(\ReflectionClass $class) |
196
|
|
|
{ |
197
|
|
|
$reader = $this->getContainer()->get('annotation_reader'); |
198
|
|
|
$businessProperties = []; |
199
|
|
|
$properties = $class->getProperties(); |
200
|
|
|
$traits = $class->getTraits(); |
201
|
|
|
$className = $class->getName(); |
|
|
|
|
202
|
|
|
// if the class is translatable, then parse annotations on it's translation class |
203
|
|
|
if (array_key_exists(Translatable::class, $traits)) { |
204
|
|
|
$translation = new \ReflectionClass($className::getTranslationEntityClass()); |
205
|
|
|
$translationProperties = $translation->getProperties(); |
206
|
|
|
$properties = array_merge($properties, $translationProperties); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
foreach ($properties as $property) { |
210
|
|
|
$annotations = $reader->getPropertyAnnotations($property); |
211
|
|
|
foreach ($annotations as $key => $annotationObj) { |
212
|
|
|
if ($annotationObj instanceof BusinessPropertyAnnotation && !in_array($class, $businessProperties)) { |
213
|
|
|
$businessProperties[$property->name] = $annotationObj; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
// we load business properties of parents recursively |
218
|
|
|
// because they are defined by an annotation not by the property type(private, protected, public) |
219
|
|
|
$parentClass = $class->getParentClass(); |
220
|
|
|
if ($parentClass) { |
221
|
|
|
//load parent properties recursively |
222
|
|
|
$parentProperties = $this->loadBusinessProperties(new \ReflectionClass($parentClass->getName())); |
223
|
|
|
foreach ($parentProperties as $propertyName => $parentProperty) { |
224
|
|
|
if (!array_key_exists($propertyName, $businessProperties)) { |
225
|
|
|
$businessProperties[$propertyName] = $parentProperty; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $businessProperties; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param $className |
235
|
|
|
* @param $annotationObj |
236
|
|
|
* @param $businessProperties |
237
|
|
|
* |
238
|
|
|
* @return ORMBusinessEntity |
239
|
|
|
*/ |
240
|
|
|
private function createBusinessEntity($className, $annotationObj, $businessProperties) |
241
|
|
|
{ |
242
|
|
|
$businessEntity = new ORMBusinessEntity(); |
243
|
|
|
$classNameArray = explode('\\', $className); |
244
|
|
|
$entityName = array_pop($classNameArray); |
245
|
|
|
$businessEntity->setName($entityName); |
246
|
|
|
$businessEntity->setClass($className); |
247
|
|
|
$businessEntity->setAvailableWidgets($annotationObj->getWidgets()); |
248
|
|
|
//parse the array of the annotation reader |
249
|
|
|
foreach ($businessProperties as $propertyName => $property) { |
250
|
|
|
$businessProperty = new BusinessProperty(); |
251
|
|
|
$businessProperty->setTypes($property->getTypes()); |
252
|
|
|
$businessProperty->setBusinessEntity($businessEntity); |
253
|
|
|
$businessProperty->setName($propertyName); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
return $businessEntity; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
private function migrateBusinessEntities() |
260
|
|
|
{ |
261
|
|
|
/** @var EntityManager $entityManager */ |
262
|
|
|
$entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); |
263
|
|
|
|
264
|
|
|
foreach ($this->getContainer()->get('victoire_business_entity.annotation_driver')->getAllClassNames() as $className) { |
265
|
|
|
$businessEntity = $this->parse(new \ReflectionClass($className)); |
|
|
|
|
266
|
|
|
if ($businessEntity) { |
267
|
|
|
$entityManager->persist($businessEntity); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$entityManager->flush(); |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.