1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ahc\Phint\Util; |
4
|
|
|
|
5
|
|
|
use Ahc\Cli\IO\Interactor; |
6
|
|
|
use Symfony\Component\Process\ExecutableFinder; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
abstract class Executable |
10
|
|
|
{ |
11
|
|
|
/** @var bool Last command successful? */ |
12
|
|
|
protected $isSuccessful = true; |
13
|
|
|
|
14
|
|
|
/** @var Interactor */ |
15
|
|
|
protected $io; |
16
|
|
|
|
17
|
|
|
/** @var string The binary executable */ |
18
|
|
|
protected $binary; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
protected $workDir; |
22
|
|
|
|
23
|
|
|
public function __construct($binary = null) |
24
|
|
|
{ |
25
|
|
|
$this->workDir = \getcwd(); |
26
|
|
|
$this->binary = $binary ? '"' . $binary . '"' : $this->binary; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function withWorkDir($workDir = null) |
30
|
|
|
{ |
31
|
|
|
if (!\is_dir($workDir)) { |
32
|
|
|
throw new \InvalidArgumentException('Not a valid working dir: ' . $workDir); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$this->workDir = $workDir; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function successful(): bool |
41
|
|
|
{ |
42
|
|
|
return $this->isSuccessful; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
protected function findBinary($binary) |
46
|
|
|
{ |
47
|
|
|
if (\is_executable($binary)) { |
48
|
|
|
return $binary; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$finder = new ExecutableFinder(); |
52
|
|
|
|
53
|
|
|
return $finder->find($binary) ?: $binary; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Runs the command using underlying binary. |
58
|
|
|
* |
59
|
|
|
* @param string $command |
60
|
|
|
* |
61
|
|
|
* @return string|null The output of command. |
62
|
|
|
*/ |
63
|
|
|
protected function runCommand($command) |
64
|
|
|
{ |
65
|
|
|
$proc = new Process($this->binary . ' ' . $command, $this->workDir, null, null, null); |
66
|
|
|
|
67
|
|
|
$proc->run(); |
68
|
|
|
|
69
|
|
|
$this->isSuccessful = $proc->isSuccessful(); |
70
|
|
|
|
71
|
|
|
return $proc->getOutput(); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|