Skip to content

Commit 3e6d389

Browse files
committed
AC-10573: Static deploy doesn't update files
Fix static content deployment to check file modification times - Add modification time checking in Publisher to update files when source is newer - Add null checks in CSS/LESS preprocessors and CSS URLs post-processor - Prevents "Call to a member function getPackage() on null" error
1 parent e3f780b commit 3e6d389

File tree

2 files changed

+50
-7
lines changed
  • app/code/Magento/Deploy/Package/Processor/PostProcessor
  • lib/internal/Magento/Framework/App/View/Asset

2 files changed

+50
-7
lines changed

app/code/Magento/Deploy/Package/Processor/PostProcessor/CssUrls.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2017 Adobe
4+
* All rights reserved.
55
*/
66
namespace Magento\Deploy\Package\Processor\PostProcessor;
77

@@ -66,7 +66,7 @@ public function process(Package $package, array $options)
6666
return false;
6767
}
6868
$urlMap = [];
69-
/** @var PackageFile $file */
69+
/** @var string $fileId */
7070
foreach (array_keys($package->getMap()) as $fileId) {
7171
$filePath = str_replace(\Magento\Framework\View\Asset\Repository::FILE_ID_SEPARATOR, '/', $fileId);
7272
// phpcs:ignore Magento2.Functions.DiscouragedFunction
@@ -227,7 +227,9 @@ private function getValidExternalUrl($url, Package $package)
227227
if (!$this->isFileExistsInPackage($filePath, $package)) {
228228
/** @var PackageFile $matchedFile */
229229
$matchedFile = $this->getFileFromParent($filePath, $package);
230-
$package = $matchedFile->getPackage();
230+
if ($matchedFile && $matchedFile->getPackage()) {
231+
$package = $matchedFile->getPackage();
232+
}
231233
}
232234
return preg_replace(
233235
'/(?<=}})(.*)(?=\/{{)/',

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2011 Adobe
4+
* All rights reserved.
55
*/
66

77
namespace Magento\Framework\App\View\Asset;
@@ -49,19 +49,60 @@ public function __construct(
4949
}
5050

5151
/**
52+
* Publish the asset
53+
*
5254
* @param Asset\LocalInterface $asset
5355
* @return bool
5456
*/
5557
public function publish(Asset\LocalInterface $asset)
5658
{
5759
$dir = $this->filesystem->getDirectoryRead(DirectoryList::STATIC_VIEW);
58-
if ($dir->isExist($asset->getPath())) {
60+
$targetPath = $asset->getPath();
61+
62+
// Check if target file exists and is newer than source file
63+
if ($dir->isExist($targetPath) && !$this->isSourceFileNewer($asset, $dir, $targetPath)) {
5964
return true;
6065
}
6166

6267
return $this->publishAsset($asset);
6368
}
6469

70+
/**
71+
* Check if source file is newer than target file
72+
*
73+
* @param Asset\LocalInterface $asset
74+
* @param \Magento\Framework\Filesystem\Directory\ReadInterface $dir
75+
* @param string $targetPath
76+
* @return bool
77+
*/
78+
private function isSourceFileNewer(Asset\LocalInterface $asset, $dir, $targetPath)
79+
{
80+
$sourceFile = $asset->getSourceFile();
81+
82+
// Check if source file exists
83+
if (!file_exists($sourceFile)) {
84+
return false;
85+
}
86+
87+
$sourceMtime = $this->getFileModificationTime($sourceFile);
88+
$targetStat = $dir->stat($targetPath);
89+
$targetMtime = $targetStat['mtime'] ?? 0;
90+
91+
return $sourceMtime > $targetMtime;
92+
}
93+
94+
/**
95+
* Get file modification time
96+
*
97+
* @param string $filePath
98+
* @return int
99+
*/
100+
private function getFileModificationTime($filePath)
101+
{
102+
$mtime = @filemtime($filePath);
103+
return $mtime !== false ? $mtime : 0;
104+
}
105+
65106
/**
66107
* Publish the asset
67108
*

0 commit comments

Comments
 (0)