|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\DirectoryCleanup; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
7
|
|
|
use Illuminate\Support\Collection; |
|
8
|
|
|
use Spatie\DirectoryCleanup\Policies\CleanupPolicy; |
|
9
|
|
|
|
|
10
|
|
|
class DirectoryCleaner |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var \Illuminate\Filesystem\Filesystem */ |
|
13
|
|
|
protected $filesystem; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
protected $directory; |
|
17
|
|
|
|
|
18
|
|
|
/** @var Carbon */ |
|
19
|
|
|
protected $timeInPast; |
|
20
|
|
|
|
|
21
|
|
|
public function __construct(Filesystem $filesystem) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->filesystem = $filesystem; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function setDirectory(string $directory) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->directory = $directory; |
|
29
|
|
|
|
|
30
|
|
|
return $this; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function setMinutes($minutes) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->timeInPast = Carbon::now()->subMinutes($minutes); |
|
36
|
|
|
|
|
37
|
|
|
return $this; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function deleteFilesOlderThanMinutes($amountOfFilesDeleted = 0, $directory = null): int |
|
41
|
|
|
{ |
|
42
|
|
|
$workingDir = $directory ?: realpath($this->directory); |
|
43
|
|
|
$directories = collect($this->filesystem->directories($workingDir)); |
|
44
|
|
|
if ($directory === null) { |
|
45
|
|
|
$directories = $directories->add($workingDir); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
foreach ($directories as $subDirectory) { |
|
49
|
|
|
$amountOfFilesDeleted = $this->deleteFilesOlderThanMinutes($amountOfFilesDeleted, $subDirectory); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$files = collect($this->filesystem->files($workingDir, true)) |
|
53
|
|
|
->filter(function ($file) { |
|
54
|
|
|
return Carbon::createFromTimestamp(filemtime($file)) |
|
55
|
|
|
->lt($this->timeInPast); |
|
56
|
|
|
}) |
|
57
|
|
|
->filter(function ($file) { |
|
58
|
|
|
return $this->policy()->shouldDelete($file); |
|
59
|
|
|
}) |
|
60
|
|
|
->each(function ($file) { |
|
61
|
|
|
$this->filesystem->delete($file); |
|
62
|
|
|
}); |
|
63
|
|
|
|
|
64
|
|
|
return $amountOfFilesDeleted + $files->count(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function deleteEmptySubdirectories(): Collection |
|
68
|
|
|
{ |
|
69
|
|
|
return collect($this->filesystem->directories($this->directory)) |
|
70
|
|
|
->filter(function ($directory) { |
|
71
|
|
|
return ! $this->filesystem->allFiles($directory, true); |
|
72
|
|
|
}) |
|
73
|
|
|
->each(function ($directory) { |
|
74
|
|
|
$this->filesystem->deleteDirectory($directory); |
|
75
|
|
|
}); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function policy(): CleanupPolicy |
|
79
|
|
|
{ |
|
80
|
|
|
return resolve(config( |
|
81
|
|
|
'laravel-directory-cleanup.cleanup_policy', |
|
82
|
|
|
\Spatie\DirectoryCleanup\Policies\DeleteEverything::class |
|
83
|
|
|
)); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|