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\ProcessorInterface; |
19
|
|
|
use RunOpenCode\Backup\Event\BackupEvents; |
20
|
|
|
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait; |
21
|
|
|
use RunOpenCode\Backup\Exception\ProcessorException; |
22
|
|
|
use RunOpenCode\Backup\Utils\Filename; |
23
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
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 |
34
|
|
|
{ |
35
|
|
|
use EventDispatcherAwareTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $filename; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $flags; |
46
|
|
|
|
47
|
4 |
|
public function __construct($flags = '-czvf', $filename = 'archive.tar.gz') |
48
|
|
|
{ |
49
|
4 |
|
$this->filename = $filename; |
50
|
4 |
|
$this->flags = $flags; |
51
|
4 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
4 |
|
public function process(array $files) |
57
|
|
|
{ |
58
|
4 |
|
$tmpFile = Filename::temporaryFilename($this->filename); |
59
|
|
|
|
60
|
4 |
|
$processBuilder = new ProcessBuilder(); |
61
|
|
|
|
62
|
4 |
|
$processBuilder->add('tar'); |
63
|
|
|
|
64
|
4 |
|
if (!is_null($this->flags)) { |
65
|
4 |
|
$processBuilder->add($this->flags); |
66
|
2 |
|
} |
67
|
|
|
|
68
|
4 |
|
$processBuilder->add($tmpFile); |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var FileInterface $backup |
72
|
|
|
*/ |
73
|
4 |
|
foreach ($files as $backup) { |
74
|
2 |
|
$processBuilder->add($backup->getPath()); |
75
|
2 |
|
} |
76
|
|
|
|
77
|
4 |
|
$process = $processBuilder->getProcess(); |
78
|
|
|
|
79
|
4 |
|
$process->run(); |
80
|
|
|
|
81
|
4 |
|
if (!$process->isSuccessful()) { |
82
|
2 |
|
throw new ProcessorException(sprintf('Unable to create gzip archive, reason: "%s".', $process->getErrorOutput())); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
$this->getEventDispatcher()->addListener(BackupEvents::TERMINATE, function() use ($tmpFile) { |
86
|
2 |
|
unlink($tmpFile); |
87
|
2 |
|
}); |
88
|
|
|
|
89
|
2 |
|
return array(File::fromLocal($tmpFile, dirname($tmpFile))); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|