Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 8 additions & 68 deletions src/BashScriptExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,24 @@

namespace Keradus\CliExecutor;

use Symfony\Component\Process\Process;

/**
* @deprecated, use `\Keradus\CliExecutor\ScriptExecutor` instead
*/
final class BashScriptExecutor
{
/**
* @var int
*/
private static $tmpCounter = 0;

/**
* @var string[]
*/
private $scriptParts;

/**
* @var string
*/
private $cwd;

/**
* @var ?CliResult
* @var ScriptExecutor
*/
private $result;

/**
* @var ?string
*/
private $tmpFilePath;
private $scriptExecutor;

/**
* @param string[] $scriptParts
* @param string $cwd
*/
public function __construct($scriptParts, $cwd)
{
$this->scriptParts = $scriptParts;
$this->cwd = $cwd;
}

public function __destruct()
{
if (null !== $this->tmpFilePath) {
@unlink($this->tmpFilePath);
}
@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', ''));
}

/**
Expand All @@ -75,40 +49,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();
}
}
124 changes: 124 additions & 0 deletions src/ScriptExecutor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/*
* This file is part of CLI Executor.
*
* (c) Dariusz Rumiński <[email protected]>
*
* 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;
}
}