88
99use Magento \Framework \Exception \FileSystemException ;
1010use Magento \Framework \Exception \ValidatorException ;
11+ use Magento \Framework \Filesystem \DriverInterface ;
12+ use Magento \Framework \Phrase ;
1113
1214/**
1315 * Write Interface implementation
@@ -25,14 +27,14 @@ class Write extends Read implements WriteInterface
2527 * Constructor
2628 *
2729 * @param \Magento\Framework\Filesystem\File\WriteFactory $fileFactory
28- * @param \Magento\Framework\Filesystem\ DriverInterface $driver
30+ * @param DriverInterface $driver
2931 * @param string $path
3032 * @param int $createPermissions
3133 * @param PathValidatorInterface|null $pathValidator
3234 */
3335 public function __construct (
3436 \Magento \Framework \Filesystem \File \WriteFactory $ fileFactory ,
35- \ Magento \ Framework \ Filesystem \ DriverInterface $ driver ,
37+ DriverInterface $ driver ,
3638 $ path ,
3739 $ createPermissions = null ,
3840 ?PathValidatorInterface $ pathValidator = null
@@ -48,13 +50,13 @@ public function __construct(
4850 *
4951 * @param string $path
5052 * @return void
51- * @throws \Magento\Framework\Exception\ FileSystemException
53+ * @throws FileSystemException|ValidatorException
5254 */
5355 protected function assertWritable ($ path )
5456 {
5557 if ($ this ->isWritable ($ path ) === false ) {
5658 $ 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 ]));
5860 }
5961 }
6062
@@ -63,15 +65,15 @@ protected function assertWritable($path)
6365 *
6466 * @param string $path
6567 * @return void
66- * @throws \Magento\Framework\Exception\ FileSystemException
68+ * @throws FileSystemException
6769 */
6870 protected function assertIsFile ($ path )
6971 {
7072 $ absolutePath = $ this ->driver ->getAbsolutePath ($ this ->path , $ path );
7173 clearstatcache (true , $ absolutePath );
7274 if (!$ this ->driver ->isFile ($ absolutePath )) {
7375 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 ])
7577 );
7678 }
7779 }
@@ -149,7 +151,7 @@ public function copyFile($path, $destination, WriteInterface $targetDirectory =
149151 * @param string $destination
150152 * @param WriteInterface $targetDirectory [optional]
151153 * @return bool
152- * @throws \Magento\Framework\Exception\ FileSystemException
154+ * @throws FileSystemException
153155 * @throws ValidatorException
154156 */
155157 public function createSymlink ($ path , $ destination , WriteInterface $ targetDirectory = null )
@@ -178,10 +180,18 @@ public function delete($path = null)
178180 {
179181 $ exceptionMessages = [];
180182 $ this ->validatePath ($ path );
183+
181184 if (!$ this ->isExist ($ path )) {
182185 return true ;
183186 }
187+
184188 $ 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+
185195 if ($ this ->driver ->isFile ($ absolutePath )) {
186196 $ this ->driver ->deleteFile ($ absolutePath );
187197 } else {
@@ -198,12 +208,13 @@ public function delete($path = null)
198208
199209 if (!empty ($ exceptionMessages )) {
200210 throw new FileSystemException (
201- new \ Magento \ Framework \ Phrase (
211+ new Phrase (
202212 \implode (' ' , $ exceptionMessages )
203213 )
204214 );
205215 }
206216 }
217+
207218 return true ;
208219 }
209220
@@ -231,7 +242,7 @@ private function deleteFilesRecursively(string $path)
231242 }
232243 if (!empty ($ exceptionMessages )) {
233244 throw new FileSystemException (
234- new \ Magento \ Framework \ Phrase (
245+ new Phrase (
235246 \implode (' ' , $ exceptionMessages )
236247 )
237248 );
@@ -297,7 +308,7 @@ public function touch($path, $modificationTime = null)
297308 *
298309 * @param string|null $path
299310 * @return bool
300- * @throws \Magento\Framework\Exception\ FileSystemException
311+ * @throws FileSystemException
301312 * @throws ValidatorException
302313 */
303314 public function isWritable ($ path = null )
@@ -313,7 +324,7 @@ public function isWritable($path = null)
313324 * @param string $path
314325 * @param string $mode
315326 * @return \Magento\Framework\Filesystem\File\WriteInterface
316- * @throws \Magento\Framework\Exception\ FileSystemException
327+ * @throws FileSystemException
317328 * @throws ValidatorException
318329 */
319330 public function openFile ($ path , $ mode = 'w ' )
@@ -334,7 +345,7 @@ public function openFile($path, $mode = 'w')
334345 * @param string $content
335346 * @param string|null $mode
336347 * @return int The number of bytes that were written.
337- * @throws FileSystemException
348+ * @throws FileSystemException|ValidatorException
338349 */
339350 public function writeFile ($ path , $ content , $ mode = 'w+ ' )
340351 {
@@ -344,7 +355,7 @@ public function writeFile($path, $content, $mode = 'w+')
344355 /**
345356 * Get driver
346357 *
347- * @return \Magento\Framework\Filesystem\ DriverInterface
358+ * @return DriverInterface
348359 */
349360 public function getDriver ()
350361 {
0 commit comments