9
9
use Magento \Framework \Exception \FileSystemException ;
10
10
use Magento \Framework \Exception \ValidatorException ;
11
11
12
+ /**
13
+ * Write Interface implementation
14
+ */
12
15
class Write extends Read implements WriteInterface
13
16
{
14
17
/**
@@ -175,6 +178,7 @@ public function createSymlink($path, $destination, WriteInterface $targetDirecto
175
178
*/
176
179
public function delete ($ path = null )
177
180
{
181
+ $ exceptionMessages = [];
178
182
$ this ->validatePath ($ path );
179
183
if (!$ this ->isExist ($ path )) {
180
184
return true ;
@@ -183,11 +187,59 @@ public function delete($path = null)
183
187
if ($ this ->driver ->isFile ($ absolutePath )) {
184
188
$ this ->driver ->deleteFile ($ absolutePath );
185
189
} else {
186
- $ this ->driver ->deleteDirectory ($ absolutePath );
190
+ try {
191
+ $ this ->deleteFilesRecursively ($ absolutePath );
192
+ } catch (FileSystemException $ e ) {
193
+ $ exceptionMessages [] = $ e ->getMessage ();
194
+ }
195
+ try {
196
+ $ this ->driver ->deleteDirectory ($ absolutePath );
197
+ } catch (FileSystemException $ e ) {
198
+ $ exceptionMessages [] = $ e ->getMessage ();
199
+ }
200
+
201
+ if (!empty ($ exceptionMessages )) {
202
+ throw new FileSystemException (
203
+ new \Magento \Framework \Phrase (
204
+ \implode (' ' , $ exceptionMessages )
205
+ )
206
+ );
207
+ }
187
208
}
188
209
return true ;
189
210
}
190
211
212
+ /**
213
+ * Delete files recursively
214
+ *
215
+ * Implemented in order to delete as much files as possible and collect all exceptions
216
+ *
217
+ * @param string $path
218
+ * @return void
219
+ * @throws FileSystemException
220
+ */
221
+ private function deleteFilesRecursively (string $ path )
222
+ {
223
+ $ exceptionMessages = [];
224
+ $ entitiesList = $ this ->driver ->readDirectoryRecursively ($ path );
225
+ foreach ($ entitiesList as $ entityPath ) {
226
+ if ($ this ->driver ->isFile ($ entityPath )) {
227
+ try {
228
+ $ this ->driver ->deleteFile ($ entityPath );
229
+ } catch (FileSystemException $ e ) {
230
+ $ exceptionMessages [] = $ e ->getMessage ();
231
+ }
232
+ }
233
+ }
234
+ if (!empty ($ exceptionMessages )) {
235
+ throw new FileSystemException (
236
+ new \Magento \Framework \Phrase (
237
+ \implode (' ' , $ exceptionMessages )
238
+ )
239
+ );
240
+ }
241
+ }
242
+
191
243
/**
192
244
* Change permissions of given path
193
245
*
@@ -245,7 +297,7 @@ public function touch($path, $modificationTime = null)
245
297
/**
246
298
* Check if given path is writable
247
299
*
248
- * @param null $path
300
+ * @param string| null $path
249
301
* @return bool
250
302
* @throws \Magento\Framework\Exception\FileSystemException
251
303
* @throws ValidatorException
0 commit comments