1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fi\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 getDestinationEntityYmlPath() |
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 |
|
$pathdoctrineyml = $destinationPath; |
37
|
|
|
|
38
|
|
|
//Si converte il nome file tabella.orm.yml se ha undercore |
39
|
1 |
|
$finder->in($pathdoctrineyml)->files()->name('*_*'); |
40
|
1 |
|
$table = new Table(); |
41
|
|
|
|
42
|
1 |
|
foreach ($finder as $file) { |
43
|
|
|
$oldfilename = $file->getPathName(); |
44
|
|
|
$newfilename = $pathdoctrineyml . 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($pathdoctrineyml)->files()->name('*_*'); |
51
|
1 |
|
$wrongfilename = array(); |
52
|
1 |
|
if (count($finderwrong) > 0) { |
53
|
|
|
foreach ($finderwrong as $file) { |
54
|
|
|
$wrongfilename[] = $file->getFileName(); |
55
|
|
|
$fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
1 |
|
$finderwrongcapitalize = new Finder(); |
59
|
1 |
|
$finderwrongcapitalize->in($pathdoctrineyml)->files()->name('*.yml'); |
60
|
1 |
|
foreach ($finderwrongcapitalize as $file) { |
61
|
|
|
if (!ctype_upper(substr($file->getFileName(), 0, 1))) { |
62
|
|
|
$wrongfilename[] = $file->getFileName(); |
63
|
|
|
$fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if (count($wrongfilename) > 0) { |
68
|
|
|
$errout = '<error>Ci sono tabelle nel file ' . $wbFile . ' con nomi non consentiti:' . |
69
|
|
|
implode(',', $wrongfilename) . |
70
|
|
|
'. I nomi tabella devono essere : con la prima lettera maiuscola,underscore ammesso,doppio underscore non ammesso</error>'; |
71
|
|
|
|
72
|
|
|
$output->writeln($errout); |
73
|
|
|
|
74
|
|
|
return -1; |
75
|
|
|
} else { |
76
|
1 |
|
return 0; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function checkprerequisiti($mwbfile, $output) |
81
|
|
|
{ |
82
|
1 |
|
$fs = new Filesystem(); |
83
|
|
|
|
84
|
1 |
|
$wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile; |
|
|
|
|
85
|
1 |
|
$bundlePath = $this->apppaths->getSrcPath(); |
86
|
|
|
|
87
|
|
|
$viewsPath = $bundlePath . |
|
|
|
|
88
|
1 |
|
DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR; |
89
|
|
|
$entityPath = $bundlePath . |
90
|
1 |
|
DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR; |
91
|
|
|
$formPath = $bundlePath . |
|
|
|
|
92
|
1 |
|
DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR; |
93
|
|
|
|
94
|
1 |
|
$scriptGenerator = $this->getScriptGenerator(); |
95
|
|
|
|
96
|
1 |
|
$destinationPath = $this->getDestinationEntityYmlPath(); |
97
|
1 |
|
$output->writeln('Creazione entities yml in ' . $destinationPath . ' da file ' . $mwbfile); |
98
|
|
|
|
99
|
1 |
|
if (!$fs->exists($bundlePath)) { |
100
|
|
|
$output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>'); |
101
|
|
|
|
102
|
|
|
return -1; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/* Creazione cartelle se non esistono nel bundle per l'esportazione */ |
106
|
1 |
|
$fs->mkdir($destinationPath); |
107
|
1 |
|
$fs->mkdir($entityPath); |
108
|
1 |
|
$fs->mkdir($formPath); |
109
|
1 |
|
$fs->mkdir($viewsPath); |
110
|
|
|
|
111
|
1 |
|
if (!$fs->exists($wbFile)) { |
112
|
|
|
$output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!'); |
113
|
|
|
|
114
|
|
|
return -1; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
if (!$fs->exists($scriptGenerator)) { |
118
|
|
|
$output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>'); |
119
|
|
|
|
120
|
|
|
return -1; |
121
|
|
|
} |
122
|
1 |
|
if (!$fs->exists($destinationPath)) { |
123
|
|
|
$output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>'); |
124
|
|
|
|
125
|
|
|
return -1; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return 0; |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
public function getScriptGenerator() |
132
|
|
|
{ |
133
|
1 |
|
$scriptGenerator = ""; |
|
|
|
|
134
|
1 |
|
$scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export'; |
135
|
1 |
|
if (!file_exists($scriptGenerator)) { |
136
|
|
|
$scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . |
137
|
|
|
'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export'; |
138
|
|
|
} |
139
|
1 |
|
if (!$scriptGenerator) { |
140
|
|
|
throw new \Exception("mysql-workbench-schema-export non trovato", -100); |
|
|
|
|
141
|
|
|
} |
142
|
1 |
|
return $scriptGenerator; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
public function getExportJsonFile() |
146
|
|
|
{ |
147
|
1 |
|
$fs = new Filesystem(); |
|
|
|
|
148
|
1 |
|
$cachedir = $this->apppaths->getCachePath(); |
|
|
|
|
149
|
1 |
|
$exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json'; |
150
|
1 |
|
if ($fs->exists($exportJson)) { |
151
|
1 |
|
$fs->remove($exportJson); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
return $exportJson; |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
public function removeExportJsonFile() |
158
|
|
|
{ |
159
|
1 |
|
$this->getExportJsonFile(); |
160
|
|
|
|
161
|
1 |
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
1 |
|
public static function getJsonMwbGenerator() |
165
|
|
|
{ |
166
|
|
|
$jsonTemplate = <<<EOF |
167
|
1 |
|
{"export": "doctrine2-annotation", |
168
|
|
|
"zip": false, |
169
|
|
|
"dir": "[dir]", |
170
|
|
|
"params": |
171
|
|
|
{"indentation": 4, |
172
|
|
|
"useTabs": false, |
173
|
|
|
"filename": "%entity%.%extension%", |
174
|
|
|
"skipPluralNameChecking": true, |
175
|
|
|
"backupExistingFile": false, |
176
|
|
|
"addGeneratorInfoAsComment": false, |
177
|
|
|
"useLoggedStorage": false, |
178
|
|
|
"enhanceManyToManyDetection": true, |
179
|
|
|
"logToConsole": false, |
180
|
|
|
"logFile": "", |
181
|
|
|
"bundleNamespace": "App", |
182
|
|
|
"entityNamespace": "Entity", |
183
|
|
|
"repositoryNamespace": "App\\\\Entity", |
184
|
|
|
"useAutomaticRepository": false, |
185
|
|
|
"generateExtendableEntity": true, |
186
|
|
|
"extendableEntityHasDiscriminator": false, |
187
|
|
|
"extendTableNameWithSchemaName": false |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
EOF; |
191
|
1 |
|
return $jsonTemplate; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
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.