3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
7
+
6
8
namespace Magento \Framework \Css \PreProcessor \Instruction ;
7
9
10
+ use Magento \Framework \App \DeploymentConfig ;
8
11
use Magento \Framework \App \ObjectManager ;
9
12
use Magento \Framework \Css \PreProcessor \ErrorHandlerInterface ;
13
+ use Magento \Framework \Module \Manager as ModuleManager ;
10
14
use Magento \Framework \ObjectManager \ResetAfterRequestInterface ;
11
15
use Magento \Framework \View \Asset \File \FallbackContext ;
12
16
use Magento \Framework \View \Asset \LocalInterface ;
13
17
use Magento \Framework \View \Asset \PreProcessorInterface ;
18
+ use Magento \Framework \View \Asset \Repository as AssetRepository ;
19
+ use Magento \Framework \View \Design \Theme \ListInterface as ThemeListInterface ;
14
20
use Magento \Framework \View \Design \Theme \ThemeProviderInterface ;
21
+ use Magento \Framework \View \Design \ThemeInterface ;
15
22
use Magento \Framework \View \DesignInterface ;
16
23
use Magento \Framework \View \File \CollectorInterface ;
24
+ use Magento \Framework \View \Asset \PreProcessor \Chain ;
17
25
18
26
/**
19
27
* @magento_import instruction preprocessor
@@ -27,6 +35,8 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
27
35
public const REPLACE_PATTERN =
28
36
'#//@magento_import(?P<reference>\s+\(reference\))?\s+[ \'\"](?P<path>(?![/ \\\]|\w:[/ \\\])[^\" \']+)[ \'\"]\s*?;# ' ;
29
37
38
+ private const CONFIG_PATH_SCD_ONLY_ENABLED_MODULES = 'static_content_only_enabled_modules ' ;
39
+
30
40
/**
31
41
* @var DesignInterface
32
42
*/
@@ -43,12 +53,12 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
43
53
protected $ errorHandler ;
44
54
45
55
/**
46
- * @var \Magento\Framework\View\Asset\Repository
56
+ * @var AssetRepository
47
57
*/
48
58
protected $ assetRepo ;
49
59
50
60
/**
51
- * @var \Magento\Framework\View\Design\Theme\ListInterface
61
+ * @var ThemeListInterface
52
62
* @deprecated 100.0.2
53
63
* @see not used
54
64
*/
@@ -59,31 +69,47 @@ class MagentoImport implements PreProcessorInterface, ResetAfterRequestInterface
59
69
*/
60
70
private $ themeProvider ;
61
71
72
+ /**
73
+ * @var DeploymentConfig
74
+ */
75
+ private DeploymentConfig $ deploymentConfig ;
76
+
77
+ /**
78
+ * @var ModuleManager
79
+ */
80
+ private ModuleManager $ moduleManager ;
81
+
62
82
/**
63
83
* @param DesignInterface $design
64
84
* @param CollectorInterface $fileSource
65
85
* @param ErrorHandlerInterface $errorHandler
66
- * @param \Magento\Framework\View\Asset\Repository $assetRepo
67
- * @param \Magento\Framework\View\Design\Theme\ListInterface $themeList
86
+ * @param AssetRepository $assetRepo
87
+ * @param ThemeListInterface $themeList
88
+ * @param DeploymentConfig|null $deploymentConfig
89
+ * @param ModuleManager|null $moduleManager
68
90
*/
69
91
public function __construct (
70
92
DesignInterface $ design ,
71
93
CollectorInterface $ fileSource ,
72
94
ErrorHandlerInterface $ errorHandler ,
73
- \Magento \Framework \View \Asset \Repository $ assetRepo ,
74
- \Magento \Framework \View \Design \Theme \ListInterface $ themeList
95
+ AssetRepository $ assetRepo ,
96
+ ThemeListInterface $ themeList ,
97
+ ?DeploymentConfig $ deploymentConfig = null ,
98
+ ?ModuleManager $ moduleManager = null
75
99
) {
76
100
$ this ->design = $ design ;
77
101
$ this ->fileSource = $ fileSource ;
78
102
$ this ->errorHandler = $ errorHandler ;
79
103
$ this ->assetRepo = $ assetRepo ;
80
104
$ this ->themeList = $ themeList ;
105
+ $ this ->deploymentConfig = $ deploymentConfig ?? ObjectManager::getInstance () ->get (DeploymentConfig::class);
106
+ $ this ->moduleManager = $ moduleManager ?? ObjectManager::getInstance ()->get (ModuleManager::class);
81
107
}
82
108
83
109
/**
84
110
* @inheritDoc
85
111
*/
86
- public function process (\ Magento \ Framework \ View \ Asset \ PreProcessor \ Chain $ chain )
112
+ public function process (Chain $ chain )
87
113
{
88
114
$ asset = $ chain ->getAsset ();
89
115
$ replaceCallback = function ($ matchContent ) use ($ asset ) {
@@ -108,24 +134,42 @@ protected function replace(array $matchedContent, LocalInterface $asset)
108
134
$ relatedAsset = $ this ->assetRepo ->createRelated ($ matchedFileId , $ asset );
109
135
$ resolvedPath = $ relatedAsset ->getFilePath ();
110
136
$ importFiles = $ this ->fileSource ->getFiles ($ this ->getTheme ($ relatedAsset ), $ resolvedPath );
137
+ $ deployOnlyEnabled = $ this ->hasEnabledFlagDeployEnabledModules ();
111
138
/** @var $importFile \Magento\Framework\View\File */
112
139
foreach ($ importFiles as $ importFile ) {
140
+ $ moduleName = $ importFile ->getModule ();
113
141
$ referenceString = $ isReference ? '(reference) ' : '' ;
114
- $ importsContent .= $ importFile ->getModule ()
115
- ? "@import $ referenceString' {$ importFile ->getModule ()}:: {$ resolvedPath }'; \n"
116
- : "@import $ referenceString' {$ matchedFileId }'; \n" ;
142
+
143
+ if ($ moduleName ) {
144
+ if (!$ deployOnlyEnabled || $ this ->moduleManager ->isEnabled ($ moduleName )) {
145
+ $ importsContent .= "@import $ referenceString' {$ moduleName }:: {$ resolvedPath }'; \n" ;
146
+ }
147
+ } else {
148
+ $ importsContent .= "@import $ referenceString' {$ matchedFileId }'; \n" ;
149
+ }
117
150
}
118
151
} catch (\LogicException $ e ) {
119
152
$ this ->errorHandler ->processException ($ e );
120
153
}
154
+
121
155
return $ importsContent ;
122
156
}
123
157
158
+ /**
159
+ * Retrieve flag deploy enabled modules
160
+ *
161
+ * @return bool
162
+ */
163
+ private function hasEnabledFlagDeployEnabledModules (): bool
164
+ {
165
+ return (bool ) $ this ->deploymentConfig ->get (self ::CONFIG_PATH_SCD_ONLY_ENABLED_MODULES );
166
+ }
167
+
124
168
/**
125
169
* Get theme model based on the information from asset
126
170
*
127
171
* @param LocalInterface $asset
128
- * @return \Magento\Framework\View\Design\ ThemeInterface
172
+ * @return ThemeInterface
129
173
*/
130
174
protected function getTheme (LocalInterface $ asset )
131
175
{
@@ -135,6 +179,7 @@ protected function getTheme(LocalInterface $asset)
135
179
$ context ->getAreaCode () . '/ ' . $ context ->getThemePath ()
136
180
);
137
181
}
182
+
138
183
return $ this ->design ->getDesignTheme ();
139
184
}
140
185
@@ -143,7 +188,7 @@ protected function getTheme(LocalInterface $asset)
143
188
*
144
189
* @return ThemeProviderInterface
145
190
*/
146
- private function getThemeProvider ()
191
+ private function getThemeProvider (): ThemeProviderInterface
147
192
{
148
193
if (null === $ this ->themeProvider ) {
149
194
$ this ->themeProvider = ObjectManager::getInstance ()->get (ThemeProviderInterface::class);
0 commit comments