|
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
|
3 |
|
protected function configure() |
|
20
|
|
|
{ |
|
21
|
3 |
|
$this |
|
22
|
3 |
|
->setName('fifree2:configuratorexport') |
|
23
|
3 |
|
->setDescription('Configuratore per Fifree') |
|
24
|
3 |
|
->setHelp('Esporta la configurazione di fifree') |
|
25
|
3 |
|
->addArgument('entity', InputArgument::REQUIRED, 'Entity da esportare') |
|
26
|
3 |
|
->addOption('append', null, InputOption::VALUE_NONE, 'Append per export'); |
|
27
|
3 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
30
|
|
|
{ |
|
31
|
|
|
$fs = new Filesystem; |
|
|
|
|
|
|
32
|
|
|
$append = $input->getOption('append'); |
|
|
|
|
|
|
33
|
|
|
$this->em = $this->getContainer()->get("doctrine")->getManager(); |
|
|
|
|
|
|
34
|
|
|
$this->output = $output; |
|
|
|
|
|
|
35
|
|
|
$entity = $input->getArgument('entity'); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
try { |
|
38
|
|
|
//$fixturefile = $this->getContainer()->get('kernel')->locateResource('@FiCoreBundle/Resources/config/fixtures.yml'); |
|
|
|
|
|
|
39
|
|
|
$fixturefile = dirname($this->getContainer()->get('kernel')->getRootDir()) . "/doc" . DIRECTORY_SEPARATOR . "fixtures.yml"; |
|
|
|
|
|
|
40
|
|
|
if (!$append) { |
|
41
|
|
|
$fs->remove($fixturefile); |
|
42
|
|
|
} |
|
43
|
|
|
return $this->export($fixturefile, $entity); |
|
44
|
|
|
} catch (\Exception $exc) { |
|
45
|
|
|
echo $exc->getMessage() . " at line " . $exc->getLine(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function export($fixturefile, $entity) |
|
50
|
|
|
{ |
|
51
|
|
|
$entityclass = "Fi\\CoreBundle\\Entity\\" . $entity; |
|
|
|
|
|
|
52
|
|
|
$ret = $this->exportEntity($fixturefile, $entityclass); |
|
|
|
|
|
|
53
|
|
|
if ($ret == 0) { |
|
54
|
|
|
foreach ($this->entities as $entity) { |
|
|
|
|
|
|
55
|
|
|
$this->output->writeln("<info>Esporto " . $entity . " su file</info>"); |
|
|
|
|
|
|
56
|
|
|
$this->exportEntityToFile($fixturefile, $entity); |
|
57
|
|
|
} |
|
58
|
|
|
$this->exportEntityToFile($fixturefile, $entityclass); |
|
59
|
|
|
return 0; |
|
60
|
|
|
} |
|
61
|
|
|
return 1; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function exportEntity($fixturefile, $entityclass) |
|
65
|
|
|
{ |
|
66
|
|
|
|
|
67
|
|
|
$this->output->writeln("<info>Export Entity: " . $entityclass . "</info>"); |
|
|
|
|
|
|
68
|
|
|
if ($this->entityExists($entityclass)) { |
|
69
|
|
|
$hasEntityCollegate = $this->hasJoinTables($entityclass); |
|
70
|
|
|
if ($hasEntityCollegate) { |
|
71
|
|
|
$this->output->writeln("<info>Entity " . $entityclass . " ha tabelle in join</info>"); |
|
|
|
|
|
|
72
|
|
|
$entityCollegata = $this->entityJoinTables($entityclass); |
|
73
|
|
|
foreach ($entityCollegata as $key => $tabella) { |
|
74
|
|
|
$this->entities[] = $key; |
|
75
|
|
|
$this->output->writeln("<info>Prima esporto " . $key . " -> " . $tabella["entity"]["fieldName"] . "</info>"); |
|
|
|
|
|
|
76
|
|
|
$this->exportEntity($fixturefile, $key); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} else { |
|
80
|
|
|
$this->output->writeln("<error>Entity not found: " . $entityclass . " </error>"); |
|
|
|
|
|
|
81
|
|
|
return 1; |
|
82
|
|
|
} |
|
83
|
|
|
return 0; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
private function exportEntityToFile($fixturefile, $entityclass) |
|
87
|
|
|
{ |
|
88
|
|
|
$entityDump = array(); |
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
$query = $this->em->createQueryBuilder() |
|
92
|
|
|
->select('p') |
|
93
|
|
|
->from($entityclass, 'p') |
|
94
|
|
|
->getQuery() |
|
95
|
|
|
; |
|
96
|
|
|
$repo = $query->getArrayResult(); |
|
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
//$repo = $this->em->getRepository($entityclass)->findAll(); |
|
|
|
|
|
|
100
|
|
|
$this->output->writeln("<info>Trovate " . count($repo) . " records per l'entity " . $entityclass . "</info>"); |
|
|
|
|
|
|
101
|
|
|
foreach ($repo as $row) { |
|
102
|
|
|
$entityDump[$entityclass][] = $row; |
|
103
|
|
|
} |
|
104
|
|
|
$yml = Yaml::dump($entityDump); |
|
105
|
|
|
file_put_contents($fixturefile, $yml, FILE_APPEND); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
private function entityExists($className) |
|
109
|
|
|
{ |
|
110
|
|
|
|
|
111
|
|
|
if (is_object($className)) { |
|
112
|
|
|
$className = ($className instanceof Proxy) ? get_parent_class($className) : get_class($className); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return !$this->em->getMetadataFactory()->isTransient($className); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
private function entityJoinTables($entityclass) |
|
119
|
|
|
{ |
|
120
|
|
|
$jointables = array(); |
|
121
|
|
|
$metadata = $this->em->getClassMetadata($entityclass); |
|
|
|
|
|
|
122
|
|
|
$fielsassoc = $metadata->associationMappings; |
|
123
|
|
|
foreach ($fielsassoc as $tableassoc) { |
|
124
|
|
|
if ($tableassoc["inversedBy"]) { |
|
|
|
|
|
|
125
|
|
|
$jointables[$tableassoc["targetEntity"]] = array("entity" => $tableassoc); |
|
|
|
|
|
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return $jointables; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
private function hasJoinTables($entityclass) |
|
132
|
|
|
{ |
|
133
|
|
|
$jointables = $this->entityJoinTables($entityclass); |
|
134
|
|
|
return count($jointables) > 0 ? true : false; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
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.