This repository was archived by the owner on Nov 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathManager.php
More file actions
157 lines (140 loc) · 3.78 KB
/
Manager.php
File metadata and controls
157 lines (140 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
namespace FDevs\Backup;
use FDevs\Backup\Compress\CompressionInterface;
use FDevs\Backup\Exception\ArchiveException;
use FDevs\Backup\Exception\DataProviderException;
use FDevs\Backup\Exception\ExtractException;
use FDevs\Backup\Source\DataProviderInterface;
use FDevs\Backup\Filesystem\FilesystemInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\OptionsResolver\OptionsResolver;
class Manager
{
/**
* @var DataProviderInterface
*/
private $source;
/**
* @var FilesystemInterface
*/
private $filesystem;
/**
* @var CompressionInterface|null
*/
private $compressor;
/**
* @var Filesystem
*/
private $local;
/**
* @var OptionsResolver
*/
private $resolver;
/**
* Manager constructor.
*
* @param DataProviderInterface $source
* @param FilesystemInterface $filesystem
* @param CompressionInterface|null $compressor
*/
public function __construct(DataProviderInterface $source, FilesystemInterface $filesystem, CompressionInterface $compressor = null)
{
$this->source = $source;
$this->filesystem = $filesystem;
$this->compressor = $compressor;
$this->local = new Filesystem();
$this->resolver = new OptionsResolver();
$this->source->configureOption($this->resolver);
}
/**
* @param array $options
*
* @throws DataProviderException
* @throws ArchiveException
*
* @return string
*/
public function dump(array $options = [])
{
try {
$options = $this->resolver->resolve($options);
$source = $this->source->dump($options);
$file = $this->pack($source);
$key = $this->filesystem->upload($file);
} finally {
if (isset($file) && $this->local->exists($file)) {
$this->local->remove($file);
}
if (isset($source) && $this->local->exists($source)) {
$this->local->remove($source);
}
}
return $key;
}
/**
* @param string $key
* @param array $options
*
* @throws DataProviderException
* @throws ExtractException
*
* @return bool
*/
public function restore($key, array $options = [])
{
try {
$options = $this->resolver->resolve($options);
$file = $this->filesystem->download($key);
$source = $this->unpack($file);
$status = $this->source->restore($source, $options);
} finally {
if (isset($file) && $this->local->exists($file)) {
$this->local->remove($file);
}
if (isset($source) && $this->local->exists($source)) {
$this->local->remove($source);
}
}
return $status;
}
/**
* @return \Iterator
*/
public function keyList()
{
return $this->filesystem->keyList();
}
/**
* @param string $source
*
* @throws ArchiveException
*
* @return string
*/
private function pack($source)
{
$target = null;
if ($this->compressor) {
$target = $source . $this->compressor->getExtension();
$this->compressor->pack($source, $target);
}
return $target ?: $source;
}
/**
* @param string $target
*
* @throws ExtractException
*
* @return string
*/
private function unpack($target)
{
$source = null;
if ($this->compressor) {
$source = dirname($target) . DIRECTORY_SEPARATOR . uniqid(mt_rand());
$this->local->mkdir($source);
$this->compressor->unpack($target, $source);
}
return $source ?: $target;
}
}