8
8
9
9
use Magento \Framework \Exception \FileSystemException ;
10
10
use Magento \Framework \Exception \ValidatorException ;
11
+ use Magento \Framework \Filesystem \DriverInterface ;
12
+ use Magento \Framework \Phrase ;
11
13
12
14
/**
13
15
* Write Interface implementation
@@ -25,14 +27,14 @@ class Write extends Read implements WriteInterface
25
27
* Constructor
26
28
*
27
29
* @param \Magento\Framework\Filesystem\File\WriteFactory $fileFactory
28
- * @param \Magento\Framework\Filesystem\ DriverInterface $driver
30
+ * @param DriverInterface $driver
29
31
* @param string $path
30
32
* @param int $createPermissions
31
33
* @param PathValidatorInterface|null $pathValidator
32
34
*/
33
35
public function __construct (
34
36
\Magento \Framework \Filesystem \File \WriteFactory $ fileFactory ,
35
- \ Magento \ Framework \ Filesystem \ DriverInterface $ driver ,
37
+ DriverInterface $ driver ,
36
38
$ path ,
37
39
$ createPermissions = null ,
38
40
?PathValidatorInterface $ pathValidator = null
@@ -48,13 +50,13 @@ public function __construct(
48
50
*
49
51
* @param string $path
50
52
* @return void
51
- * @throws \Magento\Framework\Exception\ FileSystemException
53
+ * @throws FileSystemException|ValidatorException
52
54
*/
53
55
protected function assertWritable ($ path )
54
56
{
55
57
if ($ this ->isWritable ($ path ) === false ) {
56
58
$ path = $ this ->getAbsolutePath ($ path );
57
- throw new FileSystemException (new \ Magento \ Framework \ Phrase ('The path "%1" is not writable. ' , [$ path ]));
59
+ throw new FileSystemException (new Phrase ('The path "%1" is not writable. ' , [$ path ]));
58
60
}
59
61
}
60
62
@@ -63,15 +65,15 @@ protected function assertWritable($path)
63
65
*
64
66
* @param string $path
65
67
* @return void
66
- * @throws \Magento\Framework\Exception\ FileSystemException
68
+ * @throws FileSystemException
67
69
*/
68
70
protected function assertIsFile ($ path )
69
71
{
70
72
$ absolutePath = $ this ->driver ->getAbsolutePath ($ this ->path , $ path );
71
73
clearstatcache (true , $ absolutePath );
72
74
if (!$ this ->driver ->isFile ($ absolutePath )) {
73
75
throw new FileSystemException (
74
- new \ Magento \ Framework \ Phrase ('The "%1" file doesn \'t exist. ' , [$ absolutePath ])
76
+ new Phrase ('The "%1" file doesn \'t exist. ' , [$ absolutePath ])
75
77
);
76
78
}
77
79
}
@@ -149,7 +151,7 @@ public function copyFile($path, $destination, WriteInterface $targetDirectory =
149
151
* @param string $destination
150
152
* @param WriteInterface $targetDirectory [optional]
151
153
* @return bool
152
- * @throws \Magento\Framework\Exception\ FileSystemException
154
+ * @throws FileSystemException
153
155
* @throws ValidatorException
154
156
*/
155
157
public function createSymlink ($ path , $ destination , WriteInterface $ targetDirectory = null )
@@ -178,10 +180,18 @@ public function delete($path = null)
178
180
{
179
181
$ exceptionMessages = [];
180
182
$ this ->validatePath ($ path );
183
+
181
184
if (!$ this ->isExist ($ path )) {
182
185
return true ;
183
186
}
187
+
184
188
$ absolutePath = $ this ->driver ->getAbsolutePath ($ this ->path , $ path );
189
+ $ basePath = $ this ->driver ->getRealPathSafety ($ this ->driver ->getAbsolutePath ($ this ->path , '' ));
190
+
191
+ if ($ path !== null && $ path !== '' && $ this ->driver ->getRealPathSafety ($ absolutePath ) === $ basePath ) {
192
+ throw new FileSystemException (new Phrase ('The path "%1" is not writable. ' , [$ path ]));
193
+ }
194
+
185
195
if ($ this ->driver ->isFile ($ absolutePath )) {
186
196
$ this ->driver ->deleteFile ($ absolutePath );
187
197
} else {
@@ -198,12 +208,13 @@ public function delete($path = null)
198
208
199
209
if (!empty ($ exceptionMessages )) {
200
210
throw new FileSystemException (
201
- new \ Magento \ Framework \ Phrase (
211
+ new Phrase (
202
212
\implode (' ' , $ exceptionMessages )
203
213
)
204
214
);
205
215
}
206
216
}
217
+
207
218
return true ;
208
219
}
209
220
@@ -231,7 +242,7 @@ private function deleteFilesRecursively(string $path)
231
242
}
232
243
if (!empty ($ exceptionMessages )) {
233
244
throw new FileSystemException (
234
- new \ Magento \ Framework \ Phrase (
245
+ new Phrase (
235
246
\implode (' ' , $ exceptionMessages )
236
247
)
237
248
);
@@ -297,7 +308,7 @@ public function touch($path, $modificationTime = null)
297
308
*
298
309
* @param string|null $path
299
310
* @return bool
300
- * @throws \Magento\Framework\Exception\ FileSystemException
311
+ * @throws FileSystemException
301
312
* @throws ValidatorException
302
313
*/
303
314
public function isWritable ($ path = null )
@@ -313,7 +324,7 @@ public function isWritable($path = null)
313
324
* @param string $path
314
325
* @param string $mode
315
326
* @return \Magento\Framework\Filesystem\File\WriteInterface
316
- * @throws \Magento\Framework\Exception\ FileSystemException
327
+ * @throws FileSystemException
317
328
* @throws ValidatorException
318
329
*/
319
330
public function openFile ($ path , $ mode = 'w ' )
@@ -334,7 +345,7 @@ public function openFile($path, $mode = 'w')
334
345
* @param string $content
335
346
* @param string|null $mode
336
347
* @return int The number of bytes that were written.
337
- * @throws FileSystemException
348
+ * @throws FileSystemException|ValidatorException
338
349
*/
339
350
public function writeFile ($ path , $ content , $ mode = 'w+ ' )
340
351
{
@@ -344,7 +355,7 @@ public function writeFile($path, $content, $mode = 'w+')
344
355
/**
345
356
* Get driver
346
357
*
347
- * @return \Magento\Framework\Filesystem\ DriverInterface
358
+ * @return DriverInterface
348
359
*/
349
360
public function getDriver ()
350
361
{
0 commit comments