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\Process\ProcessBuilder; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class ZipArchiveProcessor |
27
|
|
|
* |
28
|
|
|
* Zip archive processor combines all backup file into single zip compressed archive. |
29
|
|
|
* |
30
|
|
|
* @package RunOpenCode\Backup\Processor |
31
|
|
|
*/ |
32
|
|
|
class ZipArchiveProcessor implements ProcessorInterface, EventDispatcherAwareInterface |
33
|
|
|
{ |
34
|
|
|
use EventDispatcherAwareTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $filename; |
40
|
|
|
|
41
|
6 |
|
public function __construct($filename = 'archive.zip') |
42
|
|
|
{ |
43
|
6 |
|
$this->filename = $filename; |
44
|
6 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
6 |
|
public function process(array $files) |
50
|
|
|
{ |
51
|
6 |
|
$tmpFile = Filename::temporaryFilename($this->filename); |
52
|
|
|
|
53
|
6 |
|
$processBuilder = new ProcessBuilder(); |
54
|
|
|
|
55
|
|
|
$processBuilder |
56
|
6 |
|
->add('zip') |
57
|
6 |
|
->add($tmpFile); |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var FileInterface $backup |
61
|
|
|
*/ |
62
|
6 |
|
foreach ($files as $backup) { |
63
|
4 |
|
$processBuilder->add($backup->getPath()); |
64
|
3 |
|
} |
65
|
|
|
|
66
|
6 |
|
$process = $processBuilder->getProcess(); |
67
|
|
|
|
68
|
6 |
|
$process->run(); |
69
|
|
|
|
70
|
6 |
|
if (!$process->isSuccessful()) { |
71
|
2 |
|
throw new ProcessorException(sprintf('Unable to create zip archive, reason: "%s".', $process->getErrorOutput())); |
72
|
|
|
} |
73
|
|
|
|
74
|
4 |
|
$this->getEventDispatcher()->addListener(BackupEvents::TERMINATE, function() use ($tmpFile) { |
75
|
4 |
|
unlink($tmpFile); |
76
|
4 |
|
}); |
77
|
|
|
|
78
|
4 |
|
return array(File::fromLocal($tmpFile, dirname($tmpFile))); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|