|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) 2015 RunOpenCode |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
|
11
|
|
|
* view CREDITS file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
namespace RunOpenCode\Backup\Processor; |
|
14
|
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Backup\File; |
|
16
|
|
|
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface; |
|
17
|
|
|
use RunOpenCode\Backup\Contract\FileInterface; |
|
18
|
|
|
use RunOpenCode\Backup\Contract\LoggerAwareInterface; |
|
19
|
|
|
use RunOpenCode\Backup\Contract\ProcessorInterface; |
|
20
|
|
|
use RunOpenCode\Backup\Event\BackupEvent; |
|
21
|
|
|
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait; |
|
22
|
|
|
use RunOpenCode\Backup\Exception\ProcessorException; |
|
23
|
|
|
use RunOpenCode\Backup\Log\LoggerAwareTrait; |
|
24
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class GzipArchiveProcessor |
|
28
|
|
|
* |
|
29
|
|
|
* Gzip archive processor combines all backup file into single gz compressed archive. |
|
30
|
|
|
* |
|
31
|
|
|
* @package RunOpenCode\Backup\Processor |
|
32
|
|
|
*/ |
|
33
|
|
|
class GzipArchiveProcessor implements ProcessorInterface, EventDispatcherAwareInterface, LoggerAwareInterface |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
use EventDispatcherAwareTrait; |
|
37
|
|
|
use LoggerAwareTrait; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $filename; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $flags; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct($flags = '-czvf', $filename = 'archive.tar.gz') |
|
50
|
|
|
{ |
|
51
|
|
|
$this->filename = $filename; |
|
52
|
|
|
$this->flags = $flags; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function process(array $files) |
|
59
|
|
|
{ |
|
60
|
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'gzip-archive-processor'); |
|
61
|
|
|
|
|
62
|
|
|
$processBuilder = new ProcessBuilder(); |
|
63
|
|
|
|
|
64
|
|
|
$processBuilder->add('tar'); |
|
65
|
|
|
|
|
66
|
|
|
if (!is_null($this->flags)) { |
|
67
|
|
|
$processBuilder->add($this->flags); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$processBuilder->add($tmpFile); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var FileInterface $backup |
|
74
|
|
|
*/ |
|
75
|
|
|
foreach ($files as $backup) { |
|
76
|
|
|
$processBuilder->add($backup->getPath()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$process = $processBuilder->getProcess(); |
|
80
|
|
|
|
|
81
|
|
|
$process->run(); |
|
82
|
|
|
|
|
83
|
|
|
if (!$process->isSuccessful()) { |
|
84
|
|
|
$this->getLogger()->error('Unable to create gzip archive.'); |
|
85
|
|
|
throw new ProcessorException(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$this->getEventDispatcher()->addListener(BackupEvent::TERMINATE, function() use ($tmpFile) { |
|
89
|
|
|
@unlink($tmpFile); |
|
|
|
|
|
|
90
|
|
|
}); |
|
91
|
|
|
|
|
92
|
|
|
return array(File::fromLocal($tmpFile, dirname($tmpFile), $this->filename)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: