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