@@ -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