Skip to content

Commit d9938c5

Browse files
committed
Move cache cleanup operation on state modification operation and make it granula
1 parent 68aeb6d commit d9938c5

File tree

4 files changed

+1073
-53
lines changed

4 files changed

+1073
-53
lines changed

lib/internal/Magento/Framework/Filesystem/Directory/Write.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,8 @@ protected function assertWritable($path)
6767
*/
6868
protected function assertIsFile($path)
6969
{
70-
clearstatcache();
7170
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
71+
clearstatcache(true, $absolutePath);
7272
if (!$this->driver->isFile($absolutePath)) {
7373
throw new FileSystemException(
7474
new \Magento\Framework\Phrase('The "%1" file doesn\'t exist.', [$absolutePath])

lib/internal/Magento/Framework/Filesystem/Driver/File.php

Lines changed: 58 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
use Magento\Framework\Exception\FileSystemException;
1212
use Magento\Framework\Filesystem\DriverInterface;
1313
use Magento\Framework\Filesystem\Glob;
14+
use Magento\Framework\Phrase;
1415

1516
/**
16-
* Driver file class
17+
* Filesystem driver that uses the local filesystem.
18+
* Assumed that stat cache is cleanup before test filesystem
1719
*
1820
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
1921
*/
@@ -47,11 +49,12 @@ protected function getWarningMessage()
4749
*/
4850
public function isExists($path)
4951
{
50-
clearstatcache();
51-
$result = @file_exists($this->getScheme() . $path);
52+
$filename = $this->getScheme() . $path;
53+
clearstatcache(false, $filename);
54+
$result = @file_exists($filename);
5255
if ($result === null) {
5356
throw new FileSystemException(
54-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
57+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
5558
);
5659
}
5760
return $result;
@@ -66,11 +69,12 @@ public function isExists($path)
6669
*/
6770
public function stat($path)
6871
{
69-
clearstatcache();
70-
$result = @stat($this->getScheme() . $path);
72+
$filename = $this->getScheme() . $path;
73+
clearstatcache(false, $filename);
74+
$result = @stat($filename);
7175
if (!$result) {
7276
throw new FileSystemException(
73-
new \Magento\Framework\Phrase('Cannot gather stats! %1', [$this->getWarningMessage()])
77+
new Phrase('Cannot gather stats! %1', [$this->getWarningMessage()])
7478
);
7579
}
7680
return $result;
@@ -85,11 +89,12 @@ public function stat($path)
8589
*/
8690
public function isReadable($path)
8791
{
88-
clearstatcache();
89-
$result = @is_readable($this->getScheme() . $path);
92+
$filename = $this->getScheme() . $path;
93+
clearstatcache(false, $filename);
94+
$result = @is_readable($filename);
9095
if ($result === null) {
9196
throw new FileSystemException(
92-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
97+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
9398
);
9499
}
95100
return $result;
@@ -104,11 +109,12 @@ public function isReadable($path)
104109
*/
105110
public function isFile($path)
106111
{
107-
clearstatcache();
108-
$result = @is_file($this->getScheme() . $path);
112+
$filename = $this->getScheme() . $path;
113+
clearstatcache(false, $filename);
114+
$result = @is_file($filename);
109115
if ($result === null) {
110116
throw new FileSystemException(
111-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
117+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
112118
);
113119
}
114120
return $result;
@@ -123,11 +129,12 @@ public function isFile($path)
123129
*/
124130
public function isDirectory($path)
125131
{
126-
clearstatcache();
127-
$result = @is_dir($this->getScheme() . $path);
132+
$filename = $this->getScheme() . $path;
133+
clearstatcache(false, $filename);
134+
$result = @is_dir($filename);
128135
if ($result === null) {
129136
throw new FileSystemException(
130-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
137+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
131138
);
132139
}
133140
return $result;
@@ -144,11 +151,12 @@ public function isDirectory($path)
144151
*/
145152
public function fileGetContents($path, $flag = null, $context = null)
146153
{
147-
clearstatcache();
148-
$result = @file_get_contents($this->getScheme() . $path, $flag, $context);
154+
$filename = $this->getScheme() . $path;
155+
clearstatcache(false, $filename);
156+
$result = @file_get_contents($filename, $flag, $context);
149157
if (false === $result) {
150158
throw new FileSystemException(
151-
new \Magento\Framework\Phrase(
159+
new Phrase(
152160
'The contents from the "%1" file can\'t be read. %2',
153161
[$path, $this->getWarningMessage()]
154162
)
@@ -166,11 +174,12 @@ public function fileGetContents($path, $flag = null, $context = null)
166174
*/
167175
public function isWritable($path)
168176
{
169-
clearstatcache();
170-
$result = @is_writable($this->getScheme() . $path);
177+
$filename = $this->getScheme() . $path;
178+
clearstatcache(false, $filename);
179+
$result = @is_writable($filename);
171180
if ($result === null) {
172181
throw new FileSystemException(
173-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
182+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
174183
);
175184
}
176185
return $result;
@@ -224,7 +233,7 @@ private function mkdirRecursive($path, $permissions = 0777)
224233
$result = true;
225234
} else {
226235
throw new FileSystemException(
227-
new \Magento\Framework\Phrase(
236+
new Phrase(
228237
'Directory "%1" cannot be created %2',
229238
[$path, $this->getWarningMessage()]
230239
)
@@ -254,7 +263,7 @@ public function readDirectory($path)
254263
sort($result);
255264
return $result;
256265
} catch (\Exception $e) {
257-
throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
266+
throw new FileSystemException(new Phrase($e->getMessage()), $e);
258267
}
259268
}
260269

@@ -297,7 +306,7 @@ public function rename($oldPath, $newPath, DriverInterface $targetDriver = null)
297306
}
298307
if (!$result) {
299308
throw new FileSystemException(
300-
new \Magento\Framework\Phrase(
309+
new Phrase(
301310
'The path "%1" cannot be renamed into "%2" %3',
302311
[$oldPath, $newPath, $this->getWarningMessage()]
303312
)
@@ -326,7 +335,7 @@ public function copy($source, $destination, DriverInterface $targetDriver = null
326335
}
327336
if (!$result) {
328337
throw new FileSystemException(
329-
new \Magento\Framework\Phrase(
338+
new Phrase(
330339
'The file or directory "%1" cannot be copied to "%2" %3',
331340
[
332341
$source,
@@ -356,7 +365,7 @@ public function symlink($source, $destination, DriverInterface $targetDriver = n
356365
}
357366
if (!$result) {
358367
throw new FileSystemException(
359-
new \Magento\Framework\Phrase(
368+
new Phrase(
360369
'A symlink for "%1" can\'t be created and placed to "%2". %3',
361370
[
362371
$source,
@@ -381,7 +390,7 @@ public function deleteFile($path)
381390
$result = @unlink($this->getScheme() . $path);
382391
if (!$result) {
383392
throw new FileSystemException(
384-
new \Magento\Framework\Phrase(
393+
new Phrase(
385394
'The "%1" file can\'t be deleted. %2',
386395
[$path, $this->getWarningMessage()]
387396
)
@@ -417,7 +426,7 @@ public function deleteDirectory($path)
417426

418427
if (!empty($exceptionMessages)) {
419428
throw new FileSystemException(
420-
new \Magento\Framework\Phrase(
429+
new Phrase(
421430
\implode(' ', $exceptionMessages)
422431
)
423432
);
@@ -431,7 +440,7 @@ public function deleteDirectory($path)
431440
}
432441
if (!$result) {
433442
throw new FileSystemException(
434-
new \Magento\Framework\Phrase(
443+
new Phrase(
435444
'The directory "%1" cannot be deleted %2',
436445
[$path, $this->getWarningMessage()]
437446
)
@@ -453,7 +462,7 @@ public function changePermissions($path, $permissions)
453462
$result = @chmod($this->getScheme() . $path, $permissions);
454463
if (!$result) {
455464
throw new FileSystemException(
456-
new \Magento\Framework\Phrase(
465+
new Phrase(
457466
'The permissions can\'t be changed for the "%1" path. %2.',
458467
[$path, $this->getWarningMessage()]
459468
)
@@ -481,7 +490,7 @@ public function changePermissionsRecursively($path, $dirPermissions, $filePermis
481490
}
482491
if (!$result) {
483492
throw new FileSystemException(
484-
new \Magento\Framework\Phrase(
493+
new Phrase(
485494
'The permissions can\'t be changed for the "%1" path. %2.',
486495
[$path, $this->getWarningMessage()]
487496
)
@@ -503,7 +512,7 @@ public function changePermissionsRecursively($path, $dirPermissions, $filePermis
503512
}
504513
if (!$result) {
505514
throw new FileSystemException(
506-
new \Magento\Framework\Phrase(
515+
new Phrase(
507516
'The permissions can\'t be changed for the "%1" path. %2.',
508517
[$path, $this->getWarningMessage()]
509518
)
@@ -530,7 +539,7 @@ public function touch($path, $modificationTime = null)
530539
}
531540
if (!$result) {
532541
throw new FileSystemException(
533-
new \Magento\Framework\Phrase(
542+
new Phrase(
534543
'The "%1" file or directory can\'t be touched. %2',
535544
[$path, $this->getWarningMessage()]
536545
)
@@ -553,7 +562,7 @@ public function filePutContents($path, $content, $mode = null)
553562
$result = @file_put_contents($this->getScheme() . $path, $content, $mode);
554563
if ($result === false) {
555564
throw new FileSystemException(
556-
new \Magento\Framework\Phrase(
565+
new Phrase(
557566
'The specified "%1" file couldn\'t be written. %2',
558567
[$path, $this->getWarningMessage()]
559568
)
@@ -575,7 +584,7 @@ public function fileOpen($path, $mode)
575584
$result = @fopen($this->getScheme() . $path, $mode);
576585
if (!$result) {
577586
throw new FileSystemException(
578-
new \Magento\Framework\Phrase('File "%1" cannot be opened %2', [$path, $this->getWarningMessage()])
587+
new Phrase('File "%1" cannot be opened %2', [$path, $this->getWarningMessage()])
579588
);
580589
}
581590
return $result;
@@ -597,7 +606,7 @@ public function fileReadLine($resource, $length, $ending = null)
597606
// phpcs:enable
598607
if (false === $result) {
599608
throw new FileSystemException(
600-
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
609+
new Phrase('File cannot be read %1', [$this->getWarningMessage()])
601610
);
602611
}
603612

@@ -617,7 +626,7 @@ public function fileRead($resource, $length)
617626
$result = @fread($resource, $length);
618627
if ($result === false) {
619628
throw new FileSystemException(
620-
new \Magento\Framework\Phrase('File cannot be read %1', [$this->getWarningMessage()])
629+
new Phrase('File cannot be read %1', [$this->getWarningMessage()])
621630
);
622631
}
623632
return $result;
@@ -639,7 +648,7 @@ public function fileGetCsv($resource, $length = 0, $delimiter = ',', $enclosure
639648
$result = @fgetcsv($resource, $length, $delimiter, $enclosure, $escape);
640649
if ($result === null) {
641650
throw new FileSystemException(
642-
new \Magento\Framework\Phrase(
651+
new Phrase(
643652
'The "%1" CSV handle is incorrect. Verify the handle and try again.',
644653
[$this->getWarningMessage()]
645654
)
@@ -660,7 +669,7 @@ public function fileTell($resource)
660669
$result = @ftell($resource);
661670
if ($result === null) {
662671
throw new FileSystemException(
663-
new \Magento\Framework\Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
672+
new Phrase('An error occurred during "%1" execution.', [$this->getWarningMessage()])
664673
);
665674
}
666675
return $result;
@@ -680,7 +689,7 @@ public function fileSeek($resource, $offset, $whence = SEEK_SET)
680689
$result = @fseek($resource, $offset, $whence);
681690
if ($result === -1) {
682691
throw new FileSystemException(
683-
new \Magento\Framework\Phrase(
692+
new Phrase(
684693
'An error occurred during "%1" fileSeek execution.',
685694
[$this->getWarningMessage()]
686695
)
@@ -712,7 +721,7 @@ public function fileClose($resource)
712721
$result = @fclose($resource);
713722
if (!$result) {
714723
throw new FileSystemException(
715-
new \Magento\Framework\Phrase(
724+
new Phrase(
716725
'An error occurred during "%1" fileClose execution.',
717726
[$this->getWarningMessage()]
718727
)
@@ -758,7 +767,7 @@ public function fileWrite($resource, $data)
758767
*/
759768
private function fileSystemException($message, $arguments = [])
760769
{
761-
throw new FileSystemException(new \Magento\Framework\Phrase($message, $arguments));
770+
throw new FileSystemException(new Phrase($message, $arguments));
762771
}
763772

764773
/**
@@ -777,7 +786,7 @@ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure
777786
* Security enhancement for CSV data processing by Excel-like applications.
778787
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1054702
779788
*
780-
* @var $value string|\Magento\Framework\Phrase
789+
* @var $value string|Phrase
781790
*/
782791
foreach ($data as $key => $value) {
783792
if (!is_string($value)) {
@@ -791,7 +800,7 @@ public function filePutCsv($resource, array $data, $delimiter = ',', $enclosure
791800
$result = @fputcsv($resource, $data, $delimiter, $enclosure);
792801
if (!$result) {
793802
throw new FileSystemException(
794-
new \Magento\Framework\Phrase(
803+
new Phrase(
795804
'An error occurred during "%1" filePutCsv execution.',
796805
[$this->getWarningMessage()]
797806
)
@@ -812,7 +821,7 @@ public function fileFlush($resource)
812821
$result = @fflush($resource);
813822
if (!$result) {
814823
throw new FileSystemException(
815-
new \Magento\Framework\Phrase(
824+
new Phrase(
816825
'An error occurred during "%1" fileFlush execution.',
817826
[$this->getWarningMessage()]
818827
)
@@ -834,7 +843,7 @@ public function fileLock($resource, $lockMode = LOCK_EX)
834843
$result = @flock($resource, $lockMode);
835844
if (!$result) {
836845
throw new FileSystemException(
837-
new \Magento\Framework\Phrase(
846+
new Phrase(
838847
'An error occurred during "%1" fileLock execution.',
839848
[$this->getWarningMessage()]
840849
)
@@ -855,7 +864,7 @@ public function fileUnlock($resource)
855864
$result = @flock($resource, LOCK_UN);
856865
if (!$result) {
857866
throw new FileSystemException(
858-
new \Magento\Framework\Phrase(
867+
new Phrase(
859868
'An error occurred during "%1" fileUnlock execution.',
860869
[$this->getWarningMessage()]
861870
)
@@ -947,7 +956,7 @@ public function readDirectoryRecursively($path = null)
947956
$result[] = $file->getPathname();
948957
}
949958
} catch (\Exception $e) {
950-
throw new FileSystemException(new \Magento\Framework\Phrase($e->getMessage()), $e);
959+
throw new FileSystemException(new Phrase($e->getMessage()), $e);
951960
}
952961
return $result;
953962
}

lib/internal/Magento/Framework/Filesystem/Driver/Http.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use Magento\Framework\Exception\FileSystemException;
1212

1313
/**
14-
* Http driver file class.
14+
* Allows interacting with http endpoint like with FileSystem
1515
*/
1616
class Http extends File
1717
{
@@ -83,8 +83,9 @@ public function stat($path)
8383
*/
8484
public function fileGetContents($path, $flags = null, $context = null)
8585
{
86-
clearstatcache();
87-
$result = @file_get_contents($this->getScheme() . $path, $flags, $context);
86+
$fullPath = $this->getScheme() . $path;
87+
clearstatcache(false, $fullPath);
88+
$result = @file_get_contents($fullPath, $flags, $context);
8889
if (false === $result) {
8990
throw new FileSystemException(
9091
new \Magento\Framework\Phrase(

0 commit comments

Comments
 (0)