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
Inheritance diagram for Config:
ConfigInterface AdminConfig

Public Member Functions

 __construct (\Magento\Framework\ValidatorFactory $validatorFactory, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\StringUtils $stringHelper, \Magento\Framework\App\RequestInterface $request, Filesystem $filesystem, DeploymentConfig $deploymentConfig, $scopeType, $lifetimePath=self::XML_PATH_COOKIE_LIFETIME)
 
 setOptions ($options, $default=[])
 
 getOptions ()
 
 setOption ($option, $value)
 
 getOption ($option)
 
 toArray ()
 
 setName ($name, $default=null)
 
 getName ()
 
 setSavePath ($savePath)
 
 getSavePath ()
 
 setCookieLifetime ($cookieLifetime, $default=null)
 
 getCookieLifetime ()
 
 setCookiePath ($cookiePath, $default=null)
 
 getCookiePath ()
 
 setCookieDomain ($cookieDomain, $default=null)
 
 getCookieDomain ()
 
 setCookieSecure ($cookieSecure)
 
 getCookieSecure ()
 
 setCookieHttpOnly ($cookieHttpOnly)
 
 getCookieHttpOnly ()
 
 setUseCookies ($useCookies)
 
 getUseCookies ()
 
 __call ($method, $args)
 
- Public Member Functions inherited from ConfigInterface
 setOptions ($options)
 
 setName ($name)
 
 setCookieLifetime ($cookieLifetime)
 
 setCookiePath ($cookiePath)
 
 setCookieDomain ($cookieDomain)
 

Data Fields

const PARAM_SESSION_SAVE_METHOD = 'session/save'
 
const PARAM_SESSION_SAVE_PATH = 'session/save_path'
 
const PARAM_SESSION_CACHE_LIMITER = 'session/cache_limiter'
 
const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain'
 
const XML_PATH_COOKIE_LIFETIME = 'web/cookie/cookie_lifetime'
 
const XML_PATH_COOKIE_HTTPONLY = 'web/cookie/cookie_httponly'
 
const XML_PATH_COOKIE_PATH = 'web/cookie/cookie_path'
 
const COOKIE_LIFETIME_DEFAULT = 3600
 

Protected Member Functions

 getStorageOption ($option)
 
 getFixedOptionName ($option)
 

Protected Attributes

 $options = []
 
 $_scopeConfig
 
 $_stringHelper
 
 $_httpRequest
 
 $booleanOptions
 
 $_scopeType
 
 $lifetimePath
 
 $_validatorFactory
 

Detailed Description

Magento session configuration

@SuppressWarnings(PHPMD.CouplingBetweenObjects)

Definition at line 20 of file Config.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\ValidatorFactory  $validatorFactory,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Framework\Stdlib\StringUtils  $stringHelper,
\Magento\Framework\App\RequestInterface  $request,
Filesystem  $filesystem,
DeploymentConfig  $deploymentConfig,
  $scopeType,
  $lifetimePath = self::XML_PATH_COOKIE_LIFETIME 
)
Parameters
\Magento\Framework\ValidatorFactory$validatorFactory
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\Stdlib\StringUtils$stringHelper
\Magento\Framework\App\RequestInterface$request
Filesystem$filesystem
DeploymentConfig$deploymentConfig
string$scopeType
string$lifetimePath@SuppressWarnings(PHPMD.NPathComplexity)

Session path

Session save handler - memcache, files, etc

Session cache limiter

Cookie settings: lifetime, path, domain, httpOnly. These govern settings for the session cookie.

Definition at line 106 of file Config.php.

115  {
116  $this->_validatorFactory = $validatorFactory;
117  $this->_scopeConfig = $scopeConfig;
118  $this->_stringHelper = $stringHelper;
119  $this->_httpRequest = $request;
120  $this->_scopeType = $scopeType;
121  $this->lifetimePath = $lifetimePath;
122 
126  $savePath = $deploymentConfig->get(self::PARAM_SESSION_SAVE_PATH);
127  if (!$savePath && !ini_get('session.save_path')) {
128  $sessionDir = $filesystem->getDirectoryWrite(DirectoryList::SESSION);
129  $savePath = $sessionDir->getAbsolutePath();
130  $sessionDir->create();
131  }
132  if ($savePath) {
133  $this->setSavePath($savePath);
134  }
135 
139  $saveHandler = $deploymentConfig->get(self::PARAM_SESSION_SAVE_METHOD);
140  if ($saveHandler) {
141  $this->setOption('session.save_handler', $saveHandler);
142  }
143 
147  $cacheLimiter = $deploymentConfig->get(self::PARAM_SESSION_CACHE_LIMITER);
148  if ($cacheLimiter) {
149  $this->setOption('session.cache_limiter', $cacheLimiter);
150  }
151 
155  $lifetime = $this->_scopeConfig->getValue($this->lifetimePath, $this->_scopeType);
156  $this->setCookieLifetime($lifetime, self::COOKIE_LIFETIME_DEFAULT);
157 
158  $path = $this->_scopeConfig->getValue(self::XML_PATH_COOKIE_PATH, $this->_scopeType);
159  $path = empty($path) ? $this->_httpRequest->getBasePath() : $path;
160  $this->setCookiePath($path, $this->_httpRequest->getBasePath());
161 
162  $domain = $this->_scopeConfig->getValue(self::XML_PATH_COOKIE_DOMAIN, $this->_scopeType);
163  $domain = empty($domain) ? $this->_httpRequest->getHttpHost() : $domain;
164  $this->setCookieDomain((string)$domain, $this->_httpRequest->getHttpHost());
165 
166  $this->setCookieHttpOnly(
167  $this->_scopeConfig->getValue(self::XML_PATH_COOKIE_HTTPONLY, $this->_scopeType)
168  );
169 
170  $secureURL = $this->_scopeConfig->getValue('web/secure/base_url', $this->_scopeType);
171  $unsecureURL = $this->_scopeConfig->getValue('web/unsecure/base_url', $this->_scopeType);
172  $isFullySecuredURL = $secureURL == $unsecureURL;
173  $this->setCookieSecure($isFullySecuredURL && $this->_httpRequest->isSecure());
174  }
setCookiePath($cookiePath, $default=null)
Definition: Config.php:345
setCookieDomain($cookieDomain, $default=null)
Definition: Config.php:378
setCookieLifetime($cookieLifetime, $default=null)
Definition: Config.php:313
$deploymentConfig
setCookieSecure($cookieSecure)
Definition: Config.php:409
setCookieHttpOnly($cookieHttpOnly)
Definition: Config.php:432
$filesystem
setOption($option, $value)
Definition: Config.php:216

Member Function Documentation

◆ __call()

__call (   $method,
  $args 
)

Intercept get*() and set*() methods

Intercepts getters and setters and passes them to getOption() and setOption(), respectively.

Parameters
string$method
array$args
Returns
mixed
Exceptions

Definition at line 523 of file Config.php.

524  {
525  $prefix = substr($method, 0, 3);
526  $option = substr($method, 3);
527  $key = strtolower(preg_replace('#(?<=[a-z])([A-Z])#', '_\1', $option));
528 
529  if ($prefix === 'set') {
530  $value = array_shift($args);
531  return $this->setOption($key, $value);
532  } elseif ($prefix === 'get') {
533  return $this->getOption($key);
534  } else {
535  throw new \BadMethodCallException(sprintf('Method "%s" does not exist in %s', $method, get_class($this)));
536  }
537  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$prefix
Definition: name.phtml:25
$value
Definition: gender.phtml:16
$method
Definition: info.phtml:13
setOption($option, $value)
Definition: Config.php:216

◆ getCookieDomain()

getCookieDomain ( )

Get session.cookie_domain

Returns
string

Implements ConfigInterface.

Definition at line 398 of file Config.php.

399  {
400  return (string)$this->getOption('session.cookie_domain');
401  }

◆ getCookieHttpOnly()

getCookieHttpOnly ( )

Get session.cookie_httponly

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Implements ConfigInterface.

Definition at line 444 of file Config.php.

445  {
446  return (bool)$this->getOption('session.cookie_httponly');
447  }

◆ getCookieLifetime()

getCookieLifetime ( )

Get session.cookie_lifetime

Returns
int

Implements ConfigInterface.

Definition at line 333 of file Config.php.

334  {
335  return (int)$this->getOption('session.cookie_lifetime');
336  }

◆ getCookiePath()

getCookiePath ( )

Get session.cookie_path

Returns
string

Implements ConfigInterface.

Definition at line 366 of file Config.php.

367  {
368  return (string)$this->getOption('session.cookie_path');
369  }

◆ getCookieSecure()

getCookieSecure ( )

Get session.cookie_secure

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Implements ConfigInterface.

Definition at line 421 of file Config.php.

422  {
423  return (bool)$this->getOption('session.cookie_secure');
424  }

◆ getFixedOptionName()

getFixedOptionName (   $option)
protected

Fix session option name

Parameters
string$option
Returns
string

Definition at line 494 of file Config.php.

495  {
496  $option = strtolower($option);
497 
498  switch ($option) {
499  case 'url_rewriter_tags':
500  $option = 'url_rewriter.tags';
501  break;
502  default:
503  if (strpos($option, 'session.') !== 0) {
504  $option = 'session.' . $option;
505  }
506  break;
507  }
508 
509  return $option;
510  }

◆ getName()

getName ( )

Get session.name

Returns
string

Implements ConfigInterface.

Definition at line 279 of file Config.php.

280  {
281  return (string)$this->getOption('session.name');
282  }

◆ getOption()

getOption (   $option)

Get an individual option

Parameters
string$option
Returns
mixed

Implements ConfigInterface.

Definition at line 230 of file Config.php.

231  {
233  if (array_key_exists($option, $this->options)) {
234  return $this->options[$option];
235  }
236 
237  $value = $this->getStorageOption($option);
238  if (null !== $value) {
239  $this->options[$option] = $value;
240  return $value;
241  }
242 
243  return null;
244  }
$value
Definition: gender.phtml:16

◆ getOptions()

getOptions ( )

Get all options set

Returns
array

Implements ConfigInterface.

Definition at line 204 of file Config.php.

205  {
206  return $this->options;
207  }

◆ getSavePath()

getSavePath ( )

Set session.save_path

Returns
string

Implements ConfigInterface.

Definition at line 301 of file Config.php.

302  {
303  return (string)$this->getOption('session.save_path');
304  }

◆ getStorageOption()

getStorageOption (   $option)
protected

Retrieve a storage option from a backend configuration store

Parameters
string$option
Returns
string|bool

Definition at line 478 of file Config.php.

479  {
481  if (in_array($option, $this->booleanOptions)) {
482  $value = (bool)$value;
483  }
484 
485  return $value;
486  }
$value
Definition: gender.phtml:16

◆ getUseCookies()

getUseCookies ( )

Get session.use_cookies

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Implements ConfigInterface.

Definition at line 467 of file Config.php.

468  {
469  return (bool)$this->getOption('session.use_cookies');
470  }

◆ setCookieDomain()

setCookieDomain (   $cookieDomain,
  $default = null 
)

Set session.cookie_domain

Parameters
string$cookieDomain
string | null$default
Returns
$this

Definition at line 378 of file Config.php.

379  {
380  $validator = $this->_validatorFactory->create(
381  [],
382  \Magento\Framework\Session\Config\Validator\CookieDomainValidator::class
383  );
384  if ($validator->isValid($cookieDomain)) {
385  $this->setOption('session.cookie_domain', $cookieDomain);
386  } elseif (null !== $default && $validator->isValid($default)) {
387  $this->setOption('session.cookie_domain', $default);
388  }
389 
390  return $this;
391  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setOption($option, $value)
Definition: Config.php:216

◆ setCookieHttpOnly()

setCookieHttpOnly (   $cookieHttpOnly)

Set session.cookie_httponly

Parameters
bool$cookieHttpOnly
Returns
$this

Implements ConfigInterface.

Definition at line 432 of file Config.php.

433  {
434  $this->setOption('session.cookie_httponly', (bool)$cookieHttpOnly);
435  return $this;
436  }
setOption($option, $value)
Definition: Config.php:216

◆ setCookieLifetime()

setCookieLifetime (   $cookieLifetime,
  $default = null 
)

Set session.cookie_lifetime

Parameters
int$cookieLifetime
int | null$default
Returns
$this

Definition at line 313 of file Config.php.

314  {
315  $validator = $this->_validatorFactory->create(
316  [],
317  \Magento\Framework\Session\Config\Validator\CookieLifetimeValidator::class
318  );
319  if ($validator->isValid($cookieLifetime)) {
320  $this->setOption('session.cookie_lifetime', (int)$cookieLifetime);
321  } elseif (null !== $default && $validator->isValid($default)) {
322  $this->setOption('session.cookie_lifetime', (int)$default);
323  }
324 
325  return $this;
326  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setOption($option, $value)
Definition: Config.php:216

◆ setCookiePath()

setCookiePath (   $cookiePath,
  $default = null 
)

Set session.cookie_path

Parameters
string$cookiePath
string | null$default
Returns
$this

Definition at line 345 of file Config.php.

346  {
347  $cookiePath = (string)$cookiePath;
348  $validator = $this->_validatorFactory->create(
349  [],
350  \Magento\Framework\Session\Config\Validator\CookiePathValidator::class
351  );
352  if ($validator->isValid($cookiePath)) {
353  $this->setOption('session.cookie_path', $cookiePath);
354  } elseif (null !== $default && $validator->isValid($default)) {
355  $this->setOption('session.cookie_path', $default);
356  }
357 
358  return $this;
359  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
setOption($option, $value)
Definition: Config.php:216

◆ setCookieSecure()

setCookieSecure (   $cookieSecure)

Set session.cookie_secure

Parameters
bool$cookieSecure
Returns
$this

Implements ConfigInterface.

Definition at line 409 of file Config.php.

410  {
411  $this->setOption('session.cookie_secure', (bool)$cookieSecure);
412  return $this;
413  }
setOption($option, $value)
Definition: Config.php:216

◆ setName()

setName (   $name,
  $default = null 
)

Set session.name

Parameters
string$name
string | null$default
Returns
$this

Definition at line 263 of file Config.php.

264  {
265  $name = (string)$name;
266  $name = empty($name) ? $default : $name;
267  if (!empty($name)) {
268  $this->setOption('session.name', $name);
269  }
270 
271  return $this;
272  }
setOption($option, $value)
Definition: Config.php:216
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ setOption()

setOption (   $option,
  $value 
)

Set an individual option

Parameters
string$option
mixed$value
Returns
$this

Implements ConfigInterface.

Definition at line 216 of file Config.php.

217  {
219  $this->options[$option] = $value;
220 
221  return $this;
222  }
$value
Definition: gender.phtml:16

◆ setOptions()

setOptions (   $options,
  $default = [] 
)

Set many options at once

Parameters
array$options
array$default
Returns
$this

Definition at line 183 of file Config.php.

184  {
185  $options = (!is_array($options) && !$options instanceof \Traversable) ? $default : $options;
186  if (is_array($options) || $options instanceof \Traversable) {
187  foreach ($options as $option => $value) {
188  $setter = 'set' . $this->_stringHelper->upperCaseWords($option, '_', '');
189  if (method_exists($this, $setter)) {
190  $this->{$setter}($value);
191  } else {
192  $this->setOption($option, $value);
193  }
194  }
195  }
196  return $this;
197  }
$value
Definition: gender.phtml:16
setOption($option, $value)
Definition: Config.php:216

◆ setSavePath()

setSavePath (   $savePath)

Set session.save_path

Parameters
string$savePath
Returns
$this

Implements ConfigInterface.

Definition at line 290 of file Config.php.

291  {
292  $this->setOption('session.save_path', $savePath);
293  return $this;
294  }
setOption($option, $value)
Definition: Config.php:216

◆ setUseCookies()

setUseCookies (   $useCookies)

Set session.use_cookies

Parameters
bool$useCookies
Returns
$this

Implements ConfigInterface.

Definition at line 455 of file Config.php.

456  {
457  $this->setOption('session.use_cookies', (bool)$useCookies);
458  return $this;
459  }
setOption($option, $value)
Definition: Config.php:216

◆ toArray()

toArray ( )

Convert config to array

Returns
array

Implements ConfigInterface.

Definition at line 251 of file Config.php.

252  {
253  return $this->getOptions();
254  }

Field Documentation

◆ $_httpRequest

$_httpRequest
protected

Definition at line 66 of file Config.php.

◆ $_scopeConfig

$_scopeConfig
protected

Definition at line 56 of file Config.php.

◆ $_scopeType

$_scopeType
protected

Definition at line 83 of file Config.php.

◆ $_stringHelper

$_stringHelper
protected

Definition at line 61 of file Config.php.

◆ $_validatorFactory

$_validatorFactory
protected

Definition at line 93 of file Config.php.

◆ $booleanOptions

$booleanOptions
protected
Initial value:
= [
'session.use_cookies',
'session.use_only_cookies',
'session.use_trans_sid',
'session.cookie_httponly',
]

Definition at line 73 of file Config.php.

◆ $lifetimePath

$lifetimePath
protected

Definition at line 88 of file Config.php.

◆ $options

$options = []
protected

Definition at line 51 of file Config.php.

◆ COOKIE_LIFETIME_DEFAULT

const COOKIE_LIFETIME_DEFAULT = 3600

Cookie default lifetime

Definition at line 44 of file Config.php.

◆ PARAM_SESSION_CACHE_LIMITER

const PARAM_SESSION_CACHE_LIMITER = 'session/cache_limiter'

Configuration path for session cache limiter

Definition at line 29 of file Config.php.

◆ PARAM_SESSION_SAVE_METHOD

const PARAM_SESSION_SAVE_METHOD = 'session/save'

Configuration path for session save method

Definition at line 23 of file Config.php.

◆ PARAM_SESSION_SAVE_PATH

const PARAM_SESSION_SAVE_PATH = 'session/save_path'

Configuration path for session save path

Definition at line 26 of file Config.php.

◆ XML_PATH_COOKIE_DOMAIN

const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain'

Configuration path for cookie domain

Definition at line 32 of file Config.php.

◆ XML_PATH_COOKIE_HTTPONLY

const XML_PATH_COOKIE_HTTPONLY = 'web/cookie/cookie_httponly'

Configuration path for cookie http only param

Definition at line 38 of file Config.php.

◆ XML_PATH_COOKIE_LIFETIME

const XML_PATH_COOKIE_LIFETIME = 'web/cookie/cookie_lifetime'

Configuration path for cookie lifetime

Definition at line 35 of file Config.php.

◆ XML_PATH_COOKIE_PATH

const XML_PATH_COOKIE_PATH = 'web/cookie/cookie_path'

Configuration path for cookie path

Definition at line 41 of file Config.php.


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