Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
Minification Class Reference

Public Member Functions

 __construct (ScopeConfigInterface $scopeConfig, State $appState, $scope='store')
 
 isEnabled ($contentType)
 
 addMinifiedSign ($filename)
 
 removeMinifiedSign ($filename)
 
 isMinifiedFilename ($filename)
 
 isExcluded ($filename)
 
 getExcludes ($contentType)
 

Data Fields

const XML_PATH_MINIFICATION_ENABLED = 'dev/%s/minify_files'
 
const XML_PATH_MINIFICATION_EXCLUDES = 'dev/%s/minify_exclude'
 

Detailed Description

Helper class for static files minification related processes. @api

Since
100.0.2

Definition at line 16 of file Minification.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( ScopeConfigInterface  $scopeConfig,
State  $appState,
  $scope = 'store' 
)
Parameters
ScopeConfigInterface$scopeConfig
State$appState
string$scope

Definition at line 50 of file Minification.php.

51  {
52  $this->scopeConfig = $scopeConfig;
53  $this->appState = $appState;
54  $this->scope = $scope;
55  }

Member Function Documentation

◆ addMinifiedSign()

addMinifiedSign (   $filename)

Add 'min' suffix if minification is enabled and $filename has no one.

Parameters
string$filename
Returns
string

Definition at line 83 of file Minification.php.

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  }

◆ getExcludes()

getExcludes (   $contentType)
Parameters
string$contentType
Returns
string[]

Definition at line 142 of file Minification.php.

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  }

◆ isEnabled()

isEnabled (   $contentType)

Check whether asset minification is on for specified content type

Parameters
string$contentType
Returns
bool

Definition at line 63 of file Minification.php.

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  }

◆ isExcluded()

isExcluded (   $filename)
Parameters
string$filename
Returns
boolean

Definition at line 128 of file Minification.php.

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  }

◆ isMinifiedFilename()

isMinifiedFilename (   $filename)
Parameters
string$filename
Returns
bool

Definition at line 119 of file Minification.php.

120  {
121  return substr($filename, strrpos($filename, '.') - 4, 5) == '.min.';
122  }

◆ removeMinifiedSign()

removeMinifiedSign (   $filename)

Remove 'min' suffix if exists and minification is enabled

Parameters
string$filename
Returns
string

Definition at line 102 of file Minification.php.

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  }

Field Documentation

◆ XML_PATH_MINIFICATION_ENABLED

const XML_PATH_MINIFICATION_ENABLED = 'dev/%s/minify_files'

XML path for asset minification configuration

Definition at line 21 of file Minification.php.

◆ XML_PATH_MINIFICATION_EXCLUDES

const XML_PATH_MINIFICATION_EXCLUDES = 'dev/%s/minify_exclude'

Definition at line 23 of file Minification.php.


The documentation for this class was generated from the following file: