deployphp /
deployer
| 1 | <?php |
||||
| 2 | |||||
| 3 | declare(strict_types=1); |
||||
| 4 | |||||
| 5 | /* (c) Anton Medvedev <[email protected]> |
||||
| 6 | * |
||||
| 7 | * For the full copyright and license information, please view the LICENSE |
||||
| 8 | * file that was distributed with this source code. |
||||
| 9 | */ |
||||
| 10 | |||||
| 11 | namespace Deployer\ProcessRunner; |
||||
| 12 | |||||
| 13 | use Deployer\Exception\RunException; |
||||
| 14 | use Deployer\Exception\TimeoutException; |
||||
| 15 | use Deployer\Host\Host; |
||||
| 16 | use Deployer\Logger\Logger; |
||||
| 17 | use Deployer\Ssh\RunParams; |
||||
| 18 | use Symfony\Component\Process\Exception\ProcessFailedException; |
||||
| 19 | use Symfony\Component\Process\Exception\ProcessTimedOutException; |
||||
| 20 | use Symfony\Component\Process\Process; |
||||
| 21 | |||||
| 22 | use function Deployer\Support\deployer_root; |
||||
| 23 | use function Deployer\Support\env_stringify; |
||||
| 24 | |||||
| 25 | class ProcessRunner |
||||
| 26 | { |
||||
| 27 | private Printer $pop; |
||||
| 28 | private Logger $logger; |
||||
| 29 | |||||
| 30 | public function __construct(Printer $pop, Logger $logger) |
||||
| 31 | { |
||||
| 32 | $this->pop = $pop; |
||||
| 33 | $this->logger = $logger; |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | public function run(Host $host, string $command, RunParams $params): string |
||||
| 37 | { |
||||
| 38 | $this->pop->command($host, 'run', $command); |
||||
| 39 | |||||
| 40 | $terminalOutput = $this->pop->callback($host, $params->forceOutput); |
||||
| 41 | $callback = function ($type, $buffer) use ($host, $terminalOutput) { |
||||
| 42 | $this->logger->printBuffer($host, $type, $buffer); |
||||
| 43 | $terminalOutput($type, $buffer); |
||||
| 44 | }; |
||||
| 45 | |||||
| 46 | if (!empty($params->secrets)) { |
||||
| 47 | foreach ($params->secrets as $key => $value) { |
||||
| 48 | $command = str_replace('%' . $key . '%', $value, $command); |
||||
| 49 | } |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | if (!empty($params->env)) { |
||||
| 53 | $env = env_stringify($params->env); |
||||
| 54 | $command = "export $env; $command"; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | if (!empty($params->dotenv)) { |
||||
| 58 | $command = "source $params->dotenv; $command"; |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | $process = Process::fromShellCommandline($params->shell) |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 62 | ->setInput($command) |
||||
| 63 | ->setTimeout($params->timeout) |
||||
| 64 | ->setIdleTimeout($params->idleTimeout) |
||||
| 65 | ->setWorkingDirectory($params->cwd ?? deployer_root()); |
||||
| 66 | |||||
| 67 | try { |
||||
| 68 | $process->mustRun($callback); |
||||
| 69 | return $process->getOutput(); |
||||
| 70 | } catch (ProcessFailedException) { |
||||
| 71 | if ($params->nothrow) { |
||||
| 72 | return ''; |
||||
| 73 | } |
||||
| 74 | throw new RunException( |
||||
| 75 | $host, |
||||
| 76 | $command, |
||||
| 77 | $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...
|
|||||
| 78 | $process->getOutput(), |
||||
| 79 | $process->getErrorOutput(), |
||||
| 80 | ); |
||||
| 81 | } catch (ProcessTimedOutException $exception) { // @phpstan-ignore-line PHPStan doesn't know about ProcessTimedOutException for some reason. |
||||
| 82 | throw new TimeoutException( |
||||
| 83 | $command, |
||||
| 84 | $exception->getExceededTimeout(), |
||||
| 85 | ); |
||||
| 86 | } |
||||
| 87 | } |
||||
| 88 | } |
||||
| 89 |