Skip to content

Commit cf482f1

Browse files
stefanzweifelfreekmurze
authored andcommitted
Exclude certain files from being deleted (spatie#20)
* Add test * Reject files which are in the ignoredFiles array * Update default config * Update method signatures to make things work 🙁 * Update Config * Update README * Add DefaultCleanupPolicy * Update Implementation and Test * Rename DefaultCleanupPolicy to Basic * Rename Method in Policy * Revert README * Add mockery/mockery as a dev dependency * Fix StyleCI * Update README with usage docs * Revert original changes
1 parent 2e70166 commit cf482f1

8 files changed

+116
-1
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ This is the content of the published config file `laravel-directory-cleanup`
3535
```
3636
return [
3737
38+
/*
39+
* A policy will determine if a given file should be deleted. This is the perfect
40+
* place to apply custom rules (like only deleting files with a certain extension).
41+
* A valid policy is any class that extends `Spatie\DirectoryCleanup\Policies\Policy`
42+
*/
43+
'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\Basic::class,
44+
3845
'directories' => [
3946
4047
/**
@@ -69,6 +76,28 @@ protected function schedule(Schedule $schedule)
6976

7077
```
7178

79+
If you want to apply additional conditional logic before a file is deleted, you can replace the default `cleanup_policy` with a custom one.
80+
Create a class which extends `Spatie\DirectoryCleanup\Policies\Policy` and add your logic to the `allow` method. Make sure to return `true` if the file should be deleted.
81+
82+
```php
83+
// app/CleanupPolicies/MyPolicy.php
84+
85+
namespace App\CleanupPolicies;
86+
87+
use Symfony\Component\Finder\SplFileInfo;
88+
use Spatie\DirectoryCleanup\Policies\Policy;
89+
90+
class MyPolicy extends Policy
91+
{
92+
public function allow(SplFileInfo $file) : bool
93+
{
94+
$filesToKeep = ['robots.txt'];
95+
96+
return ! in_array($file->getFilename(), $filesToKeep);
97+
}
98+
}
99+
```
100+
72101
## Changelog
73102

74103
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"require-dev": {
3030
"phpunit/phpunit": "^5.0|^6.0|^7.0",
31+
"mockery/mockery": "^1.1",
3132
"orchestra/testbench":"~3.3.0|~3.4.0|~3.5.0|~3.6.0|~3.7.0"
3233
},
3334
"autoload": {

config/laravel-directory-cleanup.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
return [
44

5+
/*
6+
* A policy will determine if a given file should be deleted. This is the perfect
7+
* place to apply custom rules (like only deleting files with a certain extension).
8+
* A valid policy is any class that extends `Spatie\DirectoryCleanup\Policies\Policy`
9+
*/
10+
'cleanup_policy' => \Spatie\DirectoryCleanup\Policies\Basic::class,
11+
512
'directories' => [
613

714
/*
@@ -11,7 +18,7 @@
1118

1219
/*
1320
'path/to/a/directory' => [
14-
'deleteAllOlderThanMinutes' => 60 * 24
21+
'deleteAllOlderThanMinutes' => 60 * 24,
1522
],
1623
*/
1724
],

src/DirectoryCleaner.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Carbon\Carbon;
66
use Illuminate\Support\Collection;
77
use Illuminate\Filesystem\Filesystem;
8+
use Spatie\DirectoryCleanup\Policies\Policy;
89

910
class DirectoryCleaner
1011
{
@@ -48,8 +49,22 @@ public function deleteFilesOlderThanMinutes(int $minutes) : Collection
4849
return Carbon::createFromTimestamp(filemtime($file))
4950
->lt($timeInPast);
5051
})
52+
->filter(function ($file) {
53+
return $this->policy()->allow($file);
54+
})
5155
->each(function ($file) {
5256
$this->filesystem->delete($file);
5357
});
5458
}
59+
60+
/**
61+
* @return \Spatie\DirectoryCleanup\Policies\Policy
62+
*/
63+
protected function policy() : Policy
64+
{
65+
return resolve(config(
66+
'laravel-directory-cleanup.cleanup_policy',
67+
\Spatie\DirectoryCleanup\Policies\Basic::class
68+
));
69+
}
5570
}

src/Policies/Basic.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Spatie\DirectoryCleanup\Policies;
4+
5+
use Symfony\Component\Finder\SplFileInfo;
6+
7+
class Basic extends Policy
8+
{
9+
public function allow(SplFileInfo $file) : bool
10+
{
11+
return true;
12+
}
13+
}

src/Policies/Policy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Spatie\DirectoryCleanup\Policies;
4+
5+
use Symfony\Component\Finder\SplFileInfo;
6+
7+
abstract class Policy
8+
{
9+
abstract public function allow(SplFileInfo $file) : bool;
10+
}

tests/CustomCleanupPolicy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Spatie\DirectoryCleanup\Test;
4+
5+
use Symfony\Component\Finder\SplFileInfo;
6+
use Spatie\DirectoryCleanup\Policies\Policy;
7+
8+
class CustomCleanupPolicy extends Policy
9+
{
10+
public function allow(SplFileInfo $file) : bool
11+
{
12+
$filesToKeep = ['keepThisFile.txt'];
13+
14+
return ! in_array($file->getFilename(), $filesToKeep);
15+
}
16+
}

tests/DirectoryCleanupTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,30 @@ public function it_can_cleanup_the_directories_specified_in_the_config_file_recu
8181
}
8282
}
8383

84+
/** @test */
85+
public function it_can_cleanup_the_directories_specified_in_the_config_file_but_keep_some_files()
86+
{
87+
$directories[$this->getTempDirectory(1, true)] = [
88+
'deleteAllOlderThanMinutes' => 5,
89+
];
90+
91+
$cleanup_policy = \Spatie\DirectoryCleanup\Test\CustomCleanupPolicy::class;
92+
93+
$this->app['config']->set('laravel-directory-cleanup', compact('directories', 'cleanup_policy'));
94+
95+
foreach ($directories as $directory => $config) {
96+
$this->createFile("{$directory}/keepThisFile.txt", 5);
97+
$this->createFile("{$directory}/removeThisFile.txt", 5);
98+
}
99+
100+
$this->artisan('clean:directories');
101+
102+
foreach ($directories as $directory => $config) {
103+
$this->assertFileExists("{$directory}/keepThisFile.txt");
104+
$this->assertFileNotExists("{$directory}/removeThisFile.txt");
105+
}
106+
}
107+
84108
protected function createFile(string $fileName, int $ageInMinutes)
85109
{
86110
touch($fileName, Carbon::now()->subMinutes($ageInMinutes)->subSeconds(5)->timestamp);

0 commit comments

Comments
 (0)