From ef625639f1a655162ba6453023025bb48b20adc9 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Thu, 29 Mar 2018 17:48:15 +0200 Subject: [PATCH 1/4] Introduce ScriptExecutor --- src/BashScriptExecutor.php | 70 ++------------------- src/ScriptExecutor.php | 124 +++++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 66 deletions(-) create mode 100644 src/ScriptExecutor.php diff --git a/src/BashScriptExecutor.php b/src/BashScriptExecutor.php index 6db9050..9341b38 100644 --- a/src/BashScriptExecutor.php +++ b/src/BashScriptExecutor.php @@ -16,29 +16,9 @@ final class BashScriptExecutor { /** - * @var int + * @var ScriptExecutor */ - private static $tmpCounter = 0; - - /** - * @var string[] - */ - private $scriptParts; - - /** - * @var string - */ - private $cwd; - - /** - * @var ?CliResult - */ - private $result; - - /** - * @var ?string - */ - private $tmpFilePath; + private static $scriptExecutor; /** * @param string[] $scriptParts @@ -46,15 +26,7 @@ final class BashScriptExecutor */ public function __construct($scriptParts, $cwd) { - $this->scriptParts = $scriptParts; - $this->cwd = $cwd; - } - - public function __destruct() - { - if (null !== $this->tmpFilePath) { - @unlink($this->tmpFilePath); - } + $this->scriptExecutor = new ScriptExecutor($scriptParts, $cwd, array('#!/usr/bin/env bash', 'set -e', '')); } /** @@ -75,40 +47,6 @@ public static function create($scriptParts, $cwd) */ public function getResult() { - if (null === $this->result) { - $tmpFileName = 'tmp-'.self::$tmpCounter++.'.sh'; - $tmpFileLines = array_merge(array('#!/usr/bin/env bash', 'set -e', ''), $this->scriptParts); - $this->tmpFilePath = $this->cwd.'/'.$tmpFileName; - file_put_contents($this->tmpFilePath, implode("\n", $tmpFileLines)); - chmod($this->tmpFilePath, 0777); - $command = './'.$tmpFileName; - - $process = new Process($command, $this->cwd); - $process->run(); - - $this->result = new CliResult( - $process->getExitCode(), - $process->getOutput(), - $process->getErrorOutput() - ); - } - - if (0 !== $this->result->getCode()) { - throw new ExecutionException( - $this->result, - sprintf( - "Cannot execute `%s`:\n%s\nCode: %s\nExit text: %s\nError output: %s\nDetails:\n%s", - $command, - implode("\n", array_map(function ($line) { return "$ ${line}"; }, $tmpFileLines)), - $this->result->getCode(), - isset(Process::$exitCodes[$this->result->getCode()]) ? Process::$exitCodes[$this->result->getCode()] : 'Unknown exit code', - $this->result->getError(), - $this->result->getOutput() - ), - $process->getExitCode() - ); - } - - return $this->result; + return $this->scriptExecutor->getResult(); } } diff --git a/src/ScriptExecutor.php b/src/ScriptExecutor.php new file mode 100644 index 0000000..68dcb4f --- /dev/null +++ b/src/ScriptExecutor.php @@ -0,0 +1,124 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace Keradus\CliExecutor; + +use Symfony\Component\Process\Process; + +final class ScriptExecutor +{ + /** + * @var int + */ + private static $tmpCounter = 0; + + /** + * @var string[] + */ + private $scriptInit; + + /** + * @var string[] + */ + private $scriptParts; + + /** + * @var string + */ + private $cwd; + + /** + * @var ?CliResult + */ + private $result; + + /** + * @var ?string + */ + private $tmpFilePath; + + /** + * @param string[] $scriptParts + * @param string $cwd + * @param ?string[] $scriptInit + */ + public function __construct($scriptParts, $cwd, array $scriptInit = null) + { + $this->scriptParts = $scriptParts; + $this->cwd = $cwd; + $this->scriptInit = null !== $scriptInit ? $scriptInit : array('#!/bin/sh', 'set -eu', ''); + } + + public function __destruct() + { + if (null !== $this->tmpFilePath) { + @unlink($this->tmpFilePath); + } + } + + /** + * @param string[] $scriptParts + * @param string $cwd + * @param ?string[] $scriptInit + * + * @return self + */ + public static function create($scriptParts, $cwd, array $scriptInit = null) + { + return new self($scriptParts, $cwd, $scriptInit); + } + + /** + * @param bool $checkCode + * + * @throws ExecutionException + * + * @return CliResult + */ + public function getResult($checkCode = true) + { + if (null === $this->result) { + $tmpFileName = 'tmp-'.self::$tmpCounter++.'.sh'; + $tmpFileLines = array_merge($this->scriptInit, $this->scriptParts); + $this->tmpFilePath = $this->cwd.'/'.$tmpFileName; + file_put_contents($this->tmpFilePath, implode("\n", $tmpFileLines)); + chmod($this->tmpFilePath, 0777); + $command = './'.$tmpFileName; + + $process = new Process($command, $this->cwd); + $process->run(); + + $this->result = new CliResult( + $process->getExitCode(), + $process->getOutput(), + $process->getErrorOutput() + ); + } + + if ($checkCode && 0 !== $this->result->getCode()) { + throw new ExecutionException( + $this->result, + sprintf( + "Cannot execute `%s`:\n%s\nCode: %s\nExit text: %s\nError output: %s\nDetails:\n%s", + $command, + implode("\n", array_map(function ($line) { return "$ ${line}"; }, $tmpFileLines)), + $this->result->getCode(), + isset(Process::$exitCodes[$this->result->getCode()]) ? Process::$exitCodes[$this->result->getCode()] : 'Unknown exit code', + $this->result->getError(), + $this->result->getOutput() + ), + $process->getExitCode() + ); + } + + return $this->result; + } +} From c7b3c88d0414713ecde7e6c4894daf614c5da1f8 Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Thu, 29 Mar 2018 17:50:11 +0200 Subject: [PATCH 2/4] add deprecation --- src/BashScriptExecutor.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/BashScriptExecutor.php b/src/BashScriptExecutor.php index 9341b38..79aa7dd 100644 --- a/src/BashScriptExecutor.php +++ b/src/BashScriptExecutor.php @@ -13,6 +13,9 @@ use Symfony\Component\Process\Process; +/** + * @deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead + */ final class BashScriptExecutor { /** @@ -26,6 +29,7 @@ final class BashScriptExecutor */ public function __construct($scriptParts, $cwd) { + @trigger_error('`\Keradus\CliExecutor\BashScriptExecutor` is deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead.', E_USER_DEPRECATED); $this->scriptExecutor = new ScriptExecutor($scriptParts, $cwd, array('#!/usr/bin/env bash', 'set -e', '')); } From e96904604e3c3c3d293efe21685a1c36152f249e Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Thu, 29 Mar 2018 17:52:56 +0200 Subject: [PATCH 3/4] Fix cs --- src/BashScriptExecutor.php | 2 -- src/ScriptExecutor.php | 8 ++++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/BashScriptExecutor.php b/src/BashScriptExecutor.php index 79aa7dd..ac3438e 100644 --- a/src/BashScriptExecutor.php +++ b/src/BashScriptExecutor.php @@ -11,8 +11,6 @@ namespace Keradus\CliExecutor; -use Symfony\Component\Process\Process; - /** * @deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead */ diff --git a/src/ScriptExecutor.php b/src/ScriptExecutor.php index 68dcb4f..28faff4 100644 --- a/src/ScriptExecutor.php +++ b/src/ScriptExecutor.php @@ -46,8 +46,8 @@ final class ScriptExecutor private $tmpFilePath; /** - * @param string[] $scriptParts - * @param string $cwd + * @param string[] $scriptParts + * @param string $cwd * @param ?string[] $scriptInit */ public function __construct($scriptParts, $cwd, array $scriptInit = null) @@ -65,8 +65,8 @@ public function __destruct() } /** - * @param string[] $scriptParts - * @param string $cwd + * @param string[] $scriptParts + * @param string $cwd * @param ?string[] $scriptInit * * @return self From df1800736355b7390cf6d58f117f01c7be7b89af Mon Sep 17 00:00:00 2001 From: Dariusz Ruminski Date: Thu, 29 Mar 2018 17:55:28 +0200 Subject: [PATCH 4/4] fix --- src/BashScriptExecutor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BashScriptExecutor.php b/src/BashScriptExecutor.php index ac3438e..4c3dfc2 100644 --- a/src/BashScriptExecutor.php +++ b/src/BashScriptExecutor.php @@ -19,7 +19,7 @@ final class BashScriptExecutor /** * @var ScriptExecutor */ - private static $scriptExecutor; + private $scriptExecutor; /** * @param string[] $scriptParts