From 31bd3923935e8cf4db48c4f8770759228694bcd3 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Sat, 1 May 2021 10:38:22 +0700 Subject: [PATCH 01/16] Add logic check only import css from enabled modules --- .../Instruction/MagentoImport.php | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 4187650938bf9..ff03a5616aace 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -3,6 +3,8 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ +declare(strict_types=1); + namespace Magento\Framework\Css\PreProcessor\Instruction; use Magento\Framework\App\ObjectManager; @@ -57,25 +59,34 @@ class MagentoImport implements PreProcessorInterface */ private $themeProvider; + /** + * @var \Magento\Framework\Module\Manager + */ + private $moduleManager; + /** * @param DesignInterface $design * @param CollectorInterface $fileSource * @param ErrorHandlerInterface $errorHandler * @param \Magento\Framework\View\Asset\Repository $assetRepo * @param \Magento\Framework\View\Design\Theme\ListInterface $themeList + * @param \Magento\Framework\Module\Manager|null $moduleManager */ public function __construct( DesignInterface $design, CollectorInterface $fileSource, ErrorHandlerInterface $errorHandler, \Magento\Framework\View\Asset\Repository $assetRepo, - \Magento\Framework\View\Design\Theme\ListInterface $themeList + \Magento\Framework\View\Design\Theme\ListInterface $themeList, + ?\Magento\Framework\Module\Manager $moduleManager = null ) { $this->design = $design; $this->fileSource = $fileSource; $this->errorHandler = $errorHandler; $this->assetRepo = $assetRepo; $this->themeList = $themeList; + $this->moduleManager = $moduleManager + ?? ObjectManager::getInstance()->get(\Magento\Framework\Module\Manager::class); } /** @@ -108,9 +119,16 @@ protected function replace(array $matchedContent, LocalInterface $asset) $importFiles = $this->fileSource->getFiles($this->getTheme($relatedAsset), $resolvedPath); /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { + $moduleName = $importFile->getModule(); + + if ($moduleName && + !$this->moduleManager->isEnabled($moduleName)) { + continue; + } + $referenceString = $isReference ? '(reference) ' : ''; - $importsContent .= $importFile->getModule() - ? "@import $referenceString'{$importFile->getModule()}::{$resolvedPath}';\n" + $importsContent .= $moduleName + ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" : "@import $referenceString'{$matchedFileId}';\n"; } } catch (\LogicException $e) { From 6949d136352132766418ba4bdc97dea43ec44048 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Wed, 19 May 2021 12:13:52 +0300 Subject: [PATCH 02/16] magento/magento2#32922 Add logic check only import css from enabled modules Add test coverage --- .../Instruction/MagentoImportTest.php | 138 +++++++++++++----- 1 file changed, 104 insertions(+), 34 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php index 0192ca4572d03..9542de7c61a96 100644 --- a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php +++ b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php @@ -10,6 +10,7 @@ use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface; use Magento\Framework\Css\PreProcessor\Instruction\Import; use Magento\Framework\Css\PreProcessor\Instruction\MagentoImport; +use Magento\Framework\Module\Manager as ModuleManager; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; use Magento\Framework\View\Asset\File; use Magento\Framework\View\Asset\File\FallbackContext; @@ -30,32 +31,37 @@ class MagentoImportTest extends TestCase /** * @var DesignInterface|MockObject */ - private $design; + private $designMock; /** * @var CollectorInterface|MockObject */ - private $fileSource; + private $fileSourceMock; /** * @var ErrorHandlerInterface|MockObject */ - private $errorHandler; + private $errorHandlerMock; /** * @var File|MockObject */ - private $asset; + private $assetMock; /** * @var Repository|MockObject */ - private $assetRepo; + private $assetRepoMock; /** * @var ThemeProviderInterface|MockObject */ - private $themeProvider; + private $themeProviderMock; + + /** + * @var ModuleManager|MockObject + */ + private $moduleManagerMock; /** * @var Import @@ -64,21 +70,23 @@ class MagentoImportTest extends TestCase protected function setUp(): void { - $this->design = $this->getMockForAbstractClass(DesignInterface::class); - $this->fileSource = $this->getMockForAbstractClass(CollectorInterface::class); - $this->errorHandler = $this->getMockForAbstractClass( - ErrorHandlerInterface::class - ); - $this->asset = $this->createMock(File::class); - $this->asset->expects($this->any())->method('getContentType')->willReturn('css'); - $this->assetRepo = $this->createMock(Repository::class); - $this->themeProvider = $this->getMockForAbstractClass(ThemeProviderInterface::class); + $this->designMock = $this->getMockForAbstractClass(DesignInterface::class); + $this->fileSourceMock = $this->getMockForAbstractClass(CollectorInterface::class); + $this->errorHandlerMock = $this->getMockForAbstractClass(ErrorHandlerInterface::class); + $this->assetMock = $this->createMock(File::class); + $this->assetMock->expects($this->any())->method('getContentType')->willReturn('css'); + $this->assetRepoMock = $this->createMock(Repository::class); + $this->themeProviderMock = $this->getMockForAbstractClass(ThemeProviderInterface::class); + $this->moduleManagerMock = $this->createMock(ModuleManager::class); + $this->object = (new ObjectManager($this))->getObject(MagentoImport::class, [ - 'design' => $this->design, - 'fileSource' => $this->fileSource, - 'errorHandler' => $this->errorHandler, - 'assetRepo' => $this->assetRepo, - 'themeProvider' => $this->themeProvider + 'design' => $this->designMock, + 'fileSource' => $this->fileSourceMock, + 'errorHandler' => $this->errorHandlerMock, + 'assetRepo' => $this->assetRepoMock, + 'moduleManager' => $this->moduleManagerMock, + // Mocking private property + 'themeProvider' => $this->themeProviderMock, ]); } @@ -88,24 +96,32 @@ protected function setUp(): void * @param string $resolvedPath * @param array $foundFiles * @param string $expectedContent + * @param array $enabledModules * * @dataProvider processDataProvider */ - public function testProcess($originalContent, $foundPath, $resolvedPath, $foundFiles, $expectedContent) + public function testProcess( + string $originalContent, + string $foundPath, + string $resolvedPath, + array $foundFiles, + string $expectedContent, + array $enabledModules + ): void { - $chain = new Chain($this->asset, $originalContent, 'css', 'path'); + $chain = new Chain($this->assetMock, $originalContent, 'css', 'path'); $relatedAsset = $this->createMock(File::class); $relatedAsset->expects($this->once()) ->method('getFilePath') ->willReturn($resolvedPath); $context = $this->createMock(FallbackContext::class); - $this->assetRepo->expects($this->once()) + $this->assetRepoMock->expects($this->once()) ->method('createRelated') - ->with($foundPath, $this->asset) + ->with($foundPath, $this->assetMock) ->willReturn($relatedAsset); $relatedAsset->expects($this->once())->method('getContext')->willReturn($context); $theme = $this->getMockForAbstractClass(ThemeInterface::class); - $this->themeProvider->expects($this->once())->method('getThemeByFullPath')->willReturn($theme); + $this->themeProviderMock->expects($this->once())->method('getThemeByFullPath')->willReturn($theme); $files = []; foreach ($foundFiles as $file) { $fileObject = $this->createMock(\Magento\Framework\View\File::class); @@ -117,10 +133,16 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF ->willReturn($file['filename']); $files[] = $fileObject; } - $this->fileSource->expects($this->once()) + $this->fileSourceMock->expects($this->once()) ->method('getFiles') ->with($theme, $resolvedPath) ->willReturn($files); + + $this->moduleManagerMock->expects($this->any())->method('isEnabled') + ->willReturnCallback(function ($moduleName) use ($enabledModules) { + return in_array($moduleName, $enabledModules, true); + }); + $this->object->process($chain); $this->assertEquals($expectedContent, $chain->getContent()); $this->assertEquals('css', $chain->getContentType()); @@ -129,7 +151,7 @@ public function testProcess($originalContent, $foundPath, $resolvedPath, $foundF /** * @return array */ - public function processDataProvider() + public function processDataProvider(): array { return [ 'non-modular notation' => [ @@ -141,6 +163,7 @@ public function processDataProvider() ['module' => null, 'filename' => 'theme/some/file.css'], ], "@import 'some/file.css';\n@import 'some/file.css';\n", + [], ], 'modular' => [ '//@magento_import "Magento_Module::some/file.css";', @@ -151,6 +174,29 @@ public function processDataProvider() ['module' => 'Magento_Two', 'filename' => 'some/file.css'], ], "@import 'Magento_Module::some/file.css';\n@import 'Magento_Two::some/file.css';\n", + ['Magento_Module', 'Magento_Two'], + ], + 'modular with disabled module' => [ + '//@magento_import "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + "@import 'Magento_Two::some/file.css';\n", + ['Magento_Two'], + ], + 'modular with disabled all modules' => [ + '//@magento_import "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + '', + [], ], 'non-modular reference notation' => [ '//@magento_import (reference) "some/file.css";', @@ -161,6 +207,7 @@ public function processDataProvider() ['module' => null, 'filename' => 'theme/some/file.css'], ], "@import (reference) 'some/file.css';\n@import (reference) 'some/file.css';\n", + [], ], 'modular reference' => [ '//@magento_import (reference) "Magento_Module::some/file.css";', @@ -172,35 +219,58 @@ public function processDataProvider() ], "@import (reference) 'Magento_Module::some/file.css';\n" . "@import (reference) 'Magento_Two::some/file.css';\n", + ['Magento_Module', 'Magento_Two'], + ], + 'modular reference with disabled module' => [ + '//@magento_import (reference) "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + "@import (reference) 'Magento_Module::some/file.css';\n", + ['Magento_Module'], + ], + 'modular reference with disabled all modules' => [ + '//@magento_import (reference) "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + '', + [], ], ]; } - public function testProcessNoImport() + public function testProcessNoImport(): void { $originalContent = 'color: #000000;'; $expectedContent = 'color: #000000;'; - $chain = new Chain($this->asset, $originalContent, 'css', 'orig'); - $this->assetRepo->expects($this->never()) + $chain = new Chain($this->assetMock, $originalContent, 'css', 'orig'); + $this->assetRepoMock->expects($this->never()) ->method('createRelated'); $this->object->process($chain); $this->assertEquals($expectedContent, $chain->getContent()); $this->assertEquals('css', $chain->getContentType()); } - public function testProcessException() + public function testProcessException(): void { $chain = new Chain( - $this->asset, + $this->assetMock, '//@magento_import "some/file.css";', 'css', 'path' ); $exception = new \LogicException('Error happened'); - $this->assetRepo->expects($this->once()) + $this->assetRepoMock->expects($this->once()) ->method('createRelated') ->willThrowException($exception); - $this->errorHandler->expects($this->once()) + $this->errorHandlerMock->expects($this->once()) ->method('processException') ->with($exception); $this->object->process($chain); From c1347fa8dac2e3c147cadde7f86881d2dac58def Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Tue, 7 Sep 2021 17:17:45 +0300 Subject: [PATCH 03/16] magento/magento2#32922 Add logic check only import css from enabled modules Make deploying only enabled modules with flag static_content_only_enabled_modules --- .../Instruction/MagentoImport.php | 42 ++++++++++++------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index ff03a5616aace..0b7e3e53bbbe2 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -7,12 +7,17 @@ namespace Magento\Framework\Css\PreProcessor\Instruction; +use Magento\Framework\App\DeploymentConfig; use Magento\Framework\App\ObjectManager; use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface; +use Magento\Framework\Module\Manager as ModuleManager; use Magento\Framework\View\Asset\File\FallbackContext; use Magento\Framework\View\Asset\LocalInterface; use Magento\Framework\View\Asset\PreProcessorInterface; +use Magento\Framework\View\Asset\Repository as AssetRepository; +use Magento\Framework\View\Design\Theme\ListInterface as ThemeListInterface; use Magento\Framework\View\Design\Theme\ThemeProviderInterface; +use Magento\Framework\View\Design\ThemeInterface; use Magento\Framework\View\DesignInterface; use Magento\Framework\View\File\CollectorInterface; @@ -28,6 +33,8 @@ class MagentoImport implements PreProcessorInterface const REPLACE_PATTERN = '#//@magento_import(?P\s+\(reference\))?\s+[\'\"](?P(?![/\\\]|\w:[/\\\])[^\"\']+)[\'\"]\s*?;#'; + private const CONFIG_PATH_SCD_ONLY_ENABLED_MODULES = 'static_content_only_enabled_modules'; + /** * @var DesignInterface */ @@ -44,12 +51,12 @@ class MagentoImport implements PreProcessorInterface protected $errorHandler; /** - * @var \Magento\Framework\View\Asset\Repository + * @var AssetRepository */ protected $assetRepo; /** - * @var \Magento\Framework\View\Design\Theme\ListInterface + * @var ThemeListInterface * @deprecated 100.0.2 */ protected $themeList; @@ -60,7 +67,12 @@ class MagentoImport implements PreProcessorInterface private $themeProvider; /** - * @var \Magento\Framework\Module\Manager + * @var DeploymentConfig + */ + private $deploymentConfig; + + /** + * @var ModuleManager */ private $moduleManager; @@ -68,25 +80,27 @@ class MagentoImport implements PreProcessorInterface * @param DesignInterface $design * @param CollectorInterface $fileSource * @param ErrorHandlerInterface $errorHandler - * @param \Magento\Framework\View\Asset\Repository $assetRepo - * @param \Magento\Framework\View\Design\Theme\ListInterface $themeList - * @param \Magento\Framework\Module\Manager|null $moduleManager + * @param AssetRepository $assetRepo + * @param ThemeListInterface $themeList + * @param DeploymentConfig|null $deploymentConfig + * @param ModuleManager|null $moduleManager */ public function __construct( DesignInterface $design, CollectorInterface $fileSource, ErrorHandlerInterface $errorHandler, - \Magento\Framework\View\Asset\Repository $assetRepo, - \Magento\Framework\View\Design\Theme\ListInterface $themeList, - ?\Magento\Framework\Module\Manager $moduleManager = null + AssetRepository $assetRepo, + ThemeListInterface $themeList, + ?DeploymentConfig $deploymentConfig = null, + ?ModuleManager $moduleManager = null ) { $this->design = $design; $this->fileSource = $fileSource; $this->errorHandler = $errorHandler; $this->assetRepo = $assetRepo; $this->themeList = $themeList; - $this->moduleManager = $moduleManager - ?? ObjectManager::getInstance()->get(\Magento\Framework\Module\Manager::class); + $this->deploymentConfig = $deploymentConfig ?? ObjectManager::getInstance() ->get(DeploymentConfig::class); + $this->moduleManager = $moduleManager ?? ObjectManager::getInstance()->get(ModuleManager::class); } /** @@ -117,12 +131,12 @@ protected function replace(array $matchedContent, LocalInterface $asset) $relatedAsset = $this->assetRepo->createRelated($matchedFileId, $asset); $resolvedPath = $relatedAsset->getFilePath(); $importFiles = $this->fileSource->getFiles($this->getTheme($relatedAsset), $resolvedPath); + $deployOnlyEnabled = (bool)$this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES); /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); - if ($moduleName && - !$this->moduleManager->isEnabled($moduleName)) { + if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { continue; } @@ -141,7 +155,7 @@ protected function replace(array $matchedContent, LocalInterface $asset) * Get theme model based on the information from asset * * @param LocalInterface $asset - * @return \Magento\Framework\View\Design\ThemeInterface + * @return ThemeInterface */ protected function getTheme(LocalInterface $asset) { From 1bca47169af57cf16457bc0fe7ffc3b86b2c6a13 Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Tue, 7 Sep 2021 17:35:49 +0300 Subject: [PATCH 04/16] magento/magento2#32922 Add logic check only import css from enabled modules Update tests --- .../Instruction/MagentoImportTest.php | 78 ++++++++++++++++--- 1 file changed, 66 insertions(+), 12 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php index 9542de7c61a96..dd5ea772013d2 100644 --- a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php +++ b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php @@ -7,6 +7,7 @@ namespace Magento\Framework\Css\Test\Unit\PreProcessor\Instruction; +use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface; use Magento\Framework\Css\PreProcessor\Instruction\Import; use Magento\Framework\Css\PreProcessor\Instruction\MagentoImport; @@ -16,6 +17,7 @@ use Magento\Framework\View\Asset\File\FallbackContext; use Magento\Framework\View\Asset\PreProcessor\Chain; use Magento\Framework\View\Asset\Repository; +use Magento\Framework\View\Design\Theme\ListInterface as ThemeListInterface; use Magento\Framework\View\Design\Theme\ThemeProviderInterface; use Magento\Framework\View\Design\ThemeInterface; use Magento\Framework\View\DesignInterface; @@ -54,15 +56,24 @@ class MagentoImportTest extends TestCase private $assetRepoMock; /** - * @var ThemeProviderInterface|MockObject + * @var ThemeListInterface|MockObject */ - private $themeProviderMock; + private $themeListMock; + + /** + * @var DeploymentConfig|MockObject + */ + private $deploymentConfigMock; /** * @var ModuleManager|MockObject */ private $moduleManagerMock; + /** + * @var ThemeProviderInterface|MockObject + */ + private $themeProviderMock; /** * @var Import */ @@ -70,20 +81,25 @@ class MagentoImportTest extends TestCase protected function setUp(): void { - $this->designMock = $this->getMockForAbstractClass(DesignInterface::class); - $this->fileSourceMock = $this->getMockForAbstractClass(CollectorInterface::class); - $this->errorHandlerMock = $this->getMockForAbstractClass(ErrorHandlerInterface::class); + $this->designMock = $this->createMock(DesignInterface::class); + $this->fileSourceMock = $this->createMock(CollectorInterface::class); + $this->errorHandlerMock = $this->createMock(ErrorHandlerInterface::class); $this->assetMock = $this->createMock(File::class); - $this->assetMock->expects($this->any())->method('getContentType')->willReturn('css'); + $this->assetMock->method('getContentType')->willReturn('css'); $this->assetRepoMock = $this->createMock(Repository::class); - $this->themeProviderMock = $this->getMockForAbstractClass(ThemeProviderInterface::class); + $this->themeListMock = $this->createMock(ThemeListInterface::class); + $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class); $this->moduleManagerMock = $this->createMock(ModuleManager::class); + $this->themeProviderMock = $this->createMock(ThemeProviderInterface::class); + $this->object = (new ObjectManager($this))->getObject(MagentoImport::class, [ 'design' => $this->designMock, 'fileSource' => $this->fileSourceMock, 'errorHandler' => $this->errorHandlerMock, 'assetRepo' => $this->assetRepoMock, + 'themeList' => $this->themeListMock, + 'deploymentConfig' => $this->deploymentConfigMock, 'moduleManager' => $this->moduleManagerMock, // Mocking private property 'themeProvider' => $this->themeProviderMock, @@ -97,7 +113,7 @@ protected function setUp(): void * @param array $foundFiles * @param string $expectedContent * @param array $enabledModules - * + * @param bool $onlyEnabled * @dataProvider processDataProvider */ public function testProcess( @@ -106,7 +122,8 @@ public function testProcess( string $resolvedPath, array $foundFiles, string $expectedContent, - array $enabledModules + array $enabledModules, + bool $onlyEnabled ): void { $chain = new Chain($this->assetMock, $originalContent, 'css', 'path'); @@ -125,10 +142,10 @@ public function testProcess( $files = []; foreach ($foundFiles as $file) { $fileObject = $this->createMock(\Magento\Framework\View\File::class); - $fileObject->expects($this->any()) + $fileObject ->method('getModule') ->willReturn($file['module']); - $fileObject->expects($this->any()) + $fileObject ->method('getFilename') ->willReturn($file['filename']); $files[] = $fileObject; @@ -138,7 +155,10 @@ public function testProcess( ->with($theme, $resolvedPath) ->willReturn($files); - $this->moduleManagerMock->expects($this->any())->method('isEnabled') + $this->deploymentConfigMock->method('get')->with('static_content_only_enabled_modules') + ->willReturn($onlyEnabled); + + $this->moduleManagerMock->method('isEnabled') ->willReturnCallback(function ($moduleName) use ($enabledModules) { return in_array($moduleName, $enabledModules, true); }); @@ -164,6 +184,7 @@ public function processDataProvider(): array ], "@import 'some/file.css';\n@import 'some/file.css';\n", [], + true ], 'modular' => [ '//@magento_import "Magento_Module::some/file.css";', @@ -175,6 +196,7 @@ public function processDataProvider(): array ], "@import 'Magento_Module::some/file.css';\n@import 'Magento_Two::some/file.css';\n", ['Magento_Module', 'Magento_Two'], + true ], 'modular with disabled module' => [ '//@magento_import "Magento_Module::some/file.css";', @@ -186,6 +208,7 @@ public function processDataProvider(): array ], "@import 'Magento_Two::some/file.css';\n", ['Magento_Two'], + true ], 'modular with disabled all modules' => [ '//@magento_import "Magento_Module::some/file.css";', @@ -197,6 +220,7 @@ public function processDataProvider(): array ], '', [], + true ], 'non-modular reference notation' => [ '//@magento_import (reference) "some/file.css";', @@ -208,6 +232,7 @@ public function processDataProvider(): array ], "@import (reference) 'some/file.css';\n@import (reference) 'some/file.css';\n", [], + true ], 'modular reference' => [ '//@magento_import (reference) "Magento_Module::some/file.css";', @@ -220,6 +245,7 @@ public function processDataProvider(): array "@import (reference) 'Magento_Module::some/file.css';\n" . "@import (reference) 'Magento_Two::some/file.css';\n", ['Magento_Module', 'Magento_Two'], + true ], 'modular reference with disabled module' => [ '//@magento_import (reference) "Magento_Module::some/file.css";', @@ -231,6 +257,20 @@ public function processDataProvider(): array ], "@import (reference) 'Magento_Module::some/file.css';\n", ['Magento_Module'], + true + ], + 'modular reference with disabled module and disabled "only enabled modules" flag' => [ + '//@magento_import (reference) "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + "@import (reference) 'Magento_Module::some/file.css';\n" . + "@import (reference) 'Magento_Two::some/file.css';\n", + ['Magento_Module'], + false ], 'modular reference with disabled all modules' => [ '//@magento_import (reference) "Magento_Module::some/file.css";', @@ -242,6 +282,20 @@ public function processDataProvider(): array ], '', [], + true + ], + 'modular reference with disabled all modules and disabled "only enabled modules" flag' => [ + '//@magento_import (reference) "Magento_Module::some/file.css";', + 'Magento_Module::some/file.css', + 'some/file.css', + [ + ['module' => 'Magento_Module', 'filename' => 'some/file.css'], + ['module' => 'Magento_Two', 'filename' => 'some/file.css'], + ], + "@import (reference) 'Magento_Module::some/file.css';\n" . + "@import (reference) 'Magento_Two::some/file.css';\n", + [], + false ], ]; } From 90948c3ffb8aa43bc3989cfd4f3415c5d5fbf192 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Wed, 8 Sep 2021 06:45:00 +0700 Subject: [PATCH 05/16] Fix code style --- .../PreProcessor/Instruction/MagentoImport.php | 16 +++++++++++----- .../Instruction/MagentoImportTest.php | 3 +-- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 0b7e3e53bbbe2..caa13d04df560 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -20,6 +20,7 @@ use Magento\Framework\View\Design\ThemeInterface; use Magento\Framework\View\DesignInterface; use Magento\Framework\View\File\CollectorInterface; +use Magento\Framework\View\Asset\PreProcessor\Chain; /** * @magento_import instruction preprocessor @@ -104,9 +105,12 @@ public function __construct( } /** - * {@inheritdoc} + * Transform content and/or content type for the specified preprocessing chain object + * + * @param Chain $chain + * @return void */ - public function process(\Magento\Framework\View\Asset\PreProcessor\Chain $chain) + public function process(Chain $chain): void { $asset = $chain->getAsset(); $replaceCallback = function ($matchContent) use ($asset) { @@ -122,7 +126,7 @@ public function process(\Magento\Framework\View\Asset\PreProcessor\Chain $chain) * @param LocalInterface $asset * @return string */ - protected function replace(array $matchedContent, LocalInterface $asset) + protected function replace(array $matchedContent, LocalInterface $asset): string { $importsContent = ''; try { @@ -157,7 +161,7 @@ protected function replace(array $matchedContent, LocalInterface $asset) * @param LocalInterface $asset * @return ThemeInterface */ - protected function getTheme(LocalInterface $asset) + protected function getTheme(LocalInterface $asset): ThemeInterface { $context = $asset->getContext(); if ($context instanceof FallbackContext) { @@ -169,10 +173,12 @@ protected function getTheme(LocalInterface $asset) } /** + * Retrieve theme provider instance + * * @return ThemeProviderInterface * @deprecated 100.1.1 */ - private function getThemeProvider() + private function getThemeProvider(): ThemeProviderInterface { if (null === $this->themeProvider) { $this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class); diff --git a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php index dd5ea772013d2..5f50011cb5e66 100644 --- a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php +++ b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php @@ -124,8 +124,7 @@ public function testProcess( string $expectedContent, array $enabledModules, bool $onlyEnabled - ): void - { + ): void { $chain = new Chain($this->assetMock, $originalContent, 'css', 'path'); $relatedAsset = $this->createMock(File::class); $relatedAsset->expects($this->once()) From 46df6cd08b0239e0acaa02750e185373bd64f23d Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Wed, 8 Sep 2021 06:48:52 +0700 Subject: [PATCH 06/16] Fix code style --- .../Framework/Css/PreProcessor/Instruction/MagentoImport.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index caa13d04df560..41d923b3863ec 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -169,6 +169,7 @@ protected function getTheme(LocalInterface $asset): ThemeInterface $context->getAreaCode() . '/' . $context->getThemePath() ); } + return $this->design->getDesignTheme(); } From 551efbf806609d17e968d5956e3f4d532e5f5615 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Wed, 8 Sep 2021 06:49:26 +0700 Subject: [PATCH 07/16] Fix code style --- .../Framework/Css/PreProcessor/Instruction/MagentoImport.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 41d923b3863ec..b94cbadad70f4 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -152,6 +152,7 @@ protected function replace(array $matchedContent, LocalInterface $asset): string } catch (\LogicException $e) { $this->errorHandler->processException($e); } + return $importsContent; } From 169c42df677bfc6dd89eff4e546d94d26dab521b Mon Sep 17 00:00:00 2001 From: Ihor Sviziev Date: Wed, 8 Sep 2021 06:13:47 +0300 Subject: [PATCH 08/16] Add logic check only import css from enabled modules Fix static tests --- .../Css/PreProcessor/Instruction/MagentoImport.php | 6 +++--- .../Unit/PreProcessor/Instruction/MagentoImportTest.php | 1 + 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index b94cbadad70f4..e3df9109f3f70 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -110,7 +110,7 @@ public function __construct( * @param Chain $chain * @return void */ - public function process(Chain $chain): void + public function process(Chain $chain) { $asset = $chain->getAsset(); $replaceCallback = function ($matchContent) use ($asset) { @@ -126,7 +126,7 @@ public function process(Chain $chain): void * @param LocalInterface $asset * @return string */ - protected function replace(array $matchedContent, LocalInterface $asset): string + protected function replace(array $matchedContent, LocalInterface $asset) { $importsContent = ''; try { @@ -162,7 +162,7 @@ protected function replace(array $matchedContent, LocalInterface $asset): string * @param LocalInterface $asset * @return ThemeInterface */ - protected function getTheme(LocalInterface $asset): ThemeInterface + protected function getTheme(LocalInterface $asset) { $context = $asset->getContext(); if ($context instanceof FallbackContext) { diff --git a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php index 5f50011cb5e66..9ac37f7b96e2c 100644 --- a/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php +++ b/lib/internal/Magento/Framework/Css/Test/Unit/PreProcessor/Instruction/MagentoImportTest.php @@ -168,6 +168,7 @@ public function testProcess( } /** + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) * @return array */ public function processDataProvider(): array From 954d9d5a1cfc81bbc3ba88b0dfacee7c61182878 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Tue, 2 Nov 2021 07:28:01 +0700 Subject: [PATCH 09/16] Update MagentoImport.php Update check import --- .../Css/PreProcessor/Instruction/MagentoImport.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index e3df9109f3f70..61c61b03cd2ea 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -139,15 +139,17 @@ protected function replace(array $matchedContent, LocalInterface $asset) /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); - - if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { + + if ($moduleName && !$deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { continue; } - $referenceString = $isReference ? '(reference) ' : ''; - $importsContent .= $moduleName - ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" - : "@import $referenceString'{$matchedFileId}';\n"; + if ($moduleName && $deployOnlyEnabled && $this->moduleManager->isEnabled($moduleName)) { + $referenceString = $isReference ? '(reference) ' : ''; + $importsContent .= $moduleName + ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" + : "@import $referenceString'{$matchedFileId}';\n"; + } } } catch (\LogicException $e) { $this->errorHandler->processException($e); From 3c13863624418d885b6892c43a63cbb7d0dbe6f5 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Tue, 2 Nov 2021 07:35:47 +0700 Subject: [PATCH 10/16] Update MagentoImport.php Update import check --- .../Css/PreProcessor/Instruction/MagentoImport.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 61c61b03cd2ea..416dae102f551 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -143,13 +143,15 @@ protected function replace(array $matchedContent, LocalInterface $asset) if ($moduleName && !$deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { continue; } - - if ($moduleName && $deployOnlyEnabled && $this->moduleManager->isEnabled($moduleName)) { - $referenceString = $isReference ? '(reference) ' : ''; - $importsContent .= $moduleName - ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" - : "@import $referenceString'{$matchedFileId}';\n"; + + if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { + continue; } + + $referenceString = $isReference ? '(reference) ' : ''; + $importsContent .= $moduleName + ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" + : "@import $referenceString'{$matchedFileId}';\n"; } } catch (\LogicException $e) { $this->errorHandler->processException($e); From 0bfc522d9ec34d38ffab717f4d05fdd41db43b2d Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Tue, 2 Nov 2021 15:47:14 +0700 Subject: [PATCH 11/16] Update MagentoImport.php Update logic check --- .../PreProcessor/Instruction/MagentoImport.php | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 416dae102f551..7dc206ae1c2e6 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -139,19 +139,17 @@ protected function replace(array $matchedContent, LocalInterface $asset) /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); - - if ($moduleName && !$deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { + + if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { continue; } - if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { - continue; + if (!$deployOnlyEnabled || ($moduleName && $deployOnlyEnabled && $this->moduleManager->isEnabled($moduleName))) { + $referenceString = $isReference ? '(reference) ' : ''; + $importsContent .= $moduleName + ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" + : "@import $referenceString'{$matchedFileId}';\n"; } - - $referenceString = $isReference ? '(reference) ' : ''; - $importsContent .= $moduleName - ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" - : "@import $referenceString'{$matchedFileId}';\n"; } } catch (\LogicException $e) { $this->errorHandler->processException($e); From 1f35247f202fcc5abaddf1a7ffba6cbbff375456 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Anh Date: Sun, 8 May 2022 08:05:44 +0700 Subject: [PATCH 12/16] Rework update logic import --- .../Instruction/MagentoImport.php | 31 +++++++++++++++---- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 7dc206ae1c2e6..3cb428d77dca2 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -135,21 +135,30 @@ protected function replace(array $matchedContent, LocalInterface $asset) $relatedAsset = $this->assetRepo->createRelated($matchedFileId, $asset); $resolvedPath = $relatedAsset->getFilePath(); $importFiles = $this->fileSource->getFiles($this->getTheme($relatedAsset), $resolvedPath); - $deployOnlyEnabled = (bool)$this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES); + $deployOnlyEnabled = $this->getStaticDeployEnabledModulesFlag(); /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); - if ($moduleName && $deployOnlyEnabled && !$this->moduleManager->isEnabled($moduleName)) { - continue; - } - - if (!$deployOnlyEnabled || ($moduleName && $deployOnlyEnabled && $this->moduleManager->isEnabled($moduleName))) { + if (!$deployOnlyEnabled) { $referenceString = $isReference ? '(reference) ' : ''; $importsContent .= $moduleName ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" : "@import $referenceString'{$matchedFileId}';\n"; } + + if ($deployOnlyEnabled) { + if ($moduleName && !$this->moduleManager->isEnabled($moduleName)) { + continue; + } + + if ($moduleName && $this->moduleManager->isEnabled($moduleName)) { + $referenceString = $isReference ? '(reference) ' : ''; + $importsContent .= $moduleName + ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" + : "@import $referenceString'{$matchedFileId}';\n"; + } + } } } catch (\LogicException $e) { $this->errorHandler->processException($e); @@ -158,6 +167,16 @@ protected function replace(array $matchedContent, LocalInterface $asset) return $importsContent; } + /** + * Retrieve flag deploy enabled modules + * + * @return bool + */ + protected function getStaticDeployEnabledModulesFlag() : bool + { + return (bool) $this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES); + } + /** * Get theme model based on the information from asset * From c7085d56cd49c3548c66ee5fe8d40cdaa80b08bc Mon Sep 17 00:00:00 2001 From: Tu Nguyen Anh Date: Thu, 12 May 2022 06:50:35 +0700 Subject: [PATCH 13/16] Rename method --- .../Css/PreProcessor/Instruction/MagentoImport.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 3cb428d77dca2..20acc4c769af0 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -31,7 +31,7 @@ class MagentoImport implements PreProcessorInterface /** * PCRE pattern that matches @magento_import instruction */ - const REPLACE_PATTERN = + public const REPLACE_PATTERN = '#//@magento_import(?P\s+\(reference\))?\s+[\'\"](?P(?![/\\\]|\w:[/\\\])[^\"\']+)[\'\"]\s*?;#'; private const CONFIG_PATH_SCD_ONLY_ENABLED_MODULES = 'static_content_only_enabled_modules'; @@ -135,7 +135,7 @@ protected function replace(array $matchedContent, LocalInterface $asset) $relatedAsset = $this->assetRepo->createRelated($matchedFileId, $asset); $resolvedPath = $relatedAsset->getFilePath(); $importFiles = $this->fileSource->getFiles($this->getTheme($relatedAsset), $resolvedPath); - $deployOnlyEnabled = $this->getStaticDeployEnabledModulesFlag(); + $deployOnlyEnabled = $this->hasEnabledFlagDeployEnabledModules(); /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); @@ -172,7 +172,7 @@ protected function replace(array $matchedContent, LocalInterface $asset) * * @return bool */ - protected function getStaticDeployEnabledModulesFlag() : bool + protected function hasEnabledFlagDeployEnabledModules(): bool { return (bool) $this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES); } From 7daa8816546eb2ee42d93fdb892fd3cba71c3de4 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Fri, 22 Dec 2023 14:30:03 +0700 Subject: [PATCH 14/16] Update MagentoImport.php --- .../Css/PreProcessor/Instruction/MagentoImport.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index b70c61b240e59..afe3d2af9233c 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -138,21 +138,26 @@ protected function replace(array $matchedContent, LocalInterface $asset) /** @var $importFile \Magento\Framework\View\File */ foreach ($importFiles as $importFile) { $moduleName = $importFile->getModule(); + $referenceString = $isReference ? '(reference) ' : ''; if (!$deployOnlyEnabled) { - $referenceString = $isReference ? '(reference) ' : ''; $importsContent .= $moduleName ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" : "@import $referenceString'{$matchedFileId}';\n"; } if ($deployOnlyEnabled) { - if ($moduleName && !$this->moduleManager->isEnabled($moduleName)) { + if (($moduleName && !$this->moduleManager->isEnabled($moduleName)) || + "" === ($matchedFileId) || + null === ($matchedFileId)) { continue; } + if (!$moduleName && !empty($matchedFileId)) { + $importsContent .= "@import $referenceString'{$matchedFileId}';\n"; + } + if ($moduleName && $this->moduleManager->isEnabled($moduleName)) { - $referenceString = $isReference ? '(reference) ' : ''; $importsContent .= $moduleName ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" : "@import $referenceString'{$matchedFileId}';\n"; From 6dae70c957ab36f01b26982db19113551e751c07 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Mon, 25 Dec 2023 11:39:09 +0700 Subject: [PATCH 15/16] update logic check --- .../Instruction/MagentoImport.php | 27 +++++++------------ 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index afe3d2af9233c..63abf3863c4bb 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -140,27 +140,20 @@ protected function replace(array $matchedContent, LocalInterface $asset) $moduleName = $importFile->getModule(); $referenceString = $isReference ? '(reference) ' : ''; - if (!$deployOnlyEnabled) { - $importsContent .= $moduleName - ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" - : "@import $referenceString'{$matchedFileId}';\n"; + if (!$moduleName) { + $importsContent .= "@import $referenceString'{$matchedFileId}';\n"; } - if ($deployOnlyEnabled) { - if (($moduleName && !$this->moduleManager->isEnabled($moduleName)) || - "" === ($matchedFileId) || - null === ($matchedFileId)) { - continue; - } + if ($moduleName) { + if (!$deployOnlyEnabled) { + $importsContent .= "@import $referenceString'{$moduleName}::{$resolvedPath}';\n"; + } else { + if (!$this->moduleManager->isEnabled($moduleName)) { + continue; + } - if (!$moduleName && !empty($matchedFileId)) { - $importsContent .= "@import $referenceString'{$matchedFileId}';\n"; - } + $importsContent .= "@import $referenceString'{$moduleName}::{$resolvedPath}';\n"; - if ($moduleName && $this->moduleManager->isEnabled($moduleName)) { - $importsContent .= $moduleName - ? "@import $referenceString'{$moduleName}::{$resolvedPath}';\n" - : "@import $referenceString'{$matchedFileId}';\n"; } } } From b413793fd661a165631ed3a32026ee98da703b87 Mon Sep 17 00:00:00 2001 From: Tu Nguyen Date: Wed, 27 Dec 2023 07:23:45 +0700 Subject: [PATCH 16/16] Update MagentoImport.php --- .../Instruction/MagentoImport.php | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php index 63abf3863c4bb..831fb29022a6c 100644 --- a/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php +++ b/lib/internal/Magento/Framework/Css/PreProcessor/Instruction/MagentoImport.php @@ -72,12 +72,12 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface /** * @var DeploymentConfig */ - private $deploymentConfig; + private DeploymentConfig $deploymentConfig; /** * @var ModuleManager */ - private $moduleManager; + private ModuleManager $moduleManager; /** * @param DesignInterface $design @@ -140,21 +140,12 @@ protected function replace(array $matchedContent, LocalInterface $asset) $moduleName = $importFile->getModule(); $referenceString = $isReference ? '(reference) ' : ''; - if (!$moduleName) { - $importsContent .= "@import $referenceString'{$matchedFileId}';\n"; - } - if ($moduleName) { - if (!$deployOnlyEnabled) { + if (!$deployOnlyEnabled || $this->moduleManager->isEnabled($moduleName)) { $importsContent .= "@import $referenceString'{$moduleName}::{$resolvedPath}';\n"; - } else { - if (!$this->moduleManager->isEnabled($moduleName)) { - continue; - } - - $importsContent .= "@import $referenceString'{$moduleName}::{$resolvedPath}';\n"; - } + } else { + $importsContent .= "@import $referenceString'{$matchedFileId}';\n"; } } } catch (\LogicException $e) { @@ -169,7 +160,7 @@ protected function replace(array $matchedContent, LocalInterface $asset) * * @return bool */ - protected function hasEnabledFlagDeployEnabledModules(): bool + private function hasEnabledFlagDeployEnabledModules(): bool { return (bool) $this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES); }