-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
Copy pathMinification.php
240 lines (217 loc) · 6.96 KB
/
Minification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Asset;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
/**
* Helper class for static files minification related processes.
* @api
* @since 100.0.2
*/
class Minification implements ResetAfterRequestInterface
{
/**
* XML path for asset minification configuration
*/
public const XML_PATH_MINIFICATION_ENABLED = 'dev/%s/minify_files';
public const XML_PATH_MINIFICATION_EXCLUDES = 'dev/%s/minify_exclude';
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var State
*/
private $appState;
/**
* @var string
*/
private $scope;
/**
* @var array
*/
private $configCache = [];
/**
* @param ScopeConfigInterface $scopeConfig
* @param State $appState
* @param string $scope
*/
public function __construct(ScopeConfigInterface $scopeConfig, State $appState, $scope = 'store')
{
$this->scopeConfig = $scopeConfig;
$this->appState = $appState;
$this->scope = $scope;
}
/**
* Check whether asset minification is on for specified content type
*
* @param string $contentType
* @return bool
*/
public function isEnabled($contentType)
{
if (!isset($this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType])) {
$this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType] =
$this->appState->getMode() != State::MODE_DEVELOPER &&
$this->scopeConfig->isSetFlag(
sprintf(self::XML_PATH_MINIFICATION_ENABLED, $contentType),
$this->scope
);
}
return $this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType];
}
/**
* Add 'min' suffix if minification is enabled and $filename has no one.
*
* @param string $filename
* @return string
*/
public function addMinifiedSign($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($this->isEnabledForArea($filename) &&
!$this->isExcluded($filename) &&
!$this->isMinifiedFilename($filename)
) {
$filename = $filename !== null ? substr($filename, 0, -strlen($extension)) : '';
$filename = $filename . 'min.' . $extension;
}
return $filename;
}
/**
* Remove 'min' suffix if exists and minification is enabled
*
* @param string $filename
* @return string
*/
public function removeMinifiedSign($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($this->isEnabledForArea($filename) &&
!$this->isExcluded($filename) &&
$this->isMinifiedFilename($filename)
) {
$filename = $filename !== null ? substr($filename, 0, -strlen($extension) - 4) : '';
$filename = $filename . $extension;
}
return $filename;
}
/**
* Is Minified Filename
*
* @param string $filename
* @return bool
*/
public function isMinifiedFilename($filename)
{
return $filename && substr($filename, strrpos($filename, '.') - 4, 5) == '.min.';
}
/**
* Is Excluded
*
* @param string $filename
* @return boolean
*/
public function isExcluded($filename)
{
foreach ($this->getExcludes(pathinfo($filename, PATHINFO_EXTENSION)) as $exclude) {
if ($filename && preg_match('/' . str_replace('/', '\/', $exclude) . '/', $filename)) {
return true;
}
}
return false;
}
/**
* Get Excludes
*
* @param string $contentType
* @return string[]
*/
public function getExcludes($contentType)
{
if (!isset($this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType])) {
$this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType] = [];
$key = sprintf(self::XML_PATH_MINIFICATION_EXCLUDES, $contentType);
$excludeValues = $this->getMinificationExcludeValues($key);
foreach ($excludeValues as $exclude) {
if ($exclude && trim($exclude) != '') {
$this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType][] = trim($exclude);
}
}
}
return $this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType];
}
/**
* Get minification exclude values from configuration
*
* @param string $key
* @return string[]
*/
private function getMinificationExcludeValues($key)
{
$configValues = $this->scopeConfig->getValue($key, $this->scope) ?? [];
//value used to be a string separated by 'newline' separator so we need to convert it to array
if (!is_array($configValues)) {
$configValuesFromString = [];
foreach (explode("\n", $configValues) as $exclude) {
if ($exclude && trim($exclude) != '') {
$configValuesFromString[] = trim($exclude);
}
}
$configValues = $configValuesFromString;
}
return array_values($configValues);
}
/**
* Check whether asset minification is on for specified content type and for area
*
* @param string $filename
* @return bool
*/
private function isEnabledForArea(string $filename): bool
{
$area = $this->getAreaFromPath($filename);
$extension = pathinfo($filename, PATHINFO_EXTENSION);
if ($area !== 'adminhtml') {
$result = $this->isEnabled($extension);
} else {
$cacheConfigKey = $area . '_' . $extension;
if (!isset($this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$cacheConfigKey])) {
$this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$cacheConfigKey] =
$this->appState->getMode() != State::MODE_DEVELOPER &&
$this->scopeConfig->isSetFlag(
sprintf(self::XML_PATH_MINIFICATION_ENABLED, $extension),
'default'
);
}
$result = $this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$cacheConfigKey];
}
return $result;
}
/**
* Get area from the path
*
* @param string $filename
* @return string
*/
private function getAreaFromPath(string $filename): string
{
$area = '';
$pathParts = explode('/', $filename);
if (isset($pathParts[0])) {
$area = $pathParts[0];
}
return $area;
}
/**
* @inheritDoc
*/
public function _resetState(): void
{
$this->configCache = [];
}
}