1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\CoreBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Yaml\Yaml; |
10
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
12
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
13
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
14
|
|
|
|
15
|
|
|
class Fifree2configuratorimportCommand extends ContainerAwareCommand |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
private $forceupdate = false; |
19
|
|
|
private $verboso = false; |
|
|
|
|
20
|
|
|
private $dbutility; |
21
|
|
|
/* @var $em \Doctrine\ORM\EntityManager */ |
22
|
|
|
private $em; |
23
|
|
|
|
24
|
4 |
|
protected function configure() |
25
|
|
|
{ |
26
|
4 |
|
$this |
27
|
4 |
|
->setName('fifree2:configuratorimport') |
28
|
4 |
|
->setDescription('Configuratore per Fifree') |
29
|
4 |
|
->setHelp('Importa la configurazione di fifree da file fixtures.yml') |
30
|
4 |
|
->addOption('forceupdate', null, InputOption::VALUE_NONE, 'Forza update di record con id già presente') |
31
|
4 |
|
->addOption('truncatetables', null, InputOption::VALUE_NONE, 'Esegue una truncate della tabelle') |
32
|
4 |
|
->addOption('verboso', null, InputOption::VALUE_NONE, 'Visualizza tutti i messaggi di importazione'); |
33
|
4 |
|
} |
34
|
|
|
|
35
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
36
|
|
|
{ |
37
|
1 |
|
$this->output = $output; |
|
|
|
|
38
|
1 |
|
$this->forceupdate = $input->getOption('forceupdate'); |
|
|
|
|
39
|
1 |
|
$this->verboso = $input->getOption('verboso'); |
|
|
|
|
40
|
1 |
|
$this->truncatetables = $input->getOption('truncatetables'); |
|
|
|
|
41
|
1 |
|
$this->dbutility = $this->getContainer()->get("ficorebundle.database.utility"); |
|
|
|
|
42
|
1 |
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
|
|
|
43
|
|
|
|
44
|
1 |
|
$this->checkSchemaStatus(); |
45
|
|
|
|
46
|
1 |
|
$fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
|
|
|
47
|
1 |
|
return $this->import($fixturefile); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
protected function import($fixturefile) |
51
|
|
|
{ |
52
|
1 |
|
$fs = new Filesystem; |
53
|
1 |
|
if ($fs->exists($fixturefile)) { |
54
|
1 |
|
$fixtures = Yaml::parse(file_get_contents($fixturefile)); |
55
|
1 |
|
$msg = "<info>Trovate " . count($fixtures) . " entities nel file " . $fixturefile . "</info>"; |
|
|
|
|
56
|
1 |
|
$this->output->writeln($msg); |
57
|
|
|
|
58
|
1 |
|
if ($this->truncatetables) { |
59
|
|
|
foreach ($fixtures as $entityclass => $fixture) { |
60
|
|
|
$this->truncateTable($entityclass); |
61
|
|
|
} |
62
|
|
|
} |
63
|
1 |
|
$sortedEntities = $this->getSortedEntities($fixtures); |
64
|
1 |
|
foreach ($sortedEntities as $entityclass => $fixture) { |
65
|
1 |
|
$this->executeImport($entityclass, $fixture); |
66
|
1 |
|
} |
67
|
1 |
|
return 0; |
68
|
|
|
} else { |
69
|
1 |
|
$msgerr = "<error>Non trovato file " . $fixturefile . "</error>"; |
|
|
|
|
70
|
1 |
|
$this->output->writeln($msgerr); |
71
|
1 |
|
return 1; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
private function checkSchemaStatus() |
76
|
|
|
{ |
77
|
1 |
|
$kernel = $this->getContainer()->get("kernel"); |
|
|
|
|
78
|
1 |
|
$application = new Application($kernel); |
79
|
1 |
|
$application->setAutoExit(false); |
80
|
|
|
|
81
|
1 |
|
$input = new ArrayInput(array( |
82
|
1 |
|
'command' => 'doctrine:schema:update', |
83
|
|
|
// (optional) define the value of command arguments |
84
|
1 |
|
'--dump-sql' => true, |
85
|
|
|
// (optional) pass options to the command |
86
|
1 |
|
'--env' => $kernel->getEnvironment(), |
87
|
1 |
|
)); |
88
|
|
|
|
89
|
|
|
// You can use NullOutput() if you don't need the output |
90
|
1 |
|
$output = new BufferedOutput(); |
91
|
1 |
|
$application->run($input, $output); |
92
|
|
|
|
93
|
|
|
// return the output, don't use if you used NullOutput() |
94
|
1 |
|
$content = $output->fetch(); |
95
|
1 |
|
if (strpos($content, 'Nothing to update') == false) { |
96
|
|
|
$msgerr = "<error>Attenzione, lo schema database non è aggiornato, verrà comunque tentata l'importazione</error>"; |
97
|
|
|
$this->output->writeln($msgerr); |
98
|
|
|
sleep(3); |
99
|
|
|
} |
100
|
1 |
|
} |
101
|
|
|
|
102
|
|
|
private function truncateTable($entityclass) |
103
|
|
|
{ |
104
|
|
|
$tablename = $this->dbutility->getTableFromEntity($entityclass); |
105
|
|
|
if ($tablename) { |
106
|
|
|
$msg = "<info>TRUNCATE della tabella " . $tablename . " (" . $entityclass . ")</info>"; |
|
|
|
|
107
|
|
|
$this->output->writeln($msg); |
108
|
|
|
$this->dbutility->truncatetable($tablename, true); |
109
|
|
|
} else { |
110
|
|
|
$msgerr = "<error>Tabella non trovata per entity " . $entityclass . "</error>"; |
|
|
|
|
111
|
|
|
$this->output->writeln($msgerr); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
private function getSortedEntities($fixtures) |
116
|
|
|
{ |
117
|
1 |
|
$sortedEntities = array(); |
|
|
|
|
118
|
1 |
|
$entities = array(); |
|
|
|
|
119
|
|
|
|
120
|
1 |
|
$sortedEntities = $this->sortEntities($fixtures); |
121
|
|
|
|
122
|
1 |
|
foreach ($sortedEntities as $fixture) { |
123
|
1 |
|
$entities[$fixture] = $fixtures[$fixture]; |
124
|
1 |
|
} |
125
|
1 |
|
return $entities; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
130
|
|
|
*/ |
131
|
1 |
|
private function sortEntities($fixtures) |
132
|
|
|
{ |
133
|
1 |
|
$sortedEntities = array(); |
134
|
1 |
|
foreach ($fixtures as $entityclass => $fixture) { |
135
|
1 |
|
$hasentityjoin = $this->dbutility->entityHasJoinTables($entityclass); |
136
|
1 |
|
if ($hasentityjoin) { |
137
|
1 |
|
$entityjoins = $this->dbutility->getEntityJoinTables($entityclass); |
138
|
1 |
|
foreach ($entityjoins as $keyjoin => $entityjoin) { |
139
|
1 |
|
$sortedEntities[] = $keyjoin; |
140
|
1 |
|
} |
141
|
1 |
|
} |
142
|
1 |
|
$sortedEntities[] = $entityclass; |
143
|
1 |
|
} |
144
|
1 |
|
return $sortedEntities; |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
private function executeImport($entityclass, $fixture) |
148
|
|
|
{ |
149
|
1 |
|
$msg = "<info>Trovati " . count($fixture) . " record per l'entity " . $entityclass . "</info>"; |
|
|
|
|
150
|
1 |
|
$this->output->writeln($msg); |
151
|
1 |
|
foreach ($fixture as $record) { |
152
|
1 |
|
$objrecord = $this->em->getRepository($entityclass)->find($record["id"]); |
|
|
|
|
153
|
1 |
|
if ($objrecord) { |
154
|
1 |
|
if ($this->forceupdate) { |
155
|
1 |
|
$retcode = $this->executeUpdate($entityclass, $record, $objrecord); |
156
|
1 |
|
if ($retcode !== 0) { |
157
|
|
|
return 1; |
158
|
|
|
} |
159
|
1 |
|
} else { |
160
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
161
|
|
|
. " non modificata, specificare l'opzione --forceupdate " |
162
|
|
|
. "per sovrascrivere record presenti</error>"; |
|
|
|
|
163
|
|
|
$this->output->writeln($msgerr); |
164
|
|
|
} |
165
|
1 |
|
} else { |
166
|
1 |
|
$retcode = $this->executeInsert($entityclass, $record); |
167
|
1 |
|
if ($retcode !== 0) { |
168
|
|
|
return 1; |
169
|
|
|
} |
170
|
|
|
} |
171
|
1 |
|
} |
172
|
1 |
|
} |
173
|
|
|
|
174
|
1 |
|
private function executeInsert($entityclass, $record) |
175
|
|
|
{ |
176
|
1 |
|
$objrecord = new $entityclass(); |
177
|
|
|
|
178
|
1 |
|
foreach ($record as $key => $value) { |
179
|
1 |
|
if ($key !== 'id' && $value) { |
180
|
1 |
|
$propertyEntity = $this->dbutility->getEntityProperties($key, $objrecord); |
181
|
1 |
|
$getfieldname = $propertyEntity["get"]; |
|
|
|
|
182
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
183
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
|
|
|
|
184
|
1 |
|
if ($fieldtype === 'datetime') { |
185
|
|
|
$date = new \DateTime(); |
186
|
|
|
$date->setTimestamp($value); |
187
|
|
|
$msgok = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
188
|
|
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
189
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
|
|
|
190
|
|
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "") |
|
|
|
|
191
|
|
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
|
|
|
192
|
|
|
$this->output->writeln($msgok); |
193
|
|
|
$objrecord->$setfieldname($date); |
194
|
|
|
continue; |
195
|
|
|
} |
196
|
1 |
|
if (is_array($value)) { |
197
|
|
|
$msgarray = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
198
|
|
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
199
|
|
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
|
|
|
200
|
|
|
. json_encode($value) . " in formato array" . "</info>"; |
|
|
|
|
201
|
|
|
$this->output->writeln($msgarray); |
202
|
|
|
$objrecord->$setfieldname($value); |
203
|
|
|
continue; |
204
|
|
|
} |
205
|
|
|
|
206
|
1 |
|
$joincolumn = $this->dbutility->getJoinTableField($entityclass, $key); |
|
|
|
|
207
|
1 |
|
$joincolumnproperty = $this->dbutility->getJoinTableFieldProperty($entityclass, $key); |
208
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
209
|
|
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
210
|
|
|
$msgok = "<info>Inserimento " . $entityclass . " con id " . $record["id"] |
|
|
|
|
211
|
|
|
. " per campo " . $key |
|
|
|
|
212
|
|
|
. " con valore " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
213
|
|
|
$this->output->writeln($msgok); |
214
|
|
|
$joinobj = $this->dbutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
215
|
|
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
216
|
|
|
$objrecord->$setfieldname($joincolumnobj); |
217
|
|
|
continue; |
218
|
|
|
} |
219
|
|
|
|
220
|
1 |
|
$objrecord->$setfieldname($value); |
221
|
1 |
|
} |
222
|
1 |
|
} |
223
|
1 |
|
$this->em->persist($objrecord); |
224
|
1 |
|
$this->em->flush(); |
225
|
1 |
|
$this->em->clear(); |
226
|
1 |
|
$infomsg = "<info>" . $entityclass . " con id " . $objrecord->getId() . " aggiunta</info>"; |
|
|
|
|
227
|
1 |
|
$this->output->writeln($infomsg); |
228
|
1 |
|
if ($record["id"] !== $objrecord->getId()) { |
|
|
|
|
229
|
|
|
try { |
230
|
1 |
|
$qb = $this->em->createQueryBuilder(); |
231
|
1 |
|
$q = $qb->update($entityclass, 'u') |
|
|
|
|
232
|
1 |
|
->set('u.id', ":newid") |
|
|
|
|
233
|
1 |
|
->where('u.id = :oldid') |
234
|
1 |
|
->setParameter("newid", $record["id"]) |
|
|
|
|
235
|
1 |
|
->setParameter("oldid", $objrecord->getId()) |
|
|
|
|
236
|
1 |
|
->getQuery(); |
237
|
1 |
|
$q->execute(); |
238
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $objrecord->getId() . " sistemata</info>"; |
|
|
|
|
239
|
1 |
|
$this->output->writeln($msgok); |
240
|
1 |
|
} catch (\Exception $exc) { |
241
|
|
|
echo $exc->getMessage(); |
242
|
|
|
return 1; |
243
|
|
|
} |
244
|
1 |
|
} |
245
|
1 |
|
return 0; |
246
|
|
|
} |
247
|
|
|
|
248
|
1 |
|
private function executeUpdate($entityclass, $record, $objrecord) |
249
|
|
|
{ |
250
|
1 |
|
foreach ($record as $key => $value) { |
251
|
1 |
|
if ($key !== 'id') { |
252
|
1 |
|
$propertyEntity = $this->dbutility->getEntityProperties($key, $objrecord); |
253
|
1 |
|
$getfieldname = $propertyEntity["get"]; |
|
|
|
|
254
|
1 |
|
$setfieldname = $propertyEntity["set"]; |
|
|
|
|
255
|
1 |
|
$cambiato = $this->dbutility->isRecordChanged($entityclass, $key, $objrecord->$getfieldname(), $value); |
|
|
|
|
256
|
1 |
|
if (!$cambiato) { |
257
|
1 |
|
if ($this->verboso) { |
258
|
1 |
|
$msginfo = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
259
|
1 |
|
. " per campo " . $key . " non modificato perchè già " |
|
|
|
|
260
|
1 |
|
. $value . "</info>"; |
|
|
|
|
261
|
1 |
|
$this->output->writeln($msginfo); |
262
|
1 |
|
} |
263
|
1 |
|
} else { |
264
|
|
|
try { |
265
|
1 |
|
$fieldtype = $this->dbutility->getFieldType($objrecord, $key); |
266
|
1 |
|
if ($fieldtype === 'datetime') { |
267
|
1 |
|
$date = new \DateTime(); |
268
|
1 |
|
$date->setTimestamp($value); |
269
|
1 |
|
$msgok = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
270
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
271
|
|
|
//. (!is_null($objrecord->$getfieldname())) ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "(null)" |
|
|
|
|
272
|
1 |
|
. ($objrecord->$getfieldname() ? $objrecord->$getfieldname()->format("Y-m-d H:i:s") : "") |
|
|
|
|
273
|
1 |
|
. " a " . $date->format("Y-m-d H:i:s") . " in formato DateTime</info>"; |
|
|
|
|
274
|
1 |
|
$this->output->writeln($msgok); |
275
|
1 |
|
$objrecord->$setfieldname($date); |
276
|
1 |
|
continue; |
277
|
|
|
} |
278
|
1 |
|
if (is_array($value)) { |
279
|
1 |
|
$msgarray = "<info>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
280
|
1 |
|
. " per campo " . $key . " cambio valore da " |
|
|
|
|
281
|
1 |
|
. json_encode($objrecord->$getfieldname()) . " a " |
|
|
|
|
282
|
1 |
|
. json_encode($value) . " in formato array" . "</info>"; |
|
|
|
|
283
|
1 |
|
$this->output->writeln($msgarray); |
284
|
1 |
|
$objrecord->$setfieldname($value); |
285
|
1 |
|
continue; |
286
|
|
|
} |
287
|
|
|
|
288
|
1 |
|
$joincolumn = $this->dbutility->getJoinTableField($entityclass, $key); |
|
|
|
|
289
|
1 |
|
$joincolumnproperty = $this->dbutility->getJoinTableFieldProperty($entityclass, $key); |
290
|
1 |
|
if ($joincolumn && $joincolumnproperty) { |
291
|
1 |
|
$joincolumnobj = $this->em->getRepository($joincolumn)->find($value); |
292
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $record["id"] |
|
|
|
|
293
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
294
|
1 |
|
. " a " . print_r($value, true) . " tramite entity find</info>"; |
|
|
|
|
295
|
1 |
|
$this->output->writeln($msgok); |
296
|
1 |
|
$joinobj = $this->dbutility->getEntityProperties($joincolumnproperty, new $entityclass()); |
|
|
|
|
297
|
1 |
|
$setfieldname = $joinobj["set"]; |
|
|
|
|
298
|
1 |
|
$objrecord->$setfieldname($joincolumnobj); |
299
|
1 |
|
continue; |
300
|
|
|
} |
301
|
|
|
|
302
|
1 |
|
$msgok = "<info>Modifica " . $entityclass . " con id " . $record["id"] |
|
|
|
|
303
|
1 |
|
. " per campo " . $key . " cambio valore da " . print_r($objrecord->$getfieldname(), true) |
|
|
|
|
304
|
1 |
|
. " a " . print_r($value, true) . "</info>"; |
|
|
|
|
305
|
1 |
|
$this->output->writeln($msgok); |
306
|
1 |
|
$objrecord->$setfieldname($value); |
307
|
1 |
|
} catch (\Exception $exc) { |
308
|
|
|
$msgerr = "<error>" . $entityclass . " con id " . $record["id"] |
|
|
|
|
309
|
|
|
. " per campo " . $key . ", ERRORE: " . $exc->getMessage() |
|
|
|
|
310
|
|
|
. " alla riga " . $exc->getLine() . "</error>"; |
|
|
|
|
311
|
|
|
$this->output->writeln($msgerr); |
312
|
|
|
//dump($exc); |
313
|
|
|
return 1; |
314
|
|
|
} |
315
|
|
|
} |
316
|
1 |
|
} |
317
|
1 |
|
} |
318
|
1 |
|
$this->em->persist($objrecord); |
319
|
1 |
|
$this->em->flush(); |
320
|
1 |
|
$this->em->clear(); |
321
|
|
|
|
322
|
1 |
|
return 0; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.