Skip to content

Add logic check only import css from enabled modules #32922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
31bd392
Add logic check only import css from enabled modules
mrtuvn May 1, 2021
6949d13
magento/magento2#32922 Add logic check only import css from enabled m…
ihor-sviziev May 19, 2021
c1347fa
magento/magento2#32922 Add logic check only import css from enabled m…
ihor-sviziev Sep 7, 2021
1bca471
magento/magento2#32922 Add logic check only import css from enabled m…
ihor-sviziev Sep 7, 2021
90948c3
Fix code style
mrtuvn Sep 7, 2021
46df6cd
Fix code style
mrtuvn Sep 7, 2021
551efbf
Fix code style
mrtuvn Sep 7, 2021
169c42d
Add logic check only import css from enabled modules
ihor-sviziev Sep 8, 2021
954d9d5
Update MagentoImport.php
mrtuvn Nov 2, 2021
3c13863
Update MagentoImport.php
mrtuvn Nov 2, 2021
0bfc522
Update MagentoImport.php
mrtuvn Nov 2, 2021
1f35247
Rework update logic import
tuna2smc May 8, 2022
e7def63
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
mrtuvn May 10, 2022
c7085d5
Rename method
tuna2smc May 11, 2022
3d93217
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
mrtuvn May 13, 2022
288d16e
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
ihor-sviziev May 27, 2022
addd68b
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
mrtuvn Dec 22, 2023
7daa881
Update MagentoImport.php
mrtuvn Dec 22, 2023
6dae70c
update logic check
mrtuvn Dec 25, 2023
b413793
Update MagentoImport.php
mrtuvn Dec 27, 2023
afb812a
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
ihor-sviziev Dec 27, 2023
f8fd657
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
engcom-Echo Apr 16, 2024
60b953d
Merge branch '2.4-develop' into add-logic-check-imports-less-for-enab…
engcom-Charlie Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,25 @@
* 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\DeploymentConfig;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Css\PreProcessor\ErrorHandlerInterface;
use Magento\Framework\Module\Manager as ModuleManager;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
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;
use Magento\Framework\View\Asset\PreProcessor\Chain;

/**
* @magento_import instruction preprocessor
Expand All @@ -27,6 +35,8 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
public const REPLACE_PATTERN =
'#//@magento_import(?P<reference>\s+\(reference\))?\s+[\'\"](?P<path>(?![/\\\]|\w:[/\\\])[^\"\']+)[\'\"]\s*?;#';

private const CONFIG_PATH_SCD_ONLY_ENABLED_MODULES = 'static_content_only_enabled_modules';

/**
* @var DesignInterface
*/
Expand All @@ -43,12 +53,12 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
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
* @see not used
*/
Expand All @@ -59,31 +69,47 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
*/
private $themeProvider;

/**
* @var DeploymentConfig
*/
private DeploymentConfig $deploymentConfig;

/**
* @var ModuleManager
*/
private ModuleManager $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 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
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->deploymentConfig = $deploymentConfig ?? ObjectManager::getInstance() ->get(DeploymentConfig::class);
$this->moduleManager = $moduleManager ?? ObjectManager::getInstance()->get(ModuleManager::class);
}

/**
* @inheritDoc
*/
public function process(\Magento\Framework\View\Asset\PreProcessor\Chain $chain)
public function process(Chain $chain)
{
$asset = $chain->getAsset();
$replaceCallback = function ($matchContent) use ($asset) {
Expand All @@ -108,24 +134,42 @@ 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->hasEnabledFlagDeployEnabledModules();
/** @var $importFile \Magento\Framework\View\File */
foreach ($importFiles as $importFile) {
$moduleName = $importFile->getModule();
$referenceString = $isReference ? '(reference) ' : '';
$importsContent .= $importFile->getModule()
? "@import $referenceString'{$importFile->getModule()}::{$resolvedPath}';\n"
: "@import $referenceString'{$matchedFileId}';\n";

if ($moduleName) {
if (!$deployOnlyEnabled || $this->moduleManager->isEnabled($moduleName)) {
$importsContent .= "@import $referenceString'{$moduleName}::{$resolvedPath}';\n";
}
} else {
$importsContent .= "@import $referenceString'{$matchedFileId}';\n";
}
}
} catch (\LogicException $e) {
$this->errorHandler->processException($e);
}

return $importsContent;
}

/**
* Retrieve flag deploy enabled modules
*
* @return bool
*/
private function hasEnabledFlagDeployEnabledModules(): bool
{
return (bool) $this->deploymentConfig->get(self::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES);
}

/**
* 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)
{
Expand All @@ -135,6 +179,7 @@ protected function getTheme(LocalInterface $asset)
$context->getAreaCode() . '/' . $context->getThemePath()
);
}

return $this->design->getDesignTheme();
}

Expand All @@ -143,7 +188,7 @@ protected function getTheme(LocalInterface $asset)
*
* @return ThemeProviderInterface
*/
private function getThemeProvider()
private function getThemeProvider(): ThemeProviderInterface
{
if (null === $this->themeProvider) {
$this->themeProvider = ObjectManager::getInstance()->get(ThemeProviderInterface::class);
Expand Down
Loading