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

Public Member Functions

 __construct (\Magento\Framework\Filesystem\Directory\ReadFactory $readFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\App\Cache\StateInterface $cacheState, \Magento\Framework\Module\Dir\Reader $reader, VclGeneratorFactory $vclGeneratorFactory, Json $serializer=null)
 
 getType ()
 
 getTtl ()
 
 getVclFile ($vclTemplatePath)
 
 isEnabled ()
 

Data Fields

const BUILT_IN = 1
 
const VARNISH = 2
 
const XML_PAGECACHE_TTL = 'system/full_page_cache/ttl'
 
const XML_PAGECACHE_TYPE = 'system/full_page_cache/caching_application'
 
const XML_VARNISH_PAGECACHE_ACCESS_LIST = 'system/full_page_cache/varnish/access_list'
 
const XML_VARNISH_PAGECACHE_BACKEND_PORT = 'system/full_page_cache/varnish/backend_port'
 
const XML_VARNISH_PAGECACHE_BACKEND_HOST = 'system/full_page_cache/varnish/backend_host'
 
const XML_VARNISH_PAGECACHE_GRACE_PERIOD = 'system/full_page_cache/varnish/grace_period'
 
const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX = 'design/theme/ua_regexp'
 
const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path'
 
const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path'
 

Protected Member Functions

 _getReplacements ()
 
 _getAccessList ()
 
 _getDesignExceptions ()
 

Protected Attributes

 $_scopeConfig
 
 $_cacheState
 
 $readFactory
 
 $reader
 

Detailed Description

Model is responsible for replacing default vcl template file configuration with user-defined from configuration

@api

Since
100.0.2

Definition at line 21 of file Config.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Filesystem\Directory\ReadFactory  $readFactory,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Framework\App\Cache\StateInterface  $cacheState,
\Magento\Framework\Module\Dir\Reader  $reader,
VclGeneratorFactory  $vclGeneratorFactory,
Json  $serializer = null 
)
Parameters
Filesystem\Directory\ReadFactory$readFactory
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\App\Cache\StateInterface$cacheState
Dir\Reader$reader
VclGeneratorFactory$vclGeneratorFactory
Json | null$serializer

Definition at line 95 of file Config.php.

102  {
103  $this->readFactory = $readFactory;
104  $this->_scopeConfig = $scopeConfig;
105  $this->_cacheState = $cacheState;
106  $this->reader = $reader;
107  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
108  $this->vclGeneratorFactory = $vclGeneratorFactory;
109  }

Member Function Documentation

◆ _getAccessList()

_getAccessList ( )
protected

Get IPs access list that can purge Varnish configuration for config file generation and transform it to appropriate view

acl purge{ "127.0.0.1"; "127.0.0.2";

Returns
mixed|null|string
Deprecated:
100.2.0 see \Magento\PageCache\Model\VclGeneratorInterface::generateVcl

Definition at line 200 of file Config.php.

201  {
202  $result = '';
203  $tpl = " \"%s\";";
204  $accessList = $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_ACCESS_LIST);
205  if (!empty($accessList)) {
206  $result = [];
207  $ips = explode(',', $accessList);
208  foreach ($ips as $ip) {
209  $result[] = sprintf($tpl, trim($ip));
210  }
211  return implode("\n", $result);
212  }
213  return $result;
214  }

◆ _getDesignExceptions()

_getDesignExceptions ( )
protected

Get regexs for design exceptions Different browser user-agents may use different themes Varnish supports regex with internal modifiers only so we have to convert "/pattern/iU" into "(?Ui)pattern"

Returns
string
Deprecated:
100.2.0 see \Magento\PageCache\Model\VclGeneratorInterface::generateVcl

Definition at line 225 of file Config.php.

226  {
227  $result = '';
228  $tpl = "%s (req.http.user-agent ~ \"%s\") {\n" . " hash_data(\"%s\");\n" . " }";
229 
230  $expressions = $this->_scopeConfig->getValue(
231  self::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
232  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
233  );
234  if ($expressions) {
235  $rules = array_values($this->serializer->unserialize($expressions));
236  foreach ($rules as $i => $rule) {
237  if (preg_match('/^[\W]{1}(.*)[\W]{1}(\w+)?$/', $rule['regexp'], $matches)) {
238  if (!empty($matches[2])) {
239  $pattern = sprintf("(?%s)%s", $matches[2], $matches[1]);
240  } else {
241  $pattern = $matches[1];
242  }
243  $if = $i == 0 ? 'if' : ' elsif';
244  $result .= sprintf($tpl, $if, $pattern, $rule['value']);
245  }
246  }
247  }
248  return $result;
249  }
$pattern
Definition: website.php:22
$expressions
Definition: side-menu.phtml:10
$i
Definition: gallery.phtml:31

◆ _getReplacements()

_getReplacements ( )
protected

Prepare data for VCL config

Returns
array
Deprecated:
100.2.0 see \Magento\PageCache\Model\VclGeneratorInterface::generateVcl

Definition at line 170 of file Config.php.

171  {
172  return [
173  '/* {{ host }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_HOST),
174  '/* {{ port }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_PORT),
175  '/* {{ ips }} */' => $this->_getAccessList(),
176  '/* {{ design_exceptions_code }} */' => $this->_getDesignExceptions(),
177  // http headers get transformed by php `X-Forwarded-Proto: https`
178  // becomes $SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'
179  // Apache and Nginx drop all headers with underlines by default.
180  '/* {{ ssl_offloaded_header }} */' => str_replace(
181  '_',
182  '-',
183  $this->_scopeConfig->getValue(\Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER)
184  ),
185  '/* {{ grace_period }} */' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_GRACE_PERIOD)
186  ];
187  }

◆ getTtl()

getTtl ( )

Return page lifetime

Returns
int @api

Definition at line 128 of file Config.php.

129  {
130  return $this->_scopeConfig->getValue(self::XML_PAGECACHE_TTL);
131  }

◆ getType()

getType ( )

Return currently selected cache type: built in or varnish

Returns
int @api

Definition at line 117 of file Config.php.

118  {
119  return $this->_scopeConfig->getValue(self::XML_PAGECACHE_TYPE);
120  }

◆ getVclFile()

getVclFile (   $vclTemplatePath)

Return generated varnish.vcl configuration file

Parameters
string$vclTemplatePath
Returns
string
Deprecated:
100.2.0 see \Magento\PageCache\Model\VclGeneratorInterface::generateVcl @api

Definition at line 141 of file Config.php.

142  {
143  $accessList = $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_ACCESS_LIST);
144  $designExceptions = $this->_scopeConfig->getValue(
145  self::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
146  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
147  );
148 
149  $version = $vclTemplatePath === self::VARNISH_5_CONFIGURATION_PATH ? 5 : 4;
150  $sslOffloadedHeader = $this->_scopeConfig->getValue(
151  \Magento\Framework\HTTP\PhpEnvironment\Request::XML_PATH_OFFLOADER_HEADER
152  );
153  $vclGenerator = $this->vclGeneratorFactory->create([
154  'backendHost' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_HOST),
155  'backendPort' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_BACKEND_PORT),
156  'accessList' => $accessList ? explode(',', $accessList) : [],
157  'designExceptions' => $designExceptions ? $this->serializer->unserialize($designExceptions) : [],
158  'sslOffloadedHeader' => $sslOffloadedHeader,
159  'gracePeriod' => $this->_scopeConfig->getValue(self::XML_VARNISH_PAGECACHE_GRACE_PERIOD)
160  ]);
161  return $vclGenerator->generateVcl($version);
162  }
const XML_VARNISH_PAGECACHE_GRACE_PERIOD
Definition: Config.php:43

◆ isEnabled()

isEnabled ( )

Whether a cache type is enabled in Cache Management Grid

Returns
bool @api

Definition at line 257 of file Config.php.

258  {
259  return $this->_cacheState->isEnabled(\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER);
260  }

Field Documentation

◆ $_cacheState

Magento Framework App Cache StateInterface $_cacheState
protected

Definition at line 65 of file Config.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 50 of file Config.php.

◆ $reader

$reader
protected

Definition at line 75 of file Config.php.

◆ $readFactory

$readFactory
protected

Definition at line 70 of file Config.php.

◆ BUILT_IN

const BUILT_IN = 1

Cache types

Definition at line 26 of file Config.php.

◆ VARNISH

const VARNISH = 2

Definition at line 28 of file Config.php.

◆ VARNISH_4_CONFIGURATION_PATH

const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path'

XML path to Varnish 4 config template path

Definition at line 60 of file Config.php.

◆ VARNISH_5_CONFIGURATION_PATH

const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path'

XML path to Varnish 5 config template path

Definition at line 55 of file Config.php.

◆ XML_PAGECACHE_TTL

const XML_PAGECACHE_TTL = 'system/full_page_cache/ttl'

XML path to Varnish settings

Definition at line 33 of file Config.php.

◆ XML_PAGECACHE_TYPE

const XML_PAGECACHE_TYPE = 'system/full_page_cache/caching_application'

Definition at line 35 of file Config.php.

◆ XML_VARNISH_PAGECACHE_ACCESS_LIST

const XML_VARNISH_PAGECACHE_ACCESS_LIST = 'system/full_page_cache/varnish/access_list'

Definition at line 37 of file Config.php.

◆ XML_VARNISH_PAGECACHE_BACKEND_HOST

const XML_VARNISH_PAGECACHE_BACKEND_HOST = 'system/full_page_cache/varnish/backend_host'

Definition at line 41 of file Config.php.

◆ XML_VARNISH_PAGECACHE_BACKEND_PORT

const XML_VARNISH_PAGECACHE_BACKEND_PORT = 'system/full_page_cache/varnish/backend_port'

Definition at line 39 of file Config.php.

◆ XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX

const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX = 'design/theme/ua_regexp'

Definition at line 45 of file Config.php.

◆ XML_VARNISH_PAGECACHE_GRACE_PERIOD

const XML_VARNISH_PAGECACHE_GRACE_PERIOD = 'system/full_page_cache/varnish/grace_period'

Definition at line 43 of file Config.php.


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