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
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $path |
45
|
|
|
*/ |
46
|
|
|
public function __construct($path = null) { |
47
|
|
|
$this->binary = Cli::detectCmdLocation($this->cmd, $path, Cli::getCommandLocations($this->cmd)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Process setter, mostly for test purposes. |
52
|
|
|
* |
53
|
123 |
|
* @param \phpbu\App\Cli\Process $process |
54
|
123 |
|
*/ |
55
|
123 |
|
public function setProcess(Process $process) |
56
|
|
|
{ |
57
|
|
|
$this->process = $process; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Returns the Process for this command. |
62
|
2 |
|
* |
63
|
|
|
* @return \phpbu\App\Cli\Process |
64
|
2 |
|
*/ |
65
|
2 |
|
public function getProcess() |
66
|
|
|
{ |
67
|
|
|
if ($this->process == null) { |
68
|
|
|
$this->process = $this->createProcess(); |
69
|
|
|
} |
70
|
|
|
return $this->process; |
71
|
|
|
} |
72
|
111 |
|
|
73
|
|
|
/** |
74
|
111 |
|
* Subclass Process generator. |
75
|
109 |
|
* |
76
|
93 |
|
* @return \phpbu\App\Cli\Process |
77
|
95 |
|
*/ |
78
|
|
|
abstract protected function createProcess(); |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Executes the cli commands. |
82
|
|
|
* |
83
|
|
|
* @return \phpbu\App\Cli\Result |
84
|
|
|
* @throws \phpbu\App\Exception |
85
|
|
|
*/ |
86
|
|
|
public function run() |
87
|
|
|
{ |
88
|
|
|
$process = $this->getProcess(); |
89
|
|
|
$res = $process->run(); |
90
|
|
|
|
91
|
|
|
if (0 !== $res->getCode() && $process->isOutputRedirected()) { |
92
|
|
|
// remove file with errors |
93
|
25 |
|
$this->unlinkErrorFile($process->getRedirectPath()); |
94
|
|
|
} |
95
|
25 |
|
|
96
|
25 |
|
return $res; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Return the command line to execute. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
* @throws \phpbu\App\Exception |
104
|
|
|
*/ |
105
|
5 |
|
public function getCommandLine() |
106
|
|
|
{ |
107
|
5 |
|
return $this->getProcess()->getCommandLine(); |
108
|
2 |
|
} |
109
|
|
|
|
110
|
2 |
|
/** |
111
|
|
|
* Remove file if it exists. |
112
|
1 |
|
* |
113
|
1 |
|
* @param string $file |
114
|
|
|
*/ |
115
|
2 |
|
public function unlinkErrorFile($file) |
116
|
|
|
{ |
117
|
|
|
if (file_exists($file) && !is_dir($file)) { |
118
|
|
|
unlink($file); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|