1 | <?php |
||||
2 | /* (c) Anton Medvedev <[email protected]> |
||||
3 | * |
||||
4 | * For the full copyright and license information, please view the LICENSE |
||||
5 | * file that was distributed with this source code. |
||||
6 | */ |
||||
7 | |||||
8 | namespace Deployer\Component\ProcessRunner; |
||||
9 | |||||
10 | use Deployer\Deployer; |
||||
11 | use Deployer\Component\ProcessRunner\Printer; |
||||
12 | use Deployer\Exception\RunException; |
||||
13 | use Deployer\Host\Host; |
||||
14 | use Deployer\Logger\Logger; |
||||
15 | use Symfony\Component\Process\Exception\ProcessFailedException; |
||||
16 | use Symfony\Component\Process\Process; |
||||
17 | |||||
18 | class ProcessRunner |
||||
19 | { |
||||
20 | private $pop; |
||||
21 | private $logger; |
||||
22 | |||||
23 | 10 | public function __construct(Printer $pop, Logger $logger) |
|||
24 | { |
||||
25 | 10 | $this->pop = $pop; |
|||
26 | 10 | $this->logger = $logger; |
|||
27 | 10 | } |
|||
28 | |||||
29 | /** |
||||
30 | * Runs a command, consider deployer global configs (timeout,...) |
||||
31 | * |
||||
32 | * @param Host $host |
||||
33 | * @param string $command |
||||
34 | * @param array $config |
||||
35 | * |
||||
36 | * @return string |
||||
37 | * |
||||
38 | */ |
||||
39 | 10 | public function run(Host $host, string $command, array $config = []): string |
|||
40 | { |
||||
41 | $defaults = [ |
||||
42 | 10 | 'timeout' => $host->get('default_timeout', 300), |
|||
43 | 'idle_timeout' => null, |
||||
44 | 10 | 'cwd' => defined('DEPLOYER_ROOT') ? DEPLOYER_ROOT : null, |
|||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
45 | 'tty' => false, |
||||
46 | ]; |
||||
47 | 10 | $config = array_merge($defaults, $config); |
|||
48 | |||||
49 | 10 | $this->pop->command($host, $command); |
|||
50 | |||||
51 | 10 | $terminalOutput = $this->pop->callback($host); |
|||
52 | $callback = function ($type, $buffer) use ($host, $terminalOutput) { |
||||
53 | 9 | $this->logger->printBuffer($host, $type, $buffer); |
|||
54 | 9 | $terminalOutput($type, $buffer); |
|||
55 | 10 | }; |
|||
56 | |||||
57 | 10 | $command = str_replace('%secret%', $config['secret'] ?? '', $command); |
|||
58 | 10 | $command = str_replace('%sudo_pass%', $config['sudo_pass'] ?? '', $command); |
|||
59 | |||||
60 | 10 | $process = Process::fromShellCommandline($command) |
|||
61 | 10 | ->setTimeout($config['timeout']) |
|||
62 | 10 | ->setIdleTimeout($config['idle_timeout']) |
|||
63 | 10 | ->setTty($config['tty']); |
|||
64 | |||||
65 | 10 | if ($config['cwd'] !== null) { |
|||
66 | $process->setWorkingDirectory($config['cwd']); |
||||
67 | } |
||||
68 | |||||
69 | try { |
||||
70 | 10 | $process->mustRun($callback); |
|||
71 | 10 | return $process->getOutput(); |
|||
72 | 1 | } catch (ProcessFailedException $exception) { |
|||
73 | 1 | throw new RunException( |
|||
74 | 1 | $host, |
|||
75 | $command, |
||||
76 | 1 | $process->getExitCode(), |
|||
0 ignored issues
–
show
It seems like
$process->getExitCode() can also be of type null ; however, parameter $exitCode of Deployer\Exception\RunException::__construct() does only seem to accept integer , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
77 | 1 | $process->getOutput(), |
|||
78 | 1 | $process->getErrorOutput() |
|||
79 | ); |
||||
80 | } |
||||
81 | } |
||||
82 | } |
||||
83 |