Skip to content

Commit

Permalink
Added pruneAll to FileStore
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Mar 20, 2024
1 parent bec1e71 commit abf68f1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* Added pruneAll to FileStore

## v0.3.2 (2024-02-29)
* Added FileStores

Expand Down
7 changes: 7 additions & 0 deletions src/Stash/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,11 @@ public function deleteMatches(
* @return array<string>
*/
public function getKeys(): array;

/**
* Delete files older than a certain duration across all findable directories
*/
public static function pruneAll(
DateInterval|string|Stringable|int $duration
): int;
}
39 changes: 39 additions & 0 deletions src/Stash/FileStore/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,43 @@ protected function getFile(
$key = $this->createKey($key);
return $this->dir->getFile($key);
}


/**
* Delete files older than a certain duration across all findable directories
*/
public static function pruneAll(
DateInterval|string|Stringable|int $duration
): int {
if (class_exists(Genesis::class)) {
$basePath = Genesis::$hub->getLocalDataPath();
} else {
$basePath = getcwd();
}

$dir = Atlas::dir($basePath . '/fileStore/');

if (!$dir->exists()) {
return 0;
}

$count = 0;

foreach ($dir->scanDirs() as $name => $subDir) {
if (!str_starts_with($name, 'stash@')) {
continue;
}

foreach ($subDir->scanFiles() as $file) {
if ($file->hasChangedIn($duration)) {
continue;
}

$file->delete();
$count++;
}
}

return $count;
}
}

0 comments on commit abf68f1

Please sign in to comment.