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 |
|
$projectparentdir = dirname($projectDir); |
95
|
1 |
|
$envvars = $projectparentdir.DIRECTORY_SEPARATOR.'envvars'; |
|
|
|
|
96
|
1 |
|
$composercachedir = $projectparentdir.DIRECTORY_SEPARATOR.'.composer'; |
97
|
1 |
|
$composerinstall = ''; |
|
|
|
|
98
|
1 |
|
if (false == $windows) { |
|
|
|
|
99
|
1 |
|
if (file_exists($envvars)) { |
100
|
|
|
$composerinstall = $composerinstall.'. '.$envvars.' && '; |
101
|
|
|
} |
102
|
1 |
|
if (file_exists($composercachedir)) { |
103
|
|
|
$composerinstall = $composerinstall.' export COMPOSER_HOME='.$composercachedir.' && '; |
104
|
|
|
} |
105
|
1 |
|
$composerinstall = $composerinstall.' cd '.$projectDir.' && composer install --no-interaction 2>&1'; |
106
|
1 |
|
$sed = "sed -i -e 's/cercaquestastringa/sostituisciconquestastringa/g' ".$projectDir.'/.env'; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$comandishell = array( |
110
|
1 |
|
$this->fixSlash($dellogsfiles), |
111
|
1 |
|
$this->fixSlash($delcacheprodfiles), |
112
|
1 |
|
$this->fixSlash($delcachedevfiles), |
113
|
1 |
|
$this->fixSlash($setmaintenancefile), |
114
|
1 |
|
$this->fixSlash($remmaintenancefile), |
115
|
1 |
|
$composerinstall, |
116
|
1 |
|
$sed, ); |
|
|
|
|
117
|
|
|
|
118
|
|
|
$comandisymfony = array( |
119
|
1 |
|
'list', |
120
|
1 |
|
'cache:clear --env=prod --no-debug', |
121
|
1 |
|
'fos:user:create admin pass [email protected]', |
122
|
1 |
|
'fos:user:promote username ROLE_SUPER_ADMIN', |
123
|
1 |
|
"assets:install $projectDir/public", |
|
|
|
|
124
|
1 |
|
'pannelloamministrazione:checkgitversion', |
125
|
|
|
); |
126
|
|
|
|
127
|
1 |
|
$entities = $this->findEntities(); |
128
|
1 |
|
sort($entities); |
129
|
|
|
|
130
|
|
|
$twigparms = array( |
131
|
1 |
|
'svn' => $svn, 'git' => $git, 'mwbs' => $mwbs, 'entities' => $entities, |
132
|
1 |
|
'rootdir' => $this->fixSlash($projectDir), |
133
|
1 |
|
'comandishell' => $comandishell, |
134
|
1 |
|
'comandisymfony' => $comandisymfony, |
135
|
1 |
|
'iswindows' => $windows, |
136
|
1 |
|
'appname' => $this->appname, |
137
|
|
|
); |
138
|
|
|
|
139
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:index.html.twig', $twigparms); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
private function fixSlash($path) |
143
|
|
|
{ |
144
|
1 |
|
return str_replace('\\', '\\\\', $path); |
145
|
|
|
} |
146
|
|
|
|
147
|
1 |
|
private function getLockMessage() |
148
|
|
|
{ |
149
|
1 |
|
return "<h2 style='color: orange;'>E' già in esecuzione un comando, riprova tra qualche secondo!</h2>"; |
150
|
|
|
} |
151
|
|
|
|
152
|
1 |
|
public function aggiornaSchemaDatabase() |
153
|
|
|
{ |
154
|
1 |
|
if (!$this->locksystem->acquire()) { |
155
|
|
|
return new Response($this->getLockMessage()); |
156
|
|
|
} else { |
157
|
1 |
|
$this->locksystem->acquire(); |
158
|
1 |
|
$command = $this->pacommands; |
159
|
1 |
|
$result = $command->aggiornaSchemaDatabase(); |
|
|
|
|
160
|
|
|
|
161
|
1 |
|
$this->locksystem->release(); |
162
|
1 |
|
if (0 != $result['errcode']) { |
163
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
164
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
165
|
|
|
|
166
|
|
|
return new Response($view, 500); |
167
|
|
|
} else { |
168
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
169
|
|
|
|
170
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/* FORMS */ |
176
|
|
|
|
177
|
1 |
|
public function generateFormCrud(Request $request) |
178
|
|
|
{ |
179
|
1 |
|
if (!$this->locksystem->acquire()) { |
180
|
|
|
return new Response($this->getLockMessage()); |
181
|
|
|
} else { |
182
|
1 |
|
$entityform = $request->get('entityform'); |
|
|
|
|
183
|
1 |
|
$generatemplate = 'true' === $request->get('generatemplate') ? true : false; |
184
|
1 |
|
$this->locksystem->acquire(); |
185
|
|
|
|
186
|
1 |
|
$command = $this->pacommands; |
187
|
1 |
|
$result = $command->generateFormCrud($entityform, $generatemplate); |
|
|
|
|
188
|
|
|
|
189
|
1 |
|
$this->locksystem->release(); |
190
|
|
|
//$retcc = ''; |
|
|
|
|
191
|
1 |
|
if ($result['errcode'] < 0) { |
192
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => 'Generazione Form Crud', 'message' => $result['message']); |
193
|
|
|
|
194
|
1 |
|
return new Response( |
195
|
1 |
|
$this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms), |
196
|
1 |
|
500 |
197
|
|
|
); |
198
|
|
|
} else { |
|
|
|
|
199
|
|
|
//$retcc = $command->clearCacheEnv($this->get('kernel')->getEnvironment()); |
|
|
|
|
200
|
|
|
} |
201
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
202
|
|
|
|
203
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/* ENTITIES */ |
208
|
|
|
|
209
|
1 |
|
public function generateEntity(Request $request) |
210
|
|
|
{ |
211
|
1 |
|
if (!$this->locksystem->acquire()) { |
212
|
|
|
return new Response($this->getLockMessage()); |
213
|
|
|
} else { |
214
|
1 |
|
$this->locksystem->acquire(); |
215
|
1 |
|
$wbFile = $request->get('file'); |
|
|
|
|
216
|
1 |
|
$command = $this->pacommands; |
|
|
|
|
217
|
1 |
|
$result = $command->generateEntity($wbFile); |
|
|
|
|
218
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
|
|
|
|
219
|
1 |
|
$this->locksystem->release(); |
220
|
1 |
|
if (0 != $result['errcode']) { |
221
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
222
|
1 |
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
223
|
|
|
|
224
|
1 |
|
return new Response($view, 500); |
225
|
|
|
} else { |
226
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
227
|
|
|
|
228
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/* VCS (GIT,SVN) */ |
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* @codeCoverageIgnore |
237
|
|
|
*/ |
238
|
|
|
public function getVcs() |
239
|
|
|
{ |
240
|
|
|
set_time_limit(0); |
241
|
|
|
$this->apppaths = $this->apppaths; |
242
|
|
|
if (!$this->locksystem->acquire()) { |
243
|
|
|
return new Response($this->getLockMessage()); |
244
|
|
|
} else { |
245
|
|
|
$this->locksystem->acquire(); |
246
|
|
|
$command = $this->pacommands; |
247
|
|
|
$result = $command->getVcs(); |
|
|
|
|
248
|
|
|
$this->locksystem->release(); |
249
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
250
|
|
|
|
251
|
|
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/* CLEAR CACHE */ |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Suppress PMD warnings per exit. |
259
|
|
|
* |
260
|
|
|
* @//SuppressWarnings(PHPMD) |
261
|
|
|
*/ |
262
|
1 |
|
public function clearCache(Request $request) |
|
|
|
|
263
|
|
|
{ |
264
|
1 |
|
set_time_limit(0); |
265
|
1 |
|
if (!$this->locksystem->acquire()) { |
266
|
1 |
|
return new Response($this->getLockMessage()); |
267
|
|
|
} else { |
268
|
1 |
|
$this->locksystem->acquire(); |
269
|
1 |
|
$command = $this->pacommands; |
270
|
1 |
|
$result = $command->clearcache(); |
|
|
|
|
271
|
|
|
|
272
|
|
|
$this->locksystem->release(); |
273
|
|
|
|
274
|
|
|
if (0 != $result['errcode']) { |
275
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
276
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
277
|
|
|
|
278
|
|
|
return new Response($view, 500); |
279
|
|
|
} else { |
280
|
|
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
281
|
|
|
|
282
|
|
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/* CLEAR CACHE */ |
288
|
|
|
|
289
|
1 |
|
public function symfonyCommand(Request $request) |
290
|
|
|
{ |
291
|
1 |
|
set_time_limit(0); |
292
|
|
|
|
293
|
1 |
|
$simfonycommand = $request->get('symfonycommand'); |
294
|
1 |
|
if (!$this->locksystem->acquire()) { |
295
|
|
|
return new Response($this->getLockMessage()); |
296
|
|
|
} else { |
297
|
1 |
|
$this->locksystem->acquire(); |
298
|
1 |
|
$this->apppaths = $this->apppaths; |
299
|
1 |
|
$pammutils = $this->pautils; |
|
|
|
|
300
|
1 |
|
$command = $this->apppaths->getConsole().' '.$simfonycommand; |
|
|
|
|
301
|
1 |
|
$result = $pammutils->runCommand($command); |
|
|
|
|
302
|
|
|
|
303
|
1 |
|
$this->locksystem->release(); |
304
|
1 |
|
if (0 != $result['errcode']) { |
305
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
306
|
1 |
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
307
|
|
|
|
308
|
1 |
|
return new Response($view, 500); |
309
|
|
|
} else { |
310
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
311
|
|
|
|
312
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
313
|
|
|
} |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Suppress PMD warnings per exit. |
319
|
|
|
* |
320
|
|
|
* @SuppressWarnings(PHPMD) |
321
|
|
|
*/ |
322
|
1 |
|
public function unixCommand(Request $request) |
323
|
|
|
{ |
324
|
1 |
|
set_time_limit(0); |
325
|
1 |
|
$pammutils = $this->pautils; |
|
|
|
|
326
|
1 |
|
$unixcommand = $request->get('unixcommand'); |
327
|
|
|
//Se viene lanciato il comando per cancellare il file di lock su bypassa tutto e si lancia |
328
|
1 |
|
$dellockfile = 'DELETELOCK'; |
329
|
1 |
|
if ($unixcommand == $dellockfile) { |
330
|
|
|
$this->locksystem->release(); |
331
|
|
|
|
332
|
|
|
return new Response('File di lock cancellato'); |
333
|
|
|
} |
334
|
|
|
|
335
|
1 |
|
if (!$this->locksystem->acquire()) { |
336
|
|
|
return new Response($this->getLockMessage()); |
337
|
|
|
} else { |
338
|
1 |
|
$this->locksystem->acquire(); |
339
|
1 |
|
$result = $pammutils->runCommand($unixcommand); |
340
|
|
|
|
341
|
1 |
|
$this->locksystem->release(); |
342
|
|
|
// eseguito deopo la fine del comando |
343
|
1 |
|
if (0 != $result['errcode']) { |
344
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
345
|
1 |
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
346
|
|
|
|
347
|
1 |
|
return new Response($view, 500); |
348
|
|
|
} else { |
349
|
1 |
|
$twigparms = array('errcode' => $result['errcode'], 'command' => $result['command'], 'message' => $result['message']); |
350
|
|
|
|
351
|
1 |
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @codeCoverageIgnore |
358
|
|
|
*/ |
359
|
|
|
public function phpunittest(Request $request) |
|
|
|
|
360
|
|
|
{ |
361
|
|
|
set_time_limit(0); |
362
|
|
|
$this->apppaths = $this->apppaths; |
363
|
|
|
if (!$this->locksystem->acquire()) { |
364
|
|
|
return new Response($this->getLockMessage()); |
365
|
|
|
} else { |
366
|
|
|
if (!OsFunctions::isWindows()) { |
367
|
|
|
$this->locksystem->acquire(); |
368
|
|
|
//$phpPath = OsFunctions::getPHPExecutableFromPath(); |
|
|
|
|
369
|
|
|
$command = 'vendor'.DIRECTORY_SEPARATOR.'bin'.DIRECTORY_SEPARATOR.'simple-phpunit'; |
370
|
|
|
$process = new Process(array($command)); |
371
|
|
|
$process->setWorkingDirectory($this->apppaths->getRootPath()); |
372
|
|
|
|
373
|
|
|
$process->run(); |
374
|
|
|
|
375
|
|
|
$this->locksystem->release(); |
376
|
|
|
// eseguito dopo la fine del comando |
377
|
|
|
if (!$process->isSuccessful()) { |
378
|
|
|
$twigparms = array('errcode' => -1, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput()); |
379
|
|
|
$view = $this->renderView('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
|
|
|
|
380
|
|
|
|
381
|
|
|
return new Response($view, 500); |
382
|
|
|
} else { |
383
|
|
|
$twigparms = array('errcode' => 0, 'command' => $command, 'message' => $process->getOutput().$process->getErrorOutput()); |
384
|
|
|
|
385
|
|
|
return $this->render('PannelloAmministrazioneBundle:PannelloAmministrazione:outputcommand.html.twig', $twigparms); |
386
|
|
|
} |
387
|
|
|
} else { |
388
|
|
|
return new Response('Non previsto in ambiente windows!', 500); |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
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.