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\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
12
|
|
|
use Doctrine\Common\Persistence\Proxy; |
13
|
|
|
|
14
|
|
|
class Fifree2configuratorexportCommand extends ContainerAwareCommand |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
private $entities = array(); |
18
|
|
|
|
19
|
4 |
|
protected function configure() |
20
|
|
|
{ |
21
|
4 |
|
$this |
22
|
4 |
|
->setName('fifree2:configuratorexport') |
23
|
4 |
|
->setDescription('Configuratore per Fifree') |
24
|
4 |
|
->setHelp('Esporta la configurazione di fifree') |
25
|
4 |
|
->addArgument('entity', InputArgument::REQUIRED, 'Entity da esportare') |
26
|
4 |
|
->addOption('append', null, InputOption::VALUE_NONE, 'Append per export'); |
27
|
4 |
|
} |
28
|
|
|
|
29
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
30
|
|
|
{ |
31
|
1 |
|
$fs = new Filesystem; |
|
|
|
|
32
|
1 |
|
$append = $input->getOption('append'); |
|
|
|
|
33
|
1 |
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
|
|
|
34
|
1 |
|
$this->output = $output; |
|
|
|
|
35
|
1 |
|
$entity = $input->getArgument('entity'); |
|
|
|
|
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
//$fixturefile = $this->getContainer()->get('kernel')->locateResource('@FiCoreBundle/Resources/config/fixtures.yml'); |
|
|
|
|
39
|
1 |
|
$fixturefile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
|
|
|
40
|
1 |
|
if (!$append) { |
41
|
1 |
|
$fs->remove($fixturefile); |
42
|
1 |
|
} |
43
|
1 |
|
return $this->export($fixturefile, $entity); |
44
|
|
|
} catch (\Exception $exc) { |
45
|
|
|
echo $exc->getMessage() . " at line " . $exc->getLine(); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
protected function export($fixturefile, $entity) |
50
|
|
|
{ |
51
|
1 |
|
$entityclass = "Fi\\CoreBundle\\Entity\\" . $entity; |
|
|
|
|
52
|
1 |
|
$ret = $this->exportEntity($fixturefile, $entityclass); |
|
|
|
|
53
|
1 |
|
if ($ret == 0) { |
54
|
1 |
|
foreach ($this->entities as $entity) { |
|
|
|
|
55
|
1 |
|
$this->output->writeln("<info>Esporto " . $entity . " su file</info>"); |
|
|
|
|
56
|
1 |
|
$this->exportEntityToFile($fixturefile, $entity); |
57
|
1 |
|
} |
58
|
1 |
|
$this->exportEntityToFile($fixturefile, $entityclass); |
59
|
1 |
|
return 0; |
60
|
|
|
} |
61
|
|
|
return 1; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
private function exportEntity($fixturefile, $entityclass) |
65
|
|
|
{ |
66
|
1 |
|
$dbutility = $this->getContainer()->get("ficorebundle.database.utility"); |
|
|
|
|
67
|
1 |
|
$this->output->writeln("<info>Export Entity: " . $entityclass . "</info>"); |
|
|
|
|
68
|
1 |
|
if ($dbutility->entityExists($entityclass)) { |
69
|
1 |
|
$hasEntityCollegate = $dbutility->entityHasJoinTables($entityclass); |
70
|
1 |
|
if ($hasEntityCollegate) { |
71
|
1 |
|
$this->output->writeln("<info>Entity " . $entityclass . " ha tabelle in join</info>"); |
|
|
|
|
72
|
1 |
|
$entityCollegata = $dbutility->getEntityJoinTables($entityclass); |
73
|
1 |
|
foreach ($entityCollegata as $key => $tabella) { |
74
|
1 |
|
$this->entities[] = $key; |
75
|
1 |
|
$this->output->writeln("<info>Prima esporto " . $key . " -> " . $tabella["entity"]["fieldName"] . "</info>"); |
|
|
|
|
76
|
1 |
|
$this->exportEntity($fixturefile, $key); |
77
|
1 |
|
} |
78
|
1 |
|
} |
79
|
1 |
|
} else { |
80
|
|
|
$this->output->writeln("<error>Entity not found: " . $entityclass . " </error>"); |
|
|
|
|
81
|
|
|
return 1; |
82
|
|
|
} |
83
|
1 |
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
private function exportEntityToFile($fixturefile, $entityclass) |
87
|
|
|
{ |
88
|
1 |
|
$entityDump = array(); |
89
|
|
|
|
90
|
|
|
|
91
|
1 |
|
$query = $this->em->createQueryBuilder() |
92
|
1 |
|
->select('p') |
93
|
1 |
|
->from($entityclass, 'p') |
94
|
1 |
|
->getQuery() |
95
|
1 |
|
; |
96
|
1 |
|
$repo = $query->getArrayResult(); |
|
|
|
|
97
|
|
|
|
98
|
|
|
|
99
|
|
|
//$repo = $this->em->getRepository($entityclass)->findAll(); |
|
|
|
|
100
|
1 |
|
$this->output->writeln("<info>Trovate " . count($repo) . " records per l'entity " . $entityclass . "</info>"); |
|
|
|
|
101
|
1 |
|
foreach ($repo as $row) { |
102
|
1 |
|
$entityDump[$entityclass][] = $row; |
103
|
1 |
|
} |
104
|
1 |
|
$yml = Yaml::dump($entityDump); |
105
|
1 |
|
file_put_contents($fixturefile, $yml, FILE_APPEND); |
106
|
1 |
|
} |
107
|
|
|
} |
108
|
|
|
|
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.