Skip to content

Commit a37d558

Browse files
committed
cleanup
1 parent fcb0f34 commit a37d558

3 files changed

Lines changed: 29 additions & 22 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
composer.phar
22
/vendor/
3+
data
34
.idea
45
.phpunit.result.cache

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ $rets->setFindHandler(function (string $targetDir) use ($ftpConnection) {
8080

8181
if (preg_match('/^backup_\w+\.zip$/', $filename)) {
8282
$date = new DateTimeImmutable('now', new DateTimeZone('UTC'));
83-
$date->setTimestamp($time);
83+
$date = $date->setTimestamp($time);
8484

8585
$filepath = "$targetDir/$filename";
8686

@@ -113,7 +113,7 @@ $ret->setTimeHandler(function (string $filepath, bool $isDirectory) {
113113
$day = intval($matches[3]);
114114

115115
$date = new DateTimeImmutable('now', new DateTimeZone('UTC'));
116-
$date->setDate($year, $month, $day)->setTime(0, 0, 0, 0);
116+
$date = $date->setDate($year, $month, $day)->setTime(0, 0, 0, 0);
117117

118118
return new FileInfo(
119119
date: $date,
@@ -171,4 +171,4 @@ Please be descriptive in your posts.
171171

172172
# ToDo
173173

174-
- [ ] Add keep-within-*** policy support
174+
- [ ] Add keep-within-*** policy support
Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,57 @@
11
<?php
22

33
// Example: custom finder function that finds files in ftp by using a custom function instead of scanning the file system
4-
// Example: custom prune function that deletes files from ftp
4+
// Example: custom prune function that deletes files from ftp (use xlight ftp server for quick testing)
55

66
use PhpRetention\FileInfo;
77
use PhpRetention\Retention;
88

99
require_once __DIR__ . "/../vendor/autoload.php";
1010

11-
$ftpConnection = ftp_connect("ftp.example.com");
11+
$ftpConnection = ftp_connect("localhost");
1212
if (!$ftpConnection) {
1313
die('Could not connect to the FTP server');
1414
}
15-
$loginResult = ftp_login($ftpConnection, "ftp-user", "ftp-pass");
15+
$loginResult = ftp_login($ftpConnection, "test", "123456");
1616
if (!$loginResult) {
1717
die('FTP login failed');
1818
}
1919

2020
$ret = new Retention([
21-
'keep-last' => 2
21+
'keep-daily' => 3,
22+
'keep-weekly' => 2
2223
]);
2324
$ret->setFindHandler(function (string $targetDir) use ($ftpConnection) {
2425
$files = [];
2526
$fileList = ftp_mlsd($ftpConnection, $targetDir) ?: [];
2627
foreach ($fileList as $file) {
2728
$filename = $file['name'];
28-
$time = (int) $file['modify'];
29-
30-
if (preg_match('/^backup_\w+\.zip$/', $filename)) {
31-
$date = new DateTimeImmutable('now', new DateTimeZone('UTC'));
32-
$date->setTimestamp($time);
33-
34-
$filepath = "$targetDir/$filename";
35-
36-
$files[] = new FileInfo(
37-
date: $date,
38-
path: $filepath,
39-
isDirectory: false
40-
);
29+
if (preg_match('/^backup\-\w+$/', $filename)) {
30+
if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $file['modify'], $matches)) {
31+
$year = intval($matches[1]);
32+
$month = intval($matches[2]);
33+
$day = intval($matches[3]);
34+
35+
$date = new DateTimeImmutable('now', new DateTimeZone('UTC'));
36+
$date = $date->setDate($year, $month, $day)->setTime(0, 0, 0);
37+
38+
$filepath = "$targetDir/$filename";
39+
$files[] = new FileInfo(
40+
date: $date,
41+
path: $filepath,
42+
isDirectory: false
43+
);
44+
}
4145
}
4246
}
4347

4448
return $files;
4549
});
4650
$ret->setPruneHandler(function (FileInfo $fileInfo) use ($ftpConnection) {
47-
ftp_delete($ftpConnection, $fileInfo->path);
51+
return ftp_delete($ftpConnection, $fileInfo->path);
4852
});
49-
$ret->apply("/path/to/dir");
53+
$keptFiles = $ret->apply("/backup");
54+
55+
print_r($keptFiles);
5056

5157
ftp_close($ftpConnection);

0 commit comments

Comments
 (0)