-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathStorage.php
More file actions
90 lines (71 loc) · 3.01 KB
/
Storage.php
File metadata and controls
90 lines (71 loc) · 3.01 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
<?php
declare (strict_types=1);
namespace MakinaCorpus\DbToolsBundle\Storage;
use MakinaCorpus\DbToolsBundle\Configuration\ConfigurationRegistry;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
/**
* @internal
* Do not use class as it may change in future releases.
*/
class Storage
{
public function __construct(
private ConfigurationRegistry $configReg,
private ?array $filenameStrategies = null,
) {}
public function listBackups(
string $connectionName = 'default',
string $extension = 'sql',
bool $onlyExpired = false,
?string $preserveFile = null
): array {
$config = $this->configReg->getConnectionConfig($connectionName);
$storagePath = \rtrim($config->getStorageDirectory(), '/');
// In order to avoid listing dumps from other connections, we must
// filter files using the connection name infix. When a custom strategy
// is provided, there is no way to reproduce this filtering, it's up to
// the user to give a restricted folder name.
$rootDir = $this->getFilenameStrategy($connectionName)->getRootDir($storagePath, $connectionName);
if (!(new Filesystem())->exists($rootDir)) {
return [];
}
$finder = (new Finder())
->files()
->depth('< 10')
->in($rootDir)
->name('*.' . $extension)
->sortByName()
;
$expirationDate = new \Datetime($config->getBackupExpirationAge());
$list = [];
foreach ($finder as $file) {
$lastModified = new \Datetime(\date("Y-m-d H:i:s", \filemtime((string) $file)));
$age = $lastModified->diff(new \DateTimeImmutable(''));
if (!$onlyExpired || ($lastModified < $expirationDate)) {
if ($preserveFile === null || $file->getFilename() !== $preserveFile) {
$list[] = [$age->format('%a days'), $file];
}
}
}
return $list;
}
public function generateFilename(string $connectionName = 'default', string $extension = 'sql', bool $anonymized = false): string
{
$config = $this->configReg->getConnectionConfig($connectionName);
$storagePath = \rtrim($config->getStorageDirectory(), '/');
$strategy = $this->getFilenameStrategy($connectionName);
$rootDir = $strategy->getRootDir($storagePath, $connectionName);
$filename = \rtrim($rootDir, '/') . '/' . $strategy->generateFilename($connectionName, $extension, $anonymized);
(new Filesystem())->mkdir(\dirname($filename));
return $filename;
}
public function getStoragePath(): string
{
return $this->configReg->getDefaultConfig()->getStorageDirectory();
}
protected function getFilenameStrategy(string $connectionName): FilenameStrategyInterface
{
return $this->filenameStrategies[$connectionName] ?? $this->filenameStrategies['default'] ?? new DefaultFilenameStrategy();
}
}