|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kunstmaan\Skylab\Provider; |
|
4
|
|
|
|
|
5
|
|
|
use Cilex\Application; |
|
6
|
|
|
use Symfony\Component\Process\Process; |
|
7
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* ProcessProvider |
|
11
|
|
|
*/ |
|
12
|
|
|
class ProcessProvider extends AbstractProvider |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Registers services on the given app. |
|
16
|
|
|
* |
|
17
|
|
|
* @param Application $app An Application instance |
|
18
|
|
|
*/ |
|
19
|
|
|
public function register(Application $app) |
|
20
|
|
|
{ |
|
21
|
|
|
$app['process'] = $this; |
|
22
|
|
|
$this->app = $app; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $command The command |
|
27
|
|
|
* @param bool $silent Be silent or not |
|
28
|
|
|
* |
|
29
|
|
|
* @param \Closure $callback |
|
|
|
|
|
|
30
|
|
|
* @return bool|string |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
public function executeCommand($command, $silent = false, \Closure $callback = null, $env=array()) |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->performCommand($command, $silent, $callback, $env); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $command The command |
|
39
|
|
|
* @param bool $silent Be silent or not |
|
40
|
|
|
* @param string $sudoAs Sudo as a different user then the root user |
|
|
|
|
|
|
41
|
|
|
* |
|
42
|
|
|
* @return bool|string |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
public function executeSudoCommand($command, $silent = false, $sudoAs = null, \Closure $callback = null, $env=array()) |
|
45
|
|
|
{ |
|
46
|
|
|
if (empty($sudoAs)) { |
|
47
|
|
|
$command = 'sudo -s -p "Please enter your sudo password:" ' . $command; |
|
48
|
|
|
} else { |
|
49
|
|
|
$command = 'sudo -s -p "Please enter your sudo password:" -u ' . $sudoAs . ' ' . $command; |
|
50
|
|
|
} |
|
51
|
|
|
return $this->performCommand($command, $silent, $callback, $env); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $cmd |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
|
|
public function commandExists($cmd) |
|
59
|
|
|
{ |
|
60
|
|
|
return shell_exec("hash " . $cmd . " 2>&1") == ''; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param $command |
|
65
|
|
|
* @param $silent |
|
66
|
|
|
* @param \Closure $callback |
|
|
|
|
|
|
67
|
|
|
* @param $env |
|
68
|
|
|
* @return bool|string |
|
|
|
|
|
|
69
|
|
|
* @throws \Kunstmaan\Skylab\Exceptions\SkylabException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function performCommand($command, $silent=false, \Closure $callback = null, $env=array()) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$startTime = microtime(true); |
|
74
|
|
|
|
|
75
|
|
|
if (!$silent) { |
|
76
|
|
|
$this->dialogProvider->logCommand($command); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$env = array_replace($_ENV, $_SERVER, $env); |
|
80
|
|
|
$process = new Process($command, null, $env); |
|
81
|
|
|
$process->setTimeout(14400*100); |
|
82
|
|
|
$process->run($callback); |
|
83
|
|
|
if (!$silent) { |
|
84
|
|
|
$this->dialogProvider->logCommandTime($startTime); |
|
85
|
|
|
} |
|
86
|
|
|
if (!$process->isSuccessful()) { |
|
87
|
|
|
if ($process->getExitCode() == 23){ |
|
88
|
|
|
return $process->getOutput(); |
|
89
|
|
|
} else { |
|
90
|
|
|
if (!$silent) { |
|
91
|
|
|
$this->dialogProvider->logError($process->getErrorOutput()); |
|
92
|
|
|
} |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
return $process->getOutput(); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.