|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cdf\PannelloAmministrazioneBundle\Utils; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application; |
|
6
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
|
7
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
|
9
|
|
|
use Symfony\Component\Process\Process; |
|
10
|
|
|
|
|
11
|
|
|
class Utility |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
private ProjectPath $apppaths; |
|
15
|
|
|
private Kernel $kernel; |
|
16
|
|
|
|
|
17
|
1 |
|
public function __construct(Kernel $kernel, ProjectPath $projectpath) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$this->apppaths = $projectpath; |
|
20
|
1 |
|
$this->kernel = $kernel; |
|
21
|
|
|
} |
|
22
|
|
|
/** |
|
23
|
|
|
* |
|
24
|
|
|
* @param string $env |
|
25
|
|
|
* @return array<mixed> |
|
26
|
|
|
*/ |
|
27
|
|
|
public function clearcache($env = '') : array |
|
28
|
|
|
{ |
|
29
|
|
|
if (!$env) { |
|
30
|
|
|
$env = $this->kernel->getEnvironment(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$command = $this->apppaths->getConsoleExecute() . ' cache:clear --env=' . $env; |
|
34
|
|
|
|
|
35
|
|
|
return self::runCommand($command); |
|
36
|
|
|
} |
|
37
|
|
|
/** |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $command |
|
40
|
|
|
* @param string $workingdir |
|
41
|
|
|
* @return array<mixed> |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public static function runCommand(string $command, string $workingdir = '.') |
|
44
|
|
|
{ |
|
45
|
|
|
/* @var $process \Symfony\Component\Process\Process */ |
|
46
|
1 |
|
$process = Process::fromShellCommandline($command); |
|
47
|
|
|
|
|
48
|
1 |
|
if ($workingdir) { |
|
49
|
1 |
|
$process->setWorkingDirectory($workingdir); |
|
50
|
|
|
} |
|
51
|
1 |
|
$process->setTimeout(60 * 60 * 24); |
|
52
|
1 |
|
$process->run(); |
|
53
|
|
|
|
|
54
|
1 |
|
if (!$process->isSuccessful()) { |
|
55
|
1 |
|
$return = array('errcode' => -1, |
|
56
|
|
|
'command' => $command, |
|
57
|
1 |
|
'message' => 'Errore nel comando ' . $command . "\n" . $process->getErrorOutput() . "\n" . $process->getOutput(),); |
|
58
|
|
|
} else { |
|
59
|
1 |
|
$return = array('errcode' => 0, |
|
60
|
|
|
'command' => $command, |
|
61
|
1 |
|
'message' => $process->getOutput(), |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $command |
|
71
|
|
|
* @param array<mixed> $options |
|
72
|
|
|
* @return array<mixed> |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function runSymfonyCommand(string $command, array $options = array()) : array |
|
75
|
|
|
{ |
|
76
|
1 |
|
$application = new Application($this->kernel); |
|
77
|
1 |
|
$application->setAutoExit(false); |
|
78
|
|
|
|
|
79
|
1 |
|
$cmdoptions = array_merge(array('command' => $command), $options); |
|
80
|
|
|
|
|
81
|
1 |
|
$outputbuf = new BufferedOutput(); |
|
82
|
|
|
// return the output, don't use if you used NullOutput() |
|
83
|
1 |
|
$returncode = $application->run(new ArrayInput($cmdoptions), $outputbuf); |
|
84
|
1 |
|
$output = $outputbuf->fetch(); |
|
85
|
|
|
|
|
86
|
1 |
|
return array('errcode' => (0 == $returncode ? 0 : 1), 'command' => $cmdoptions['command'], 'message' => $output); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|