|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Sonata\AdminBundle\Controller\CRUDController as Controller; |
|
6
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
7
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
8
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @author Alexis BUSSIERES <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class AdminCommandController extends Controller |
|
16
|
|
|
{ |
|
17
|
|
|
public function commandAction() |
|
18
|
|
|
{ |
|
19
|
|
|
$request = $this->getRequest(); |
|
20
|
|
|
$query = $request->query; |
|
21
|
|
|
$command = $query->get('name'); |
|
22
|
|
|
$environment = $this->get('kernel')->getEnvironment(); |
|
23
|
|
|
|
|
24
|
|
|
if ($query->has('flush_cache') && $query->getInt('flush_cache') === 1) { |
|
25
|
|
|
$responseFlush = $this->commandFlushCache(); |
|
26
|
|
|
if ($environment === 'dev') { |
|
27
|
|
|
if ($responseFlush->isSuccessful()) { |
|
28
|
|
|
$this->addFlash('success', 'Les caches ont bien été vidés'); |
|
29
|
|
|
} else { |
|
30
|
|
|
$this->addFlash('error', 'Une erreur est survenue lors de la supression des caches !'); |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$responseCommand = $this->executeCommand(['command' => $command]); |
|
36
|
|
|
if ($responseCommand->isSuccessful()) { |
|
37
|
|
|
$this->addFlash( |
|
38
|
|
|
'success', |
|
39
|
|
|
sprintf('La commande "%s" a bien été exécuté.', $command) |
|
40
|
|
|
); |
|
41
|
|
|
} else { |
|
42
|
|
|
$this->addFlash( |
|
43
|
|
|
'error', |
|
44
|
|
|
sprintf('Une erreur est survenue lors de l\'exécution de la commande "%s"!', $command) |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return $this->redirectTo($this->admin->generateUrl('list')); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function listAction() |
|
52
|
|
|
{ |
|
53
|
|
|
$request = $this->getRequest(); |
|
54
|
|
|
|
|
55
|
|
|
$this->admin->checkAccess('list'); |
|
56
|
|
|
|
|
57
|
|
|
$preResponse = $this->preList($request); |
|
58
|
|
|
if ($preResponse !== null) { |
|
59
|
|
|
return $preResponse; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
if ($listMode = $request->get('_list_mode')) { |
|
63
|
|
|
$this->admin->setListMode($listMode); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->render('AlpixelCMSBundle:admin/page:base_list.html.twig', [ |
|
|
|
|
|
|
67
|
|
|
'object' => null, |
|
68
|
|
|
'action' => 'list', |
|
69
|
|
|
'csrf_token' => $this->getCsrfToken('sonata.batch'), |
|
70
|
|
|
], null, $request); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function commandFlushCache() |
|
74
|
|
|
{ |
|
75
|
|
|
$environment = $this->get('kernel')->getEnvironment(); |
|
76
|
|
|
$inputs = [ |
|
77
|
|
|
'command' => 'cache:clear', |
|
78
|
|
|
'--env' => $environment, |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
return $this->executeCommand($inputs); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function executeCommand(array $inputs) |
|
85
|
|
|
{ |
|
86
|
|
|
if (empty($inputs) || !isset($inputs['command'])) { |
|
87
|
|
|
throw new \Exception('The array is empty or "command" index is missing'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$kernel = $this->get('kernel'); |
|
91
|
|
|
$application = new Application($kernel); |
|
92
|
|
|
$application->setAutoExit(false); |
|
93
|
|
|
|
|
94
|
|
|
$input = new ArrayInput($inputs); |
|
95
|
|
|
$output = new BufferedOutput( |
|
96
|
|
|
OutputInterface::VERBOSITY_NORMAL, |
|
97
|
|
|
true |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
$application->run($input, $output); |
|
101
|
|
|
$content = $output->fetch(); |
|
102
|
|
|
|
|
103
|
|
|
return new Response($content); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: