Skip to content

Commit ebe4134

Browse files
Moved some code from 'Exec' to new 'Source\Cli' class
1 parent c35fc6a commit ebe4134

File tree

4 files changed

+95
-104
lines changed

4 files changed

+95
-104
lines changed

src/Backup/Cli/Exec.php

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -25,81 +25,18 @@ class Exec
2525
private $commands = array();
2626

2727
/**
28-
* Backup target
29-
*
30-
* @var \phpbu\Backup\Target
31-
*/
32-
private $target;
33-
34-
/**
35-
* Do we use the commands output for compression
36-
*
37-
* @var boolean
38-
*/
39-
private $compressOutput = true;
40-
41-
/**
42-
* Target settter
43-
*
44-
* @param \phpbu\Backup\Target $target
45-
*/
46-
public function setTarget(Target $target)
47-
{
48-
$this->target = $target;
49-
}
50-
51-
/**
52-
* OutputCompression setter
53-
*
54-
* @param boolean $bool
55-
*/
56-
public function setOutputCompression($bool)
57-
{
58-
$this->compressOutput = $bool;
59-
}
60-
61-
/**
28+
* Executes the commands
6229
*
6330
* @throws \phpbu\App\Exception
6431
* @return \phpbu\Cli\Result
6532
*/
66-
public function execute()
33+
public function execute($redirect = null)
6734
{
68-
$cmd = $this->getExec();
35+
$cmd = $this->getExec() . ( $redirect ? ' > ' . $redirect : '' );
6936
$output = array();
7037
$code = 0;
7138
$old = error_reporting(0);
7239
exec($cmd, $output, $code);
73-
74-
if ($code == 0) {
75-
// run the compressor command
76-
if ($this->compressOutput && $this->target->shouldBeCompressed()) {
77-
$compressorCode = 0;
78-
$compressorOutput = array();
79-
exec(
80-
$this->target->getCompressor()->getCommand()
81-
. ' -f '
82-
. $this->target->getPathname(),
83-
$compressorOutput,
84-
$compressorCode
85-
);
86-
87-
if ($compressorCode !== 0) {
88-
// remove compressed file with errors
89-
if ($this->target->fileExists()) {
90-
$this->target->unlink();
91-
}
92-
}
93-
94-
$code += $compressorCode;
95-
$output = array_merge($output, $compressorOutput);
96-
}
97-
} else {
98-
// remove file with errors
99-
if ($this->target->fileExists(false)) {
100-
$this->target->unlink(false);
101-
}
102-
}
10340
error_reporting($old);
10441

10542
return new Result($cmd, $code, $output);
@@ -119,9 +56,6 @@ public function getExec()
11956
}
12057
$cmd = $amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0];
12158

122-
if ($this->compressOutput) {
123-
$cmd .= ' > ' . $this->target->getPathname();
124-
}
12559
return $cmd;
12660
}
12761

src/Backup/Source/Cli.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace phpbu\Backup\Source;
3+
4+
use phpbu\Backup\Cli\Exec;
5+
use phpbu\Backup\Cli\Result;
6+
use phpbu\Backup\Target;
7+
8+
/**
9+
* Cli Runner
10+
*
11+
* @package phpbu
12+
* @subpackage Backup
13+
* @author Sebastian Feldmann <sebastian@phpbu.de>
14+
* @copyright 2014 Sebastian Feldmann <sebastian@phpbu.de>
15+
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
16+
* @link http://www.phpbu.de/
17+
* @since Class available since Release 1.0.3
18+
*/
19+
abstract class Cli
20+
{
21+
/**
22+
* Executes the cli commands and handles compression
23+
*
24+
* @throws \phpbu\App\Exception
25+
* @return \phpbu\Cli\Result
26+
*/
27+
protected function execute(Exec $exec, Target $target, $compressOutput = true)
28+
{
29+
$res = $exec->execute($compressOutput ? $target->getPathname() : null);
30+
$code = $res->getCode();
31+
$cmd = $res->getCmd();
32+
$output = $res->getOutput();
33+
34+
if ($code == 0) {
35+
// run the compressor command
36+
if ($compressOutput && $target->shouldBeCompressed()) {
37+
$compressorCode = 0;
38+
$compressorOutput = array();
39+
$compressorCmd = $target->getCompressor()->getCommand();
40+
$old = error_reporting(0);
41+
exec(
42+
$compressorCmd
43+
. ' -f '
44+
. $target->getPathname(),
45+
$compressorOutput,
46+
$compressorCode
47+
);
48+
error_reporting($old);
49+
50+
if ($compressorCode !== 0) {
51+
// remove compressed file with errors
52+
if ($target->fileExists()) {
53+
$target->unlink();
54+
}
55+
}
56+
57+
$cmd .= PHP_EOL . $compressorCmd;
58+
$code += $compressorCode;
59+
$output = array_merge($output, $compressorOutput);
60+
}
61+
} else {
62+
// remove file with errors
63+
if ($target->fileExists(false)) {
64+
$target->unlink(false);
65+
}
66+
}
67+
68+
return new Result($cmd, $code, $output);
69+
}
70+
}

src/Backup/Source/Mysqldump.php

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
use phpbu\App\Exception;
55
use phpbu\App\Result;
6-
use phpbu\Backup\Cli;
6+
use phpbu\Backup\Cli\Cmd;
7+
use phpbu\Backup\Cli\Exec;
78
use phpbu\Backup\Source;
89
use phpbu\Backup\Target;
910
use phpbu\Util;
@@ -19,15 +20,8 @@
1920
* @link http://www.phpbu.de/
2021
* @since Class available since Release 1.0.0
2122
*/
22-
class Mysqldump implements Source
23+
class Mysqldump extends Cli implements Source
2324
{
24-
/**
25-
* Executor to run the mysqldump shell commands.
26-
*
27-
* @var \phpbu\Cli\Exec
28-
*/
29-
private $exec;
30-
3125
/**
3226
* Configuration
3327
*
@@ -55,12 +49,12 @@ public function setup(array $conf = array())
5549
*/
5650
public function backup(Target $target, Result $result)
5751
{
58-
$host = 'localhost';
59-
$user = $_SERVER['USER'];
60-
$password = null;
61-
$datbases = array();
62-
$this->exec = new Cli\Exec();
63-
$this->exec->setTarget($target);
52+
$host = 'localhost';
53+
$user = $_SERVER['USER'];
54+
$password = null;
55+
$datbases = array();
56+
$exec = new Exec();
57+
6458

6559
$path = isset($this->conf['pathToMysqldump']) ? $this->conf['pathToMysqldump'] : null;
6660
$mysqldump = Util\Cli::detectCmdLocation(
@@ -72,8 +66,8 @@ public function backup(Target $target, Result $result)
7266
)
7367
);
7468

75-
$cmd = new Cli\Cmd($mysqldump);
76-
$this->exec->addCommand($cmd);
69+
$cmd = new Cmd($mysqldump);
70+
$exec->addCommand($cmd);
7771

7872
// no std error unless it is activated
7973
if (empty($this->conf['showStdErr']) || !Util\String::toBoolean($this->conf['showStdErr'], false)) {
@@ -140,10 +134,10 @@ public function backup(Target $target, Result $result)
140134
$cmd2->addOption('--skip-add-drop-table');
141135
$cmd2->addOption('--no-create-db');
142136
$cmd2->addOption('--no-create-info');
143-
$this->exec->addCommand($cmd2);
137+
$exec->addCommand($cmd2);
144138
}
145139
}
146-
$r = $this->exec->execute();
140+
$r = $this->execute($exec, $target);
147141

148142
$result->debug($r->getCmd());
149143

src/Backup/Source/Tar.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
11
<?php
22
namespace phpbu\Backup\Source;
33

4+
use phpbu\App\Exception;
45
use phpbu\App\Result;
5-
use phpbu\Backup\Cli;
6+
use phpbu\Backup\Cli\Cmd;
7+
use phpbu\Backup\Cli\Exec;
68
use phpbu\Backup\Source;
79
use phpbu\Backup\Target;
810
use phpbu\Util;
911

10-
class Tar implements Source
12+
class Tar extends Cli implements Source
1113
{
12-
/**
13-
* Executor to run the tar shell command.
14-
*
15-
* @var \phpbu\Cli\Exec
16-
*/
17-
private $exec;
18-
1914
/**
2015
* Configuration
2116
*
@@ -36,17 +31,15 @@ public function setup(array $conf = array())
3631
}
3732

3833
/**
34+
* Executes the backup
3935
*
4036
* @param \phpbu\Backup\Target $target
4137
* @param \phpbu\App\Result $result
4238
* @return \phpbu\App\Result
4339
*/
4440
public function backup(Target $target, Result $result)
4541
{
46-
$this->exec = new Cli\Exec();
47-
$this->exec->setTarget($target);
48-
$this->exec->setOutputCompression(false);
49-
42+
$exec = new Exec();
5043
$compressOption = '';
5144
$allowedCompressors = array(
5245
'bzip2' => 'j',
@@ -67,7 +60,7 @@ public function backup(Target $target, Result $result)
6760

6861
$path = isset($this->conf['pathToTar']) ? $this->conf['pathToTar'] : null;
6962
$tar = Util\Cli::detectCmdLocation('tar', $path);
70-
$cmd = new Cli\Cmd($tar);
63+
$cmd = new Cmd($tar);
7164

7265
// no std error unless it is activated
7366
if (empty($this->conf['showStdErr']) || !Util\String::toBoolean($this->conf['showStdErr'], false)) {
@@ -81,9 +74,9 @@ public function backup(Target $target, Result $result)
8174
$this->conf['path'],
8275
)
8376
);
84-
$this->exec->addCommand($cmd);
77+
$exec->addCommand($cmd);
8578

86-
$r = $this->exec->execute();
79+
$r = $this->execute($exec, $target, false);
8780

8881
$result->debug($r->getCmd());
8982

0 commit comments

Comments
 (0)