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