1
|
|
|
<?php |
2
|
|
|
namespace Cdf\PannelloAmministrazioneBundle\Controller; |
3
|
|
|
|
4
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
use Symfony\Component\Finder\Finder; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
use Fi\OsBundle\DependencyInjection\OsFunctions; |
11
|
|
|
use Cdf\PannelloAmministrazioneBundle\DependencyInjection\PannelloAmministrazioneUtils; |
12
|
|
|
use Symfony\Component\Lock\Factory; |
13
|
|
|
use Symfony\Component\Lock\Store\FlockStore; |
14
|
|
|
|
15
|
|
|
class PannelloAmministrazioneController extends Controller |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
protected $apppaths; |
19
|
|
|
protected $locksystem; |
20
|
|
|
protected $factory; |
21
|
|
|
|
22
|
1 |
|
public function __construct() |
23
|
|
|
{ |
24
|
1 |
|
$store = new FlockStore(sys_get_temp_dir()); |
|
|
|
|
25
|
1 |
|
$factory = new Factory($store); |
|
|
|
|
26
|
1 |
|
$this->locksystem = $factory->createLock('pannelloamministrazione-command'); |
27
|
1 |
|
$this->locksystem->release(); |
28
|
1 |
|
} |
29
|
|
|
|
30
|
1 |
|
private function findEntities() |
31
|
|
|
{ |
32
|
1 |
|
$entitiesprogetto = array(); |
33
|
1 |
|
$prefix = 'App\\Entity\\'; |
|
|
|
|
34
|
1 |
|
$prefixBase = 'Base'; |
|
|
|
|
35
|
1 |
|
$entities = $this->get("doctrine")->getManager()->getConfiguration()->getMetadataDriverImpl()->getAllClassNames(); |
|
|
|
|
36
|
1 |
|
foreach ($entities as $entity) { |
37
|
1 |
|
if (substr($entity, 0, strlen($prefix)) == $prefix) { |
38
|
1 |
|
if (substr(substr($entity, strlen($prefix)), 0, strlen($prefixBase)) != $prefixBase) { |
39
|
1 |
|
$entitiesprogetto[] = substr($entity, strlen($prefix)); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
} |
43
|
1 |
|
return $entitiesprogetto; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function indexAction() |
47
|
|
|
{ |
48
|
1 |
|
$finder = new Finder(); |
|
|
|
|
49
|
1 |
|
$fs = new Filesystem(); |
|
|
|
|
50
|
1 |
|
$this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
|
|
|
|
51
|
|
|
|
52
|
1 |
|
$projectDir = $this->apppaths->getRootPath(); |
53
|
1 |
|
$docDir = $this->apppaths->getDocPath(); |
|
|
|
|
54
|
|
|
|
55
|
1 |
|
$mwbs = array(); |
56
|
|
|
|
57
|
1 |
|
if ($fs->exists($docDir)) { |
58
|
1 |
|
$finder->in($docDir)->files()->name('*.mwb'); |
59
|
1 |
|
foreach ($finder as $file) { |
60
|
1 |
|
$mwbs[] = $file->getBasename(); |
61
|
|
|
} |
62
|
|
|
} |
63
|
1 |
|
sort($mwbs); |
64
|
1 |
|
$svn = $fs->exists($projectDir . '/.svn'); |
65
|
1 |
|
$git = $fs->exists($projectDir . '/.git'); |
66
|
|
|
|
67
|
1 |
|
if (!OsFunctions::isWindows()) { |
68
|
1 |
|
$delcmd = 'rm -rf'; |
|
|
|
|
69
|
1 |
|
$setfilelock = "touch " . $this->getParameter("cdf.bicorebundle.parameter.maintenance.lockfile"); |
|
|
|
|
70
|
1 |
|
$remfilelock = "rm " . $this->getParameter("cdf.bicorebundle.parameter.maintenance.lockfile"); |
|
|
|
|
71
|
1 |
|
$windows = false; |
|
|
|
|
72
|
|
|
} else { |
73
|
|
|
$delcmd = 'del'; |
|
|
|
|
74
|
|
|
$setfilelock = 'echo $null >> ' . $this->getParameter("cdf.bicorebundle.parameter.maintenance.lockfile"); |
|
|
|
|
75
|
|
|
$remfilelock = "del " . $this->getParameter("cdf.bicorebundle.parameter.maintenance.lockfile"); |
|
|
|
|
76
|
|
|
$windows = true; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
$dellogsfiles = $delcmd . ' ' . $this->apppaths->getLogsPath() . DIRECTORY_SEPARATOR . '*'; |
|
|
|
|
80
|
1 |
|
$delcacheprodfiles = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . 'prod' . DIRECTORY_SEPARATOR . '*'; |
|
|
|
|
81
|
1 |
|
$delcachedevfiles = $delcmd . ' ' . $this->apppaths->getCachePath() . DIRECTORY_SEPARATOR . 'dev' . DIRECTORY_SEPARATOR . '*'; |
|
|
|
|
82
|
1 |
|
$setmaintenancefile = $setfilelock; |
83
|
1 |
|
$remmaintenancefile = $remfilelock; |
84
|
|
|
|
85
|
1 |
|
$projectparentdir = realpath($projectDir . '/../'); |
86
|
1 |
|
$envvars = $projectparentdir . "/" . "envvars"; |
|
|
|
|
87
|
1 |
|
$composercachedir = $projectparentdir . "/" . ".composer"; |
|
|
|
|
88
|
1 |
|
$composerinstall = ""; |
|
|
|
|
89
|
1 |
|
if ($windows == false) { |
|
|
|
|
90
|
1 |
|
if (file_exists($envvars)) { |
91
|
|
|
$composerinstall = $composerinstall . ". " . $envvars . " && "; |
|
|
|
|
92
|
|
|
} |
93
|
1 |
|
if (file_exists($composercachedir)) { |
94
|
|
|
$composerinstall = $composerinstall . " export COMPOSER_HOME=" . $composercachedir . " && "; |
|
|
|
|
95
|
|
|
} |
96
|
1 |
|
$composerinstall = $composerinstall . " cd " . $projectDir . " && composer install --no-interaction 2>&1"; |
|
|
|
|
97
|
1 |
|
$sed = "sed -i -e 's/cercaquestastringa/sostituisciconquestastringa/g' " . $projectDir . "/.env"; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$comandishell = array( |
101
|
1 |
|
$this->fixSlash($dellogsfiles), |
102
|
1 |
|
$this->fixSlash($delcacheprodfiles), |
103
|
1 |
|
$this->fixSlash($delcachedevfiles), |
104
|
1 |
|
$this->fixSlash($setmaintenancefile), |
105
|
1 |
|
$this->fixSlash($remmaintenancefile), |
106
|
1 |
|
$composerinstall, |
107
|
1 |
|
$sed); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$comandisymfony = array( |
110
|
1 |
|
"list", |
|
|
|
|
111
|
1 |
|
"cache:clear --env=prod --no-debug", |
|
|
|
|
112
|
1 |
|
"fos:user:create admin pass [email protected]", |
|
|
|
|
113
|
1 |
|
"fos:user:promote username ROLE_SUPER_ADMIN", |
|
|
|
|
114
|
1 |
|
"assets:install ' . $projectDir . ' /public", |
|
|
|
|
115
|
1 |
|
"pannelloamministrazione:checkgitversion" |
|
|
|
|
116
|
|
|
); |
117
|
|
|
|
118
|
1 |
|
$entities = $this->findEntities(); |
119
|
1 |
|
sort($entities); |
120
|
|
|
|
121
|
|
|
$twigparms = array( |
122
|
1 |
|
'svn' => $svn, 'git' => $git, 'mwbs' => $mwbs, 'entities' => $entities, |
123
|
1 |
|
'rootdir' => $this->fixSlash($projectDir), |
124
|
1 |
|
'comandishell' => $comandishell, |
125
|
1 |
|
'comandisymfony' => $comandisymfony, |
126
|
1 |
|
'iswindows' => $windows, |
127
|
|
|
); |
128
|
|
|
|
129
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:index.html.twig', $twigparms); |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
private function fixSlash($path) |
133
|
|
|
{ |
134
|
1 |
|
return str_replace('\\', '\\\\', $path); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getLockMessage() |
138
|
|
|
{ |
139
|
|
|
return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>"; |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
public function aggiornaSchemaDatabaseAction() |
143
|
|
|
{ |
144
|
1 |
|
if (!$this->locksystem->acquire()) { |
145
|
|
|
return new Response($this->getLockMessage()); |
146
|
|
|
} else { |
147
|
1 |
|
$this->locksystem->acquire(); |
148
|
1 |
|
$command = $this->get("pannelloamministrazione.commands"); |
|
|
|
|
149
|
1 |
|
$result = $command->aggiornaSchemaDatabase(); |
|
|
|
|
150
|
|
|
|
151
|
1 |
|
$this->locksystem->release(); |
152
|
1 |
|
if ($result['errcode'] != 0) { |
153
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
154
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
155
|
|
|
return new Response($view, 500); |
156
|
|
|
} else { |
157
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
158
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
/* FORMS */ |
163
|
|
|
|
164
|
1 |
|
public function generateFormCrudAction(Request $request) |
165
|
|
|
{ |
166
|
1 |
|
if (!$this->locksystem->acquire()) { |
167
|
|
|
return new Response($this->getLockMessage()); |
168
|
|
|
} else { |
169
|
1 |
|
$entityform = $request->get('entityform'); |
170
|
|
|
|
171
|
1 |
|
$this->locksystem->acquire(); |
172
|
|
|
|
173
|
1 |
|
$command = $this->get("pannelloamministrazione.commands"); |
|
|
|
|
174
|
1 |
|
$result = $command->generateFormCrud($entityform); |
|
|
|
|
175
|
|
|
|
176
|
1 |
|
$this->locksystem->release(); |
177
|
|
|
//$retcc = ''; |
|
|
|
|
178
|
1 |
|
if ($result['errcode'] < 0) { |
179
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => "Generazione Form Crud", 'message' => $result['message']); |
|
|
|
|
180
|
|
|
return new Response( |
181
|
|
|
$this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms), |
182
|
|
|
500 |
183
|
|
|
); |
184
|
|
|
} else { |
|
|
|
|
185
|
|
|
//$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment()); |
|
|
|
|
186
|
|
|
} |
187
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
188
|
|
|
|
189
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
/* ENTITIES */ |
193
|
|
|
|
194
|
1 |
|
public function generateEntityAction(Request $request) |
195
|
|
|
{ |
196
|
1 |
|
if (!$this->locksystem->acquire()) { |
197
|
|
|
return new Response($this->getLockMessage()); |
198
|
|
|
} else { |
199
|
1 |
|
$this->locksystem->acquire(); |
200
|
1 |
|
$wbFile = $request->get('file'); |
|
|
|
|
201
|
1 |
|
$command = $this->get("pannelloamministrazione.commands"); |
|
|
|
|
202
|
1 |
|
$ret = $command->generateEntity($wbFile); |
|
|
|
|
203
|
1 |
|
$this->locksystem->release(); |
204
|
1 |
|
return new Response($ret['message']); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
/* VCS (GIT,SVN) */ |
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @codeCoverageIgnore |
211
|
|
|
*/ |
212
|
|
|
public function getVcsAction() |
213
|
|
|
{ |
214
|
|
|
set_time_limit(0); |
215
|
|
|
$this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
|
|
|
|
216
|
|
|
if (!$this->locksystem->acquire()) { |
217
|
|
|
return new Response($this->getLockMessage()); |
218
|
|
|
} else { |
219
|
|
|
$this->locksystem->acquire(); |
220
|
|
|
$command = $this->get("pannelloamministrazione.commands"); |
|
|
|
|
221
|
|
|
$result = $command->getVcs(); |
|
|
|
|
222
|
|
|
$this->locksystem->release(); |
223
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
224
|
|
|
|
225
|
|
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
/* CLEAR CACHE */ |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Suppress PMD warnings per exit. |
232
|
|
|
* |
233
|
|
|
* @//SuppressWarnings(PHPMD) |
234
|
|
|
*/ |
235
|
1 |
|
public function clearCacheAction(Request $request) |
|
|
|
|
236
|
|
|
{ |
237
|
1 |
|
set_time_limit(0); |
238
|
1 |
|
if (!$this->locksystem->acquire()) { |
239
|
|
|
return new Response($this->getLockMessage()); |
240
|
|
|
} else { |
241
|
1 |
|
$this->locksystem->acquire(); |
242
|
1 |
|
$command = $this->get("pannelloamministrazione.commands"); |
|
|
|
|
243
|
1 |
|
$result = $command->clearcache(); |
|
|
|
|
244
|
|
|
|
245
|
1 |
|
$this->locksystem->release(); |
246
|
|
|
|
247
|
1 |
|
if ($result['errcode'] != 0) { |
248
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
249
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
250
|
|
|
return new Response($view, 500); |
251
|
|
|
} else { |
252
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
253
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
/* CLEAR CACHE */ |
258
|
|
|
|
259
|
1 |
|
public function symfonyCommandAction(Request $request) |
260
|
|
|
{ |
261
|
1 |
|
set_time_limit(0); |
262
|
1 |
|
$comando = $request->get('symfonycommand'); |
263
|
1 |
|
if (!$this->locksystem->acquire()) { |
264
|
|
|
return new Response($this->getLockMessage()); |
265
|
|
|
} else { |
266
|
1 |
|
$this->locksystem->acquire(); |
267
|
1 |
|
$this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
|
|
|
|
268
|
1 |
|
$pammutils = new PannelloAmministrazioneUtils($this->container); |
|
|
|
|
269
|
1 |
|
$phpPath = OsFunctions::getPHPExecutableFromPath(); |
|
|
|
|
270
|
1 |
|
$result = $pammutils->runCommand($phpPath . ' ' . $this->apppaths->getConsole() . ' ' . $comando); |
|
|
|
|
271
|
|
|
|
272
|
1 |
|
$this->locksystem->release(); |
273
|
1 |
|
if ($result['errcode'] != 0) { |
274
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
275
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
276
|
|
|
return new Response($view, 500); |
277
|
|
|
} else { |
278
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
279
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Suppress PMD warnings per exit. |
286
|
|
|
* |
287
|
|
|
* @SuppressWarnings(PHPMD) |
288
|
|
|
*/ |
289
|
1 |
|
public function unixCommandAction(Request $request) |
290
|
|
|
{ |
291
|
1 |
|
set_time_limit(0); |
292
|
1 |
|
$pammutils = new PannelloAmministrazioneUtils($this->container); |
293
|
1 |
|
$command = $request->get('unixcommand'); |
|
|
|
|
294
|
|
|
//Se viene lanciato il comando per cancellare il file di lock su bypassa tutto e si lancia |
295
|
1 |
|
$dellockfile = "DELETELOCK"; |
|
|
|
|
296
|
1 |
|
if ($command == $dellockfile) { |
297
|
|
|
$this->locksystem->release(); |
298
|
|
|
return new Response('File di lock cancellato'); |
299
|
|
|
} |
300
|
|
|
|
301
|
1 |
|
if (!$this->locksystem->acquire()) { |
302
|
|
|
return new Response($this->getLockMessage()); |
303
|
|
|
} else { |
304
|
1 |
|
$this->locksystem->acquire(); |
305
|
1 |
|
$result = $pammutils->runCommand($command); |
306
|
|
|
|
307
|
1 |
|
$this->locksystem->release(); |
308
|
|
|
// eseguito deopo la fine del comando |
309
|
1 |
|
if ($result['errcode'] != 0) { |
310
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
311
|
1 |
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
312
|
1 |
|
return new Response($view, 500); |
313
|
|
|
} else { |
314
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
315
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
316
|
|
|
} |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @codeCoverageIgnore |
322
|
|
|
*/ |
323
|
|
|
public function phpunittestAction(Request $request) |
|
|
|
|
324
|
|
|
{ |
325
|
|
|
set_time_limit(0); |
326
|
|
|
$this->apppaths = $this->get("pannelloamministrazione.projectpath"); |
|
|
|
|
327
|
|
|
if (!$this->locksystem->acquire()) { |
328
|
|
|
return new Response($this->getLockMessage()); |
329
|
|
|
} else { |
330
|
|
|
if (!OsFunctions::isWindows()) { |
331
|
|
|
$this->locksystem->acquire(); |
332
|
|
|
//$phpPath = OsFunctions::getPHPExecutableFromPath(); |
|
|
|
|
333
|
|
|
$sepchr = OsFunctions::getSeparator(); |
|
|
|
|
334
|
|
|
$phpPath = OsFunctions::getPHPExecutableFromPath(); |
335
|
|
|
|
336
|
|
|
$command = 'cd ' . $this->apppaths->getRootPath() . $sepchr . |
337
|
|
|
$phpPath . ' ' . 'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'simple-phpunit'; |
338
|
|
|
|
339
|
|
|
$process = new Process($command); |
340
|
|
|
$process->run(); |
341
|
|
|
|
342
|
|
|
$this->locksystem->release(); |
343
|
|
|
// eseguito dopo la fine del comando |
344
|
|
|
if (!$process->isSuccessful()) { |
345
|
|
|
$twigparms = array('errcode' => -1, 'command' => $command, 'message' => $process->getOutput() . $process->getErrorOutput()); |
346
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
347
|
|
|
return new Response($view, 500); |
348
|
|
|
} else { |
349
|
|
|
$twigparms = array('errcode' => 0, 'command' => $command, 'message' => $process->getOutput() . $process->getErrorOutput()); |
350
|
|
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
351
|
|
|
} |
352
|
|
|
} else { |
353
|
|
|
return new Response('Non previsto in ambiente windows!', 500); |
354
|
|
|
} |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
} |
358
|
|
|
|
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.