diff --git a/.gitignore b/.gitignore index 8e540a3..e81b05c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ composer.phar /vendor/ +data .idea .phpunit.result.cache diff --git a/README.md b/README.md index 635cb9e..87d2d0d 100644 --- a/README.md +++ b/README.md @@ -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"; @@ -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, @@ -171,4 +171,4 @@ Please be descriptive in your posts. # ToDo -- [ ] Add keep-within-*** policy support \ No newline at end of file +- [ ] Add keep-within-*** policy support \ No newline at end of file diff --git a/examples/custom_find_and_prune_handler.php b/examples/custom_find_and_prune_handler.php index 969cc9a..fc5ab92 100644 --- a/examples/custom_find_and_prune_handler.php +++ b/examples/custom_find_and_prune_handler.php @@ -1,51 +1,57 @@ 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); \ No newline at end of file