Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Minification.php
Go to the documentation of this file.
1 <?php
7 
10 
17 {
21  const XML_PATH_MINIFICATION_ENABLED = 'dev/%s/minify_files';
22 
23  const XML_PATH_MINIFICATION_EXCLUDES = 'dev/%s/minify_exclude';
24 
28  private $scopeConfig;
29 
33  private $appState;
34 
38  private $scope;
39 
43  private $configCache = [];
44 
50  public function __construct(ScopeConfigInterface $scopeConfig, State $appState, $scope = 'store')
51  {
52  $this->scopeConfig = $scopeConfig;
53  $this->appState = $appState;
54  $this->scope = $scope;
55  }
56 
63  public function isEnabled($contentType)
64  {
65  if (!isset($this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType])) {
66  $this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType] =
67  $this->appState->getMode() != State::MODE_DEVELOPER &&
68  (bool)$this->scopeConfig->isSetFlag(
69  sprintf(self::XML_PATH_MINIFICATION_ENABLED, $contentType),
70  $this->scope
71  );
72  }
73 
74  return $this->configCache[self::XML_PATH_MINIFICATION_ENABLED][$contentType];
75  }
76 
83  public function addMinifiedSign($filename)
84  {
85  $extension = pathinfo($filename, PATHINFO_EXTENSION);
86 
87  if ($this->isEnabled($extension) &&
88  !$this->isExcluded($filename) &&
89  !$this->isMinifiedFilename($filename)
90  ) {
91  $filename = substr($filename, 0, -strlen($extension)) . 'min.' . $extension;
92  }
93  return $filename;
94  }
95 
102  public function removeMinifiedSign($filename)
103  {
104  $extension = pathinfo($filename, PATHINFO_EXTENSION);
105 
106  if ($this->isEnabled($extension) &&
107  !$this->isExcluded($filename) &&
108  $this->isMinifiedFilename($filename)
109  ) {
110  $filename = substr($filename, 0, -strlen($extension) - 4) . $extension;
111  }
112  return $filename;
113  }
114 
119  public function isMinifiedFilename($filename)
120  {
121  return substr($filename, strrpos($filename, '.') - 4, 5) == '.min.';
122  }
123 
128  public function isExcluded($filename)
129  {
130  foreach ($this->getExcludes(pathinfo($filename, PATHINFO_EXTENSION)) as $exclude) {
131  if (preg_match('/' . str_replace('/', '\/', $exclude) . '/', $filename)) {
132  return true;
133  }
134  }
135  return false;
136  }
137 
142  public function getExcludes($contentType)
143  {
144  if (!isset($this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType])) {
145  $this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType] = [];
146  $key = sprintf(self::XML_PATH_MINIFICATION_EXCLUDES, $contentType);
147  $excludeValues = $this->getMinificationExcludeValues($key);
148  foreach ($excludeValues as $exclude) {
149  if (trim($exclude) != '') {
150  $this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType][] = trim($exclude);
151  }
152  }
153  }
154  return $this->configCache[self::XML_PATH_MINIFICATION_EXCLUDES][$contentType];
155  }
156 
163  private function getMinificationExcludeValues($key)
164  {
165  $configValues = $this->scopeConfig->getValue($key, $this->scope) ?? [];
166  //value used to be a string separated by 'newline' separator so we need to convert it to array
167  if (!is_array($configValues)) {
168  $configValuesFromString = [];
169  foreach (explode("\n", $configValues) as $exclude) {
170  if (trim($exclude) != '') {
171  $configValuesFromString[] = trim($exclude);
172  }
173  }
174  $configValues = $configValuesFromString;
175  }
176  return array_values($configValues);
177  }
178 }
__construct(ScopeConfigInterface $scopeConfig, State $appState, $scope='store')