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