|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of CLI Executor. |
| 5 | + * |
| 6 | + * (c) Dariusz Rumiński <[email protected]> |
| 7 | + * |
| 8 | + * This source file is subject to the MIT license that is bundled |
| 9 | + * with this source code in the file LICENSE. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Keradus\CliExecutor; |
| 13 | + |
| 14 | +use Symfony\Component\Process\Process; |
| 15 | + |
| 16 | +final class ScriptExecutor |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var int |
| 20 | + */ |
| 21 | + private static $tmpCounter = 0; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var string[] |
| 25 | + */ |
| 26 | + private $scriptInit; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var string[] |
| 30 | + */ |
| 31 | + private $scriptParts; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var string |
| 35 | + */ |
| 36 | + private $cwd; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var ?CliResult |
| 40 | + */ |
| 41 | + private $result; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var ?string |
| 45 | + */ |
| 46 | + private $tmpFilePath; |
| 47 | + |
| 48 | + /** |
| 49 | + * @param string[] $scriptParts |
| 50 | + * @param string $cwd |
| 51 | + * @param ?string[] $scriptInit |
| 52 | + */ |
| 53 | + public function __construct($scriptParts, $cwd, array $scriptInit = null) |
| 54 | + { |
| 55 | + $this->scriptParts = $scriptParts; |
| 56 | + $this->cwd = $cwd; |
| 57 | + $this->scriptInit = null !== $scriptInit ? $scriptInit : array('#!/bin/sh', 'set -eu', ''); |
| 58 | + } |
| 59 | + |
| 60 | + public function __destruct() |
| 61 | + { |
| 62 | + if (null !== $this->tmpFilePath) { |
| 63 | + @unlink($this->tmpFilePath); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string[] $scriptParts |
| 69 | + * @param string $cwd |
| 70 | + * @param ?string[] $scriptInit |
| 71 | + * |
| 72 | + * @return self |
| 73 | + */ |
| 74 | + public static function create($scriptParts, $cwd, array $scriptInit = null) |
| 75 | + { |
| 76 | + return new self($scriptParts, $cwd, $scriptInit); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param bool $checkCode |
| 81 | + * |
| 82 | + * @throws ExecutionException |
| 83 | + * |
| 84 | + * @return CliResult |
| 85 | + */ |
| 86 | + public function getResult($checkCode = true) |
| 87 | + { |
| 88 | + if (null === $this->result) { |
| 89 | + $tmpFileName = 'tmp-'.self::$tmpCounter++.'.sh'; |
| 90 | + $tmpFileLines = array_merge($this->scriptInit, $this->scriptParts); |
| 91 | + $this->tmpFilePath = $this->cwd.'/'.$tmpFileName; |
| 92 | + file_put_contents($this->tmpFilePath, implode("\n", $tmpFileLines)); |
| 93 | + chmod($this->tmpFilePath, 0777); |
| 94 | + $command = './'.$tmpFileName; |
| 95 | + |
| 96 | + $process = new Process($command, $this->cwd); |
| 97 | + $process->run(); |
| 98 | + |
| 99 | + $this->result = new CliResult( |
| 100 | + $process->getExitCode(), |
| 101 | + $process->getOutput(), |
| 102 | + $process->getErrorOutput() |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + if ($checkCode && 0 !== $this->result->getCode()) { |
| 107 | + throw new ExecutionException( |
| 108 | + $this->result, |
| 109 | + sprintf( |
| 110 | + "Cannot execute `%s`:\n%s\nCode: %s\nExit text: %s\nError output: %s\nDetails:\n%s", |
| 111 | + $command, |
| 112 | + implode("\n", array_map(function ($line) { return "$ ${line}"; }, $tmpFileLines)), |
| 113 | + $this->result->getCode(), |
| 114 | + isset(Process::$exitCodes[$this->result->getCode()]) ? Process::$exitCodes[$this->result->getCode()] : 'Unknown exit code', |
| 115 | + $this->result->getError(), |
| 116 | + $this->result->getOutput() |
| 117 | + ), |
| 118 | + $process->getExitCode() |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + return $this->result; |
| 123 | + } |
| 124 | +} |
0 commit comments