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