1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cdf\PannelloAmministrazioneBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
6
|
|
|
use Symfony\Component\Finder\Finder; |
7
|
|
|
use MwbExporter\Model\Table; |
8
|
|
|
|
9
|
|
|
class GeneratorHelper |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
private $container; |
13
|
|
|
private $apppaths; |
14
|
|
|
|
15
|
1 |
|
public function __construct($container) |
16
|
|
|
{ |
17
|
1 |
|
$this->container = $container; |
18
|
1 |
|
$this->apppaths = $container->get("pannelloamministrazione.projectpath"); |
|
|
|
|
19
|
1 |
|
} |
20
|
|
|
|
21
|
1 |
|
public function getDestinationEntityOrmPath() |
22
|
|
|
{ |
23
|
1 |
|
$entitypath = realpath($this->apppaths->getSrcPath() . '/../src/Entity/'); |
24
|
1 |
|
if (DIRECTORY_SEPARATOR == '/') { |
25
|
1 |
|
return $entitypath; |
26
|
|
|
} else { |
27
|
|
|
return str_replace('/', "\/", $entitypath); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
1 |
|
public function checktables($destinationPath, $wbFile, $output) |
32
|
|
|
{ |
33
|
1 |
|
$finder = new Finder(); |
34
|
1 |
|
$fs = new Filesystem(); |
|
|
|
|
35
|
|
|
|
36
|
1 |
|
$pathdoctrineorm = $destinationPath; |
37
|
|
|
|
38
|
|
|
//Si converte il nome file per l'Orm della tabella se ha undercore |
39
|
1 |
|
$finder->in($pathdoctrineorm)->files()->name('*_*'); |
40
|
1 |
|
$table = new Table(); |
41
|
|
|
|
42
|
1 |
|
foreach ($finder as $file) { |
43
|
|
|
$oldfilename = $file->getPathName(); |
44
|
|
|
$newfilename = $pathdoctrineorm . DIRECTORY_SEPARATOR . $table->beautify($file->getFileName()); |
45
|
|
|
$fs->rename($oldfilename, $newfilename, true); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
//Si cercano file con nomi errati |
49
|
1 |
|
$finderwrong = new Finder(); |
50
|
1 |
|
$finderwrong->in($pathdoctrineorm)->files()->name('*_*'); |
51
|
1 |
|
$wrongfilename = array(); |
52
|
1 |
|
if (count($finderwrong) > 0) { |
53
|
|
|
foreach ($finderwrong as $file) { |
54
|
|
|
$wrongfilename[] = $file->getFileName(); |
55
|
|
|
$fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
//Si cercano file con nomi campo errati |
60
|
1 |
|
$finderwrongproperty = new Finder(); |
61
|
1 |
|
$finderwrongproperty->in($pathdoctrineorm)->files()->name('*'); |
62
|
1 |
|
$wrongpropertyname = array(); |
63
|
1 |
|
foreach ($finderwrongproperty as $file) { |
64
|
1 |
|
$ref = new \ReflectionClass("App\\Entity\\" . basename($file->getFileName(), ".php")); |
|
|
|
|
65
|
1 |
|
$props = $ref->getProperties(); |
66
|
1 |
|
foreach ($props as $prop) { |
67
|
1 |
|
$f = $prop->getName(); |
68
|
1 |
|
if ($f !== strtolower($f)) { |
69
|
1 |
|
$wrongpropertyname[] = $file->getFileName(); |
70
|
1 |
|
$fs->remove($pathdoctrineorm . DIRECTORY_SEPARATOR . $file->getFileName()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
if (count($wrongpropertyname) > 0) { |
76
|
1 |
|
$errout = '<error>Ci sono campi nel file ' . $wbFile . ' con nomi non consentiti:' . |
77
|
1 |
|
implode(',', $wrongpropertyname) . |
78
|
1 |
|
'. I nomi dei campi devono essere lower case</error>'; |
79
|
|
|
|
80
|
1 |
|
$output->writeln($errout); |
81
|
|
|
|
82
|
1 |
|
return -1; |
83
|
|
|
} else { |
84
|
1 |
|
return 0; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function checkprerequisiti($mwbfile, $output) |
89
|
|
|
{ |
90
|
1 |
|
$fs = new Filesystem(); |
91
|
|
|
|
92
|
1 |
|
$wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile; |
|
|
|
|
93
|
1 |
|
$bundlePath = $this->apppaths->getSrcPath(); |
94
|
|
|
|
95
|
|
|
$viewsPath = $bundlePath . |
|
|
|
|
96
|
1 |
|
DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR; |
97
|
|
|
$entityPath = $bundlePath . |
98
|
1 |
|
DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR; |
99
|
|
|
$formPath = $bundlePath . |
|
|
|
|
100
|
1 |
|
DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR; |
101
|
|
|
|
102
|
1 |
|
$scriptGenerator = $this->getScriptGenerator(); |
103
|
|
|
|
104
|
1 |
|
$destinationPath = $this->getDestinationEntityOrmPath(); |
105
|
1 |
|
$output->writeln('Creazione orm entities in ' . $destinationPath . ' da file ' . $mwbfile); |
106
|
|
|
|
107
|
1 |
|
if (!$fs->exists($bundlePath)) { |
108
|
|
|
$output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>'); |
109
|
|
|
|
110
|
|
|
return -1; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/* Creazione cartelle se non esistono nel bundle per l'esportazione */ |
114
|
1 |
|
$fs->mkdir($destinationPath); |
115
|
1 |
|
$fs->mkdir($entityPath); |
116
|
1 |
|
$fs->mkdir($formPath); |
117
|
1 |
|
$fs->mkdir($viewsPath); |
118
|
|
|
|
119
|
1 |
|
if (!$fs->exists($wbFile)) { |
120
|
|
|
$output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!'); |
121
|
|
|
|
122
|
|
|
return -1; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
if (!$fs->exists($scriptGenerator)) { |
126
|
|
|
$output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>'); |
127
|
|
|
|
128
|
|
|
return -1; |
129
|
|
|
} |
130
|
1 |
|
if (!$fs->exists($destinationPath)) { |
131
|
|
|
$output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>'); |
132
|
|
|
|
133
|
|
|
return -1; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
return 0; |
137
|
|
|
} |
138
|
|
|
|
139
|
1 |
|
public function getScriptGenerator() |
140
|
|
|
{ |
141
|
1 |
|
$scriptGenerator = ""; |
|
|
|
|
142
|
1 |
|
$scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export'; |
143
|
1 |
|
if (!file_exists($scriptGenerator)) { |
144
|
|
|
$scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . |
145
|
|
|
'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export'; |
146
|
|
|
} |
147
|
1 |
|
if (!$scriptGenerator) { |
148
|
|
|
throw new \Exception("mysql-workbench-schema-export non trovato", -100); |
|
|
|
|
149
|
|
|
} |
150
|
1 |
|
return $scriptGenerator; |
151
|
|
|
} |
152
|
|
|
|
153
|
1 |
|
public function getExportJsonFile() |
154
|
|
|
{ |
155
|
1 |
|
$fs = new Filesystem(); |
|
|
|
|
156
|
1 |
|
$cachedir = $this->apppaths->getCachePath(); |
|
|
|
|
157
|
1 |
|
$exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json'; |
158
|
1 |
|
if ($fs->exists($exportJson)) { |
159
|
1 |
|
$fs->remove($exportJson); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
return $exportJson; |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
public function removeExportJsonFile() |
166
|
|
|
{ |
167
|
1 |
|
$this->getExportJsonFile(); |
168
|
|
|
|
169
|
1 |
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
public static function getJsonMwbGenerator() |
173
|
|
|
{ |
174
|
|
|
$jsonTemplate = <<<EOF |
175
|
1 |
|
{"export": "doctrine2-annotation", |
176
|
|
|
"zip": false, |
177
|
|
|
"dir": "[dir]", |
178
|
|
|
"params": |
179
|
|
|
{"indentation": 4, |
180
|
|
|
"useTabs": false, |
181
|
|
|
"filename": "%entity%.%extension%", |
182
|
|
|
"skipPluralNameChecking": true, |
183
|
|
|
"backupExistingFile": false, |
184
|
|
|
"addGeneratorInfoAsComment": false, |
185
|
|
|
"useLoggedStorage": false, |
186
|
|
|
"enhanceManyToManyDetection": true, |
187
|
|
|
"logToConsole": false, |
188
|
|
|
"logFile": "", |
189
|
|
|
"bundleNamespace": "App", |
190
|
|
|
"entityNamespace": "Entity", |
191
|
|
|
"repositoryNamespace": "App\\\\Entity", |
192
|
|
|
"useAutomaticRepository": false, |
193
|
|
|
"generateExtendableEntity": true, |
194
|
|
|
"extendableEntityHasDiscriminator": false, |
195
|
|
|
"extendTableNameWithSchemaName": false |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
EOF; |
199
|
1 |
|
return $jsonTemplate; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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.