|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use App\Domain\Path; |
|
6
|
|
|
use App\UI\Skippable; |
|
7
|
|
|
use App\UI\UserInterface; |
|
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
|
9
|
|
|
use Symfony\Component\Finder\Finder; |
|
10
|
|
|
|
|
11
|
|
|
final class FilesystemCleaner |
|
12
|
|
|
{ |
|
13
|
|
|
use Skippable; |
|
14
|
|
|
|
|
15
|
|
|
/** @var \App\UI\UserInterface */ |
|
16
|
|
|
private $ui; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param \App\UI\UserInterface $ui |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct(UserInterface $ui) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->ui = $ui; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param \App\Domain\Path $downloadPath |
|
28
|
|
|
* |
|
29
|
|
|
* @throws \RuntimeException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __invoke(Path $downloadPath): void |
|
32
|
|
|
{ |
|
33
|
|
|
$foldersToRemove = $this->getFoldersToRemove($downloadPath); |
|
34
|
|
|
|
|
35
|
|
|
if ($this->shouldRemoveFolders($foldersToRemove, $downloadPath)) { |
|
36
|
|
|
$this->removeFolders($foldersToRemove); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param \App\Domain\Path $downloadPath |
|
42
|
|
|
* |
|
43
|
|
|
* @return \App\Filesystem\FilesystemObjects |
|
44
|
|
|
* @throws \RuntimeException |
|
45
|
|
|
*/ |
|
46
|
|
|
private function getFoldersToRemove(Path $downloadPath): FilesystemObjects |
|
47
|
|
|
{ |
|
48
|
|
|
$foldersToRemove = new FilesystemObjects(); |
|
49
|
|
|
try { |
|
50
|
|
|
// Look for empty folders and remove these |
|
51
|
|
|
$emptyFolders = $this->getDownloadFoldersFinder($downloadPath) |
|
52
|
|
|
->filter(function (\SplFileInfo $folder) { |
|
53
|
|
|
return !(new Finder())->files()->in($folder->getRealPath())->hasResults(); |
|
54
|
|
|
}); |
|
55
|
|
|
|
|
56
|
|
|
foreach ($emptyFolders->getIterator() as $folder) { |
|
57
|
|
|
$foldersToRemove->set($folder->getRealPath(), $folder); |
|
58
|
|
|
} |
|
59
|
|
|
} catch (\LogicException $e) { |
|
60
|
|
|
// Here we know that the download folder will exist. |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $foldersToRemove; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param \App\Domain\Path $downloadPath |
|
68
|
|
|
* |
|
69
|
|
|
* @return \Symfony\Component\Finder\Finder |
|
70
|
|
|
* @throws \InvalidArgumentException |
|
71
|
|
|
*/ |
|
72
|
|
|
private function getDownloadFoldersFinder(Path $downloadPath): Finder |
|
73
|
|
|
{ |
|
74
|
|
|
return (new Finder()) |
|
75
|
|
|
->directories() |
|
76
|
|
|
->in((string) $downloadPath) |
|
77
|
|
|
->sort(function (\SplFileInfo $fileInfoA, \SplFileInfo $fileInfoB) { |
|
78
|
|
|
// Sort the result by folder depth |
|
79
|
|
|
$a = substr_count($fileInfoA->getRealPath(), DIRECTORY_SEPARATOR); |
|
80
|
|
|
$b = substr_count($fileInfoB->getRealPath(), DIRECTORY_SEPARATOR); |
|
81
|
|
|
|
|
82
|
|
|
return $a <=> $b; |
|
83
|
|
|
}); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param \App\Filesystem\FilesystemObjects $foldersToRemove |
|
88
|
|
|
*/ |
|
89
|
|
|
private function removeFolders(FilesystemObjects $foldersToRemove): void |
|
90
|
|
|
{ |
|
91
|
|
|
$errors = []; |
|
92
|
|
|
foreach ($foldersToRemove as $folderToRemove) { |
|
93
|
|
|
$relativeFolderPath = $folderToRemove->getRelativePathname(); |
|
94
|
|
|
|
|
95
|
|
|
try { |
|
96
|
|
|
(new Filesystem())->remove($folderToRemove->getRealPath()); |
|
97
|
|
|
|
|
98
|
|
|
$this->ui->writeln( |
|
99
|
|
|
sprintf( |
|
100
|
|
|
'%s* The folder <info>%s</info> has been removed.', |
|
101
|
|
|
$this->ui->indent(2), |
|
102
|
|
|
$relativeFolderPath |
|
103
|
|
|
) |
|
104
|
|
|
); |
|
105
|
|
|
} catch (\Exception $e) { |
|
106
|
|
|
$this->ui->logError( |
|
107
|
|
|
sprintf( |
|
108
|
|
|
'%s* <error>The folder %s could not be removed.</error>', |
|
109
|
|
|
$this->ui->indent(2), |
|
110
|
|
|
$relativeFolderPath |
|
111
|
|
|
), |
|
112
|
|
|
$errors |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
$this->ui->displayErrors($errors, 'the removal of folders', 'info', 1); |
|
117
|
|
|
|
|
118
|
|
|
$this->ui->writeln(PHP_EOL.'<info>Done.</info>'.PHP_EOL); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param \App\Filesystem\FilesystemObjects $foldersToRemove |
|
123
|
|
|
* @param \App\Domain\Path $downloadPath |
|
124
|
|
|
* |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
private function shouldRemoveFolders(FilesystemObjects $foldersToRemove, Path $downloadPath): bool |
|
128
|
|
|
{ |
|
129
|
|
|
$this->ui->write( |
|
130
|
|
|
sprintf( |
|
131
|
|
|
'Synchronize the <info>%s</info> folder with the downloaded contents... ', |
|
132
|
|
|
(string) $downloadPath |
|
133
|
|
|
) |
|
134
|
|
|
); |
|
135
|
|
|
|
|
136
|
|
|
if ($foldersToRemove->isEmpty()) { |
|
137
|
|
|
$this->ui->writeln('<info>Done.</info>'); |
|
138
|
|
|
|
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
$this->ui->writeln(PHP_EOL); |
|
143
|
|
|
|
|
144
|
|
|
if (!$this->ui->isDryRun() && !$this->ui->isInteractive()) { |
|
145
|
|
|
return true; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
$confirmationDefault = true; |
|
149
|
|
|
|
|
150
|
|
|
// If there's less than 10 folders, we can display them |
|
151
|
|
|
$nbFoldersToRemove = $foldersToRemove->count(); |
|
152
|
|
|
if ($nbFoldersToRemove <= 10) { |
|
153
|
|
|
$this->ui->writeln( |
|
154
|
|
|
sprintf( |
|
155
|
|
|
'%sThe script is about to remove the following folders from <info>%s</info>:', |
|
156
|
|
|
$this->ui->indent(), |
|
157
|
|
|
(string) $downloadPath |
|
158
|
|
|
) |
|
159
|
|
|
); |
|
160
|
|
|
$this->ui->listing( |
|
161
|
|
|
$foldersToRemove |
|
162
|
|
|
->map(function (\SplFileInfo $folder) use ($downloadPath) { |
|
163
|
|
|
return sprintf( |
|
164
|
|
|
'<info>%s</info>', |
|
165
|
|
|
str_replace((string) $downloadPath.DIRECTORY_SEPARATOR, '', $folder->getRealPath()) |
|
166
|
|
|
); |
|
167
|
|
|
}) |
|
168
|
|
|
->toArray(), |
|
169
|
|
|
3 |
|
170
|
|
|
); |
|
171
|
|
|
} else { |
|
172
|
|
|
$confirmationDefault = false; |
|
173
|
|
|
|
|
174
|
|
|
$this->ui->write( |
|
175
|
|
|
sprintf( |
|
176
|
|
|
'%sThe script is about to remove <question> %s </question> folders from <info>%s</info>. ', |
|
177
|
|
|
$this->ui->indent(), |
|
178
|
|
|
$nbFoldersToRemove, |
|
179
|
|
|
(string) $downloadPath |
|
180
|
|
|
) |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$this->ui->write($this->ui->indent()); |
|
185
|
|
|
|
|
186
|
|
|
if ($this->skip($this->ui) || !$this->ui->confirm($confirmationDefault)) { |
|
187
|
|
|
$this->ui->writeln(($this->ui->isDryRun() ? '' : PHP_EOL).'<info>Done.</info>'.PHP_EOL); |
|
188
|
|
|
|
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
return true; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|