Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ardabeyazoglu committed Jan 30, 2024
1 parent fcb0f34 commit a37d558
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.phar
/vendor/
data
.idea
.phpunit.result.cache
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ $rets->setFindHandler(function (string $targetDir) use ($ftpConnection) {

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

$filepath = "$targetDir/$filename";

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

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

return new FileInfo(
date: $date,
Expand Down Expand Up @@ -171,4 +171,4 @@ Please be descriptive in your posts.

# ToDo

- [ ] Add keep-within-*** policy support
- [ ] Add keep-within-*** policy support
44 changes: 25 additions & 19 deletions examples/custom_find_and_prune_handler.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,57 @@
<?php

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

use PhpRetention\FileInfo;
use PhpRetention\Retention;

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

$ftpConnection = ftp_connect("ftp.example.com");
$ftpConnection = ftp_connect("localhost");
if (!$ftpConnection) {
die('Could not connect to the FTP server');
}
$loginResult = ftp_login($ftpConnection, "ftp-user", "ftp-pass");
$loginResult = ftp_login($ftpConnection, "test", "123456");
if (!$loginResult) {
die('FTP login failed');
}

$ret = new Retention([
'keep-last' => 2
'keep-daily' => 3,
'keep-weekly' => 2
]);
$ret->setFindHandler(function (string $targetDir) use ($ftpConnection) {
$files = [];
$fileList = ftp_mlsd($ftpConnection, $targetDir) ?: [];
foreach ($fileList as $file) {
$filename = $file['name'];
$time = (int) $file['modify'];

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

$filepath = "$targetDir/$filename";

$files[] = new FileInfo(
date: $date,
path: $filepath,
isDirectory: false
);
if (preg_match('/^backup\-\w+$/', $filename)) {
if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $file['modify'], $matches)) {
$year = intval($matches[1]);
$month = intval($matches[2]);
$day = intval($matches[3]);

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

$filepath = "$targetDir/$filename";
$files[] = new FileInfo(
date: $date,
path: $filepath,
isDirectory: false
);
}
}
}

return $files;
});
$ret->setPruneHandler(function (FileInfo $fileInfo) use ($ftpConnection) {
ftp_delete($ftpConnection, $fileInfo->path);
return ftp_delete($ftpConnection, $fileInfo->path);
});
$ret->apply("/path/to/dir");
$keptFiles = $ret->apply("/backup");

print_r($keptFiles);

ftp_close($ftpConnection);

0 comments on commit a37d558

Please sign in to comment.