Skip to content

Commit 4f93a5f

Browse files
authored
Introduce ScriptExecutor (#1)
1 parent 36a5919 commit 4f93a5f

File tree

2 files changed

+132
-68
lines changed

2 files changed

+132
-68
lines changed

src/BashScriptExecutor.php

+8-68
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,24 @@
1111

1212
namespace Keradus\CliExecutor;
1313

14-
use Symfony\Component\Process\Process;
15-
14+
/**
15+
* @deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead
16+
*/
1617
final class BashScriptExecutor
1718
{
1819
/**
19-
* @var int
20-
*/
21-
private static $tmpCounter = 0;
22-
23-
/**
24-
* @var string[]
25-
*/
26-
private $scriptParts;
27-
28-
/**
29-
* @var string
30-
*/
31-
private $cwd;
32-
33-
/**
34-
* @var ?CliResult
20+
* @var ScriptExecutor
3521
*/
36-
private $result;
37-
38-
/**
39-
* @var ?string
40-
*/
41-
private $tmpFilePath;
22+
private $scriptExecutor;
4223

4324
/**
4425
* @param string[] $scriptParts
4526
* @param string $cwd
4627
*/
4728
public function __construct($scriptParts, $cwd)
4829
{
49-
$this->scriptParts = $scriptParts;
50-
$this->cwd = $cwd;
51-
}
52-
53-
public function __destruct()
54-
{
55-
if (null !== $this->tmpFilePath) {
56-
@unlink($this->tmpFilePath);
57-
}
30+
@trigger_error('`\Keradus\CliExecutor\BashScriptExecutor` is deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead.', E_USER_DEPRECATED);
31+
$this->scriptExecutor = new ScriptExecutor($scriptParts, $cwd, array('#!/usr/bin/env bash', 'set -e', ''));
5832
}
5933

6034
/**
@@ -75,40 +49,6 @@ public static function create($scriptParts, $cwd)
7549
*/
7650
public function getResult()
7751
{
78-
if (null === $this->result) {
79-
$tmpFileName = 'tmp-'.self::$tmpCounter++.'.sh';
80-
$tmpFileLines = array_merge(array('#!/usr/bin/env bash', 'set -e', ''), $this->scriptParts);
81-
$this->tmpFilePath = $this->cwd.'/'.$tmpFileName;
82-
file_put_contents($this->tmpFilePath, implode("\n", $tmpFileLines));
83-
chmod($this->tmpFilePath, 0777);
84-
$command = './'.$tmpFileName;
85-
86-
$process = new Process($command, $this->cwd);
87-
$process->run();
88-
89-
$this->result = new CliResult(
90-
$process->getExitCode(),
91-
$process->getOutput(),
92-
$process->getErrorOutput()
93-
);
94-
}
95-
96-
if (0 !== $this->result->getCode()) {
97-
throw new ExecutionException(
98-
$this->result,
99-
sprintf(
100-
"Cannot execute `%s`:\n%s\nCode: %s\nExit text: %s\nError output: %s\nDetails:\n%s",
101-
$command,
102-
implode("\n", array_map(function ($line) { return "$ ${line}"; }, $tmpFileLines)),
103-
$this->result->getCode(),
104-
isset(Process::$exitCodes[$this->result->getCode()]) ? Process::$exitCodes[$this->result->getCode()] : 'Unknown exit code',
105-
$this->result->getError(),
106-
$this->result->getOutput()
107-
),
108-
$process->getExitCode()
109-
);
110-
}
111-
112-
return $this->result;
52+
return $this->scriptExecutor->getResult();
11353
}
11454
}

src/ScriptExecutor.php

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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

Comments
 (0)