Skip to content

Commit e6b2a28

Browse files
committed
AC-10573: Static deploy doesn't update files
Changed to Content hash comparison (most reliable) than modification time approach as it is always showing file as modified
1 parent 492238d commit e6b2a28

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

lib/internal/Magento/Framework/App/Test/Unit/View/Asset/PublisherTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@ public function testPublishWithSourceFileNewer()
122122
->with('some/file.ext')
123123
->willReturn(true);
124124
$this->staticDirRead->expects($this->once())
125-
->method('stat')
125+
->method('readFile')
126126
->with('some/file.ext')
127-
->willReturn(['mtime' => 1000]);
127+
->willReturn('test');
128128

129129
$materializationStrategy =
130130
$this->getMockForAbstractClass(StrategyInterface::class);
@@ -148,9 +148,9 @@ public function testPublishWithSourceFileOlder()
148148
->with('some/file.ext')
149149
->willReturn(true);
150150
$this->staticDirRead->expects($this->once())
151-
->method('stat')
151+
->method('readFile')
152152
->with('some/file.ext')
153-
->willReturn(['mtime' => 0]);
153+
->willReturn(null);
154154

155155
$this->assertTrue($this->object->publish($this->getAsset()));
156156
}

lib/internal/Magento/Framework/App/View/Asset/Publisher.php

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ class Publisher
3333
*/
3434
private $writeFactory;
3535

36+
/**
37+
* @var array
38+
*/
39+
private static $fileHashes = [];
40+
3641
/**
3742
* @param \Magento\Framework\Filesystem $filesystem
3843
* @param MaterializationStrategy\Factory $materializationStrategyFactory
@@ -59,43 +64,69 @@ public function publish(Asset\LocalInterface $asset)
5964
$dir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
6065
$targetPath = $asset->getPath();
6166

62-
// Check if target file exists and is newer than source file
63-
if ($dir->isExist($targetPath) && !$this->isSourceFileNewer($asset, $dir, $targetPath)) {
67+
// Check if target file exists and content hasn't changed
68+
if ($dir->isExist($targetPath) && !$this->hasSourceFileChanged($asset, $dir, $targetPath)) {
6469
return true;
6570
}
6671

6772
return $this->publishAsset($asset);
6873
}
6974

7075
/**
71-
* Check if source file is newer than target file
76+
* Check if source file content has changed compared to target file
7277
*
7378
* @param Asset\LocalInterface $asset
7479
* @param \Magento\Framework\Filesystem\Directory\ReadInterface $dir
7580
* @param string $targetPath
7681
* @return bool
7782
*/
78-
private function isSourceFileNewer(Asset\LocalInterface $asset, $dir, $targetPath)
83+
private function hasSourceFileChanged(Asset\LocalInterface $asset, $dir, $targetPath)
7984
{
8085
$sourceFile = $asset->getSourceFile();
86+
// Get source file hash
87+
$sourceHash = $this->getFileHash($sourceFile);
8188

82-
$sourceMtime = $this->getFileModificationTime($sourceFile);
83-
$targetStat = $dir->stat($targetPath);
84-
$targetMtime = $targetStat['mtime'] ?? 0;
89+
// Get target file hash
90+
$targetHash = $this->getTargetFileHash($dir, $targetPath);
8591

86-
return ($sourceMtime > $targetMtime) || ($sourceMtime === 0 && $targetMtime > 0);
92+
// Compare hashes
93+
return $sourceHash !== $targetHash;
8794
}
8895

8996
/**
90-
* Get file modification time
97+
* Get file hash with caching
9198
*
9299
* @param string $filePath
93-
* @return int
100+
* @return string|false
94101
*/
95-
private function getFileModificationTime($filePath)
102+
private function getFileHash($filePath)
96103
{
97-
$mtime = @filemtime($filePath);
98-
return $mtime !== false ? $mtime : 0;
104+
if (!isset(self::$fileHashes[$filePath])) {
105+
$content = @file_get_contents($filePath);
106+
if ($content === false) {
107+
self::$fileHashes[$filePath] = false;
108+
} else {
109+
self::$fileHashes[$filePath] = md5($content);
110+
}
111+
}
112+
return self::$fileHashes[$filePath];
113+
}
114+
115+
/**
116+
* Get target file hash
117+
*
118+
* @param \Magento\Framework\Filesystem\Directory\ReadInterface $dir
119+
* @param string $targetPath
120+
* @return string|false
121+
*/
122+
private function getTargetFileHash($dir, $targetPath)
123+
{
124+
try {
125+
$content = $dir->readFile($targetPath);
126+
return md5($content);
127+
} catch (\Exception $e) {
128+
return false;
129+
}
99130
}
100131

101132
/**

0 commit comments

Comments
 (0)