|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of SebastianFeldmann\Cli. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Cli\Command\Runner; |
|
13
|
|
|
|
|
14
|
|
|
use RuntimeException; |
|
15
|
|
|
use SebastianFeldmann\Cli\Command; |
|
16
|
|
|
use SebastianFeldmann\Cli\Command\Runner; |
|
17
|
|
|
use SebastianFeldmann\Cli\Command\OutputFormatter; |
|
18
|
|
|
use SebastianFeldmann\Cli\Processor; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class Simple |
|
22
|
|
|
* |
|
23
|
|
|
* @package SebastianFeldmann\Cli |
|
24
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
25
|
|
|
* @link https://github.com/sebastianfeldmann/cli |
|
26
|
|
|
* @since Class available since Release 0.9.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
class Simple implements Runner |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Class handling system calls. |
|
32
|
|
|
* |
|
33
|
|
|
* @var \SebastianFeldmann\Cli\Processor |
|
34
|
|
|
*/ |
|
35
|
|
|
private $processor; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Exec constructor. |
|
39
|
|
|
* |
|
40
|
2 |
|
* @param \SebastianFeldmann\Cli\Processor $processor |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function __construct(Processor $processor = null) |
|
43
|
2 |
|
{ |
|
44
|
|
|
$this->processor = $processor !== null ? $processor : new Processor\ProcOpen(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Execute a cli command. |
|
49
|
|
|
* |
|
50
|
|
|
* @param \SebastianFeldmann\Cli\Command $command |
|
51
|
|
|
* @param \SebastianFeldmann\Cli\Command\OutputFormatter $formatter |
|
52
|
2 |
|
* @return \SebastianFeldmann\Cli\Command\Runner\Result |
|
53
|
|
|
*/ |
|
54
|
2 |
|
public function run(Command $command, OutputFormatter $formatter = null): Result |
|
55
|
|
|
{ |
|
56
|
2 |
|
$cmd = $this->processor->run($command->getCommand(), $command->getAcceptableExitCodes()); |
|
57
|
1 |
|
|
|
58
|
1 |
|
if (!$cmd->isSuccessful()) { |
|
59
|
1 |
|
throw new RuntimeException( |
|
60
|
1 |
|
'Command failed:' . PHP_EOL |
|
61
|
1 |
|
. ' exit-code: ' . $cmd->getCode() . PHP_EOL |
|
62
|
|
|
. ' message: ' . $cmd->getStdErr() . PHP_EOL, |
|
63
|
|
|
$cmd->getCode() |
|
64
|
|
|
); |
|
65
|
1 |
|
} |
|
66
|
1 |
|
|
|
67
|
|
|
$formatted = $formatter !== null ? $formatter->format($cmd->getStdOutAsArray()) : []; |
|
68
|
1 |
|
$result = new Result($cmd, $formatted); |
|
69
|
|
|
|
|
70
|
|
|
return $result; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|