Skip to content

Commit 25cf8a4

Browse files
mariavilarofreekmurze
authored andcommitted
Add support for deleting empty subdirectories (spatie#25)
1 parent cff7295 commit 25cf8a4

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ return [
6666

6767
Specify the directories that need cleaning in the config file.
6868

69-
When running the console command `clean:directories` all files in the specified directories older then `deleteAllOlderThanMinutes` will be deleted.
69+
When running the console command `clean:directories` all files in the specified directories older then `deleteAllOlderThanMinutes` will be deleted. Empty subdirectories will also be deleted.
7070

7171
This command can be scheduled in Laravel's console kernel.
7272

src/DirectoryCleaner.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ public function deleteFilesOlderThanMinutes(int $minutes) : Collection
4444
});
4545
}
4646

47+
public function deleteEmptySubdirectories() : Collection
48+
{
49+
return collect($this->filesystem->directories($this->directory))
50+
->filter(function ($directory) {
51+
return ! $this->filesystem->allFiles($directory);
52+
})
53+
->each(function ($directory) {
54+
$this->filesystem->deleteDirectory($directory);
55+
});
56+
}
57+
4758
protected function policy() : CleanupPolicy
4859
{
4960
return resolve(config(

src/DirectoryCleanupCommand.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public function handle()
2020
collect($directories)->each(function ($config, $directory) {
2121
if (File::isDirectory($directory)) {
2222
$this->deleteFilesIfOlderThanMinutes($directory, $config['deleteAllOlderThanMinutes']);
23+
$this->deleteEmptySubdirectories($directory);
2324
}
2425
});
2526

@@ -34,4 +35,13 @@ protected function deleteFilesIfOlderThanMinutes(string $directory, int $minutes
3435

3536
$this->info("Deleted {$deletedFiles->count()} file(s) from {$directory}.");
3637
}
38+
39+
protected function deleteEmptySubdirectories(string $directory)
40+
{
41+
$deletedSubdirectories = app(DirectoryCleaner::class)
42+
->setDirectory($directory)
43+
->deleteEmptySubdirectories();
44+
45+
$this->info("Deleted {$deletedSubdirectories->count()} directory(ies) from {$directory}.");
46+
}
3747
}

tests/DirectoryCleanupTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,37 @@ public function it_doesnt_fail_if_a_configured_dir_doesnt_exist()
126126
$this->assertFileNotExists("{$existingDirectory}/5MinutesOld.txt");
127127
}
128128

129+
/** @test */
130+
public function it_can_delete_empty_subdirectories()
131+
{
132+
$directories[$this->getTempDirectory('deleteEmptySubdirs', true)] = [
133+
'deleteAllOlderThanMinutes' => 3,
134+
'deleteEmptySubdirectories' => true,
135+
];
136+
137+
$this->app['config']->set('laravel-directory-cleanup', compact('directories'));
138+
139+
foreach ($directories as $directory => $config) {
140+
$this->createDirectory("{$directory}/emptyDir");
141+
$this->createFile("{$directory}/emptyDir/5MinutesOld.txt", 5);
142+
$this->createDirectory("{$directory}/notEmptyDir");
143+
$this->createFile("{$directory}/notEmptyDir/1MinutesOld.txt", 1);
144+
}
145+
146+
$this->artisan('clean:directories');
147+
148+
foreach ($directories as $directory => $config) {
149+
$this->assertDirectoryExists("{$directory}/notEmptyDir");
150+
$this->assertDirectoryNotExists("{$directory}/emptyDir");
151+
}
152+
}
153+
129154
protected function createFile(string $fileName, int $ageInMinutes)
130155
{
131156
touch($fileName, Carbon::now()->subMinutes($ageInMinutes)->subSeconds(5)->timestamp);
132157
}
133158

134-
protected function createDirectory(string $fileName, int $ageInMinutes)
159+
protected function createDirectory(string $fileName)
135160
{
136161
mkdir($fileName);
137162
}

0 commit comments

Comments
 (0)