Skip to content
Snippets Groups Projects
Commit 66ce98f6 authored by Karpenko, Oleksandr's avatar Karpenko, Oleksandr
Browse files

MAGETWO-59053: [Github] Merge CSS Files causes entire site to load Extremely Slow #4321

parent bc9df94e
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
namespace Magento\Framework\View\Asset\MergeStrategy; namespace Magento\Framework\View\Asset\MergeStrategy;
use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Asset\Source;
/** /**
* Skip merging if all of the files that need to be merged were not modified * Skip merging if all of the files that need to be merged were not modified
...@@ -25,6 +27,11 @@ class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface ...@@ -25,6 +27,11 @@ class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface
*/ */
protected $filesystem; protected $filesystem;
/**
* @var Source
*/
private $assetSource;
/** /**
* @param \Magento\Framework\View\Asset\MergeStrategyInterface $strategy * @param \Magento\Framework\View\Asset\MergeStrategyInterface $strategy
* @param \Magento\Framework\Filesystem $filesystem * @param \Magento\Framework\Filesystem $filesystem
...@@ -37,17 +44,31 @@ class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface ...@@ -37,17 +44,31 @@ class Checksum implements \Magento\Framework\View\Asset\MergeStrategyInterface
$this->filesystem = $filesystem; $this->filesystem = $filesystem;
} }
/**
* @deprecated
* @return Source
*/
private function getAssetSource()
{
if (!$this->assetSource) {
$this->assetSource = ObjectManager::getInstance()->get(Source::class);
}
return $this->assetSource;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset) public function merge(array $assetsToMerge, \Magento\Framework\View\Asset\LocalInterface $resultAsset)
{ {
$sourceDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT); $rootDir = $this->filesystem->getDirectoryRead(DirectoryList::ROOT);
$mTime = null; $mTime = null;
/** @var \Magento\Framework\View\Asset\MergeableInterface $asset */ /** @var \Magento\Framework\View\Asset\MergeableInterface $asset */
foreach ($assetsToMerge as $asset) { foreach ($assetsToMerge as $asset) {
$mTime .= $sourceDir->stat($sourceDir->getRelativePath($asset->getSourceFile()))['mtime']; $sourceFile = $this->getAssetSource()->findSource($asset);
$mTime .= $rootDir->stat($rootDir->getRelativePath($sourceFile))['mtime'];
} }
if (null === $mTime) { if (null === $mTime) {
return; // nothing to merge return; // nothing to merge
} }
......
...@@ -8,6 +8,7 @@ namespace Magento\Framework\View\Test\Unit\Asset\MergeStrategy; ...@@ -8,6 +8,7 @@ namespace Magento\Framework\View\Test\Unit\Asset\MergeStrategy;
use \Magento\Framework\View\Asset\MergeStrategy\Checksum; use \Magento\Framework\View\Asset\MergeStrategy\Checksum;
use Magento\Framework\App\Filesystem\DirectoryList; use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\View\Asset\Source;
class ChecksumTest extends \PHPUnit_Framework_TestCase class ChecksumTest extends \PHPUnit_Framework_TestCase
{ {
...@@ -31,6 +32,11 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase ...@@ -31,6 +32,11 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase
*/ */
private $resultAsset; private $resultAsset;
/**
* @var Source|\PHPUnit_Framework_MockObject_MockObject
*/
private $assetSource;
/** /**
* @var \Magento\Framework\View\Asset\MergeStrategy\Checksum * @var \Magento\Framework\View\Asset\MergeStrategy\Checksum
*/ */
...@@ -53,6 +59,15 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase ...@@ -53,6 +59,15 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase
->with(DirectoryList::STATIC_VIEW) ->with(DirectoryList::STATIC_VIEW)
->will($this->returnValue($this->targetDir)); ->will($this->returnValue($this->targetDir));
$this->checksum = new Checksum($this->mergerMock, $filesystem); $this->checksum = new Checksum($this->mergerMock, $filesystem);
$this->assetSource = $this->getMockBuilder(Source::class)
->disableOriginalConstructor()
->getMock();
$reflection = new \ReflectionClass(Checksum::class);
$reflectionProperty = $reflection->getProperty('assetSource');
$reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($this->checksum, $this->assetSource);
$this->resultAsset = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false); $this->resultAsset = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false);
} }
...@@ -114,9 +129,13 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase ...@@ -114,9 +129,13 @@ class ChecksumTest extends \PHPUnit_Framework_TestCase
private function getAssetsToMerge() private function getAssetsToMerge()
{ {
$one = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false); $one = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false);
$one->expects($this->once())->method('getSourceFile')->will($this->returnValue('/dir/file/one.txt'));
$two = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false); $two = $this->getMock(\Magento\Framework\View\Asset\File::class, [], [], '', false);
$two->expects($this->once())->method('getSourceFile')->will($this->returnValue('/dir/file/two.txt'));
$this->assetSource->expects($this->exactly(2))
->method('findSource')
->withConsecutive([$one], [$two])
->willReturnOnConsecutiveCalls('/dir/file/one.txt', '/dir/file/two.txt');
$this->sourceDir->expects($this->exactly(2)) $this->sourceDir->expects($this->exactly(2))
->method('getRelativePath') ->method('getRelativePath')
->will($this->onConsecutiveCalls('file/one.txt', 'file/two.txt')); ->will($this->onConsecutiveCalls('file/one.txt', 'file/two.txt'));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment