1
|
|
|
<?php |
2
|
|
|
namespace phpbu\App\Cli\Executable; |
3
|
|
|
|
4
|
|
|
use phpbu\App\Cli\Process; |
5
|
|
|
use phpbu\App\Util\Cli; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Execute Binary |
9
|
|
|
* |
10
|
|
|
* @package phpbu |
11
|
|
|
* @subpackage Backup |
12
|
|
|
* @author Sebastian Feldmann <[email protected]> |
13
|
|
|
* @copyright Sebastian Feldmann <[email protected]> |
14
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
15
|
|
|
* @link http://phpbu.de/ |
16
|
|
|
* @since Class available since Release 2.1.0 |
17
|
|
|
*/ |
18
|
|
|
abstract class Abstraction |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Command name. |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $cmd; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Absolute path to command. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $binary; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Command to execute |
36
|
|
|
* |
37
|
|
|
* @var \phpbu\App\Cli\Process |
38
|
|
|
*/ |
39
|
|
|
protected $process; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Setup binary. |
43
|
|
|
* |
44
|
|
|
* @param string $cmd |
45
|
|
|
* @param string $path |
46
|
|
|
*/ |
47
|
|
|
protected function setup($cmd, $path = null) { |
48
|
|
|
$this->cmd = $cmd; |
49
|
|
|
$this->binary = Cli::detectCmdLocation($cmd, $path, Cli::getCommandLocations($this->cmd)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
123 |
|
* Process setter, mostly for test purposes. |
54
|
123 |
|
* |
55
|
123 |
|
* @param \phpbu\App\Cli\Process $process |
56
|
|
|
*/ |
57
|
|
|
public function setProcess(Process $process) |
58
|
|
|
{ |
59
|
|
|
$this->process = $process; |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
/** |
63
|
|
|
* Returns the Process for this command. |
64
|
2 |
|
* |
65
|
2 |
|
* @return \phpbu\App\Cli\Process |
66
|
|
|
*/ |
67
|
|
|
public function getProcess() |
68
|
|
|
{ |
69
|
|
|
if ($this->process == null) { |
70
|
|
|
$this->process = $this->createProcess(); |
71
|
|
|
} |
72
|
111 |
|
return $this->process; |
73
|
|
|
} |
74
|
111 |
|
|
75
|
109 |
|
/** |
76
|
93 |
|
* Subclass Process generator. |
77
|
95 |
|
* |
78
|
|
|
* @return \phpbu\App\Cli\Process |
79
|
|
|
*/ |
80
|
|
|
abstract protected function createProcess(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Executes the cli commands. |
84
|
|
|
* |
85
|
|
|
* @return \phpbu\App\Cli\Result |
86
|
|
|
* @throws \phpbu\App\Exception |
87
|
|
|
*/ |
88
|
|
|
public function run() |
89
|
|
|
{ |
90
|
|
|
$process = $this->getProcess(); |
91
|
|
|
$res = $process->run(); |
92
|
|
|
|
93
|
25 |
|
if (0 !== $res->getCode() && $process->isOutputRedirected()) { |
94
|
|
|
// remove file with errors |
95
|
25 |
|
$this->unlinkErrorFile($process->getRedirectPath()); |
96
|
25 |
|
} |
97
|
|
|
|
98
|
|
|
return $res; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Return the command line to execute. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
5 |
|
* @throws \phpbu\App\Exception |
106
|
|
|
*/ |
107
|
5 |
|
public function getCommandLine() |
108
|
2 |
|
{ |
109
|
|
|
return $this->getProcess()->getCommandLine(); |
110
|
2 |
|
} |
111
|
|
|
|
112
|
1 |
|
/** |
113
|
1 |
|
* Return the command with masked passwords or keys. |
114
|
|
|
* |
115
|
2 |
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getCommandLinePrintable() |
118
|
|
|
{ |
119
|
|
|
return $this->getCommandLine(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Remove file if it exists. |
124
|
106 |
|
* |
125
|
|
|
* @param string $file |
126
|
106 |
|
*/ |
127
|
|
|
public function unlinkErrorFile($file) |
128
|
|
|
{ |
129
|
|
|
if (file_exists($file) && !is_dir($file)) { |
130
|
|
|
unlink($file); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|