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

Public Member Functions

 __construct (Filesystem $filesystem)
 
 isOn ($remoteAddr='')
 
 set ($isOn)
 
 setAddresses ($addresses)
 
 getAddressInfo ()
 

Data Fields

const FLAG_FILENAME = '.maintenance.flag'
 
const IP_FILENAME = '.maintenance.ip'
 
const FLAG_DIR = DirectoryList::VAR_DIR
 

Protected Attributes

 $flagDir
 

Detailed Description

Application Maintenance Mode

Definition at line 14 of file MaintenanceMode.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Filesystem  $filesystem)

Constructor

Parameters
\Magento\Framework\Filesystem$filesystem

Definition at line 46 of file MaintenanceMode.php.

47  {
48  $this->flagDir = $filesystem->getDirectoryWrite(self::FLAG_DIR);
49  }
$filesystem

Member Function Documentation

◆ getAddressInfo()

getAddressInfo ( )

Get list of IP addresses effective for maintenance mode

Returns
string[]

Definition at line 113 of file MaintenanceMode.php.

114  {
115  if ($this->flagDir->isExist(self::IP_FILENAME)) {
116  $temp = $this->flagDir->readFile(self::IP_FILENAME);
117  return explode(',', trim($temp));
118  } else {
119  return [];
120  }
121  }

◆ isOn()

isOn (   $remoteAddr = '')

Checks whether mode is on

Optionally specify an IP-address to compare against the white list

Parameters
string$remoteAddr
Returns
bool

Definition at line 59 of file MaintenanceMode.php.

60  {
61  if (!$this->flagDir->isExist(self::FLAG_FILENAME)) {
62  return false;
63  }
64  $info = $this->getAddressInfo();
65  return !in_array($remoteAddr, $info);
66  }
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ set()

set (   $isOn)

Sets maintenance mode "on" or "off"

Parameters
bool$isOn
Returns
bool

Definition at line 74 of file MaintenanceMode.php.

75  {
76  if ($isOn) {
77  return $this->flagDir->touch(self::FLAG_FILENAME);
78  }
79  if ($this->flagDir->isExist(self::FLAG_FILENAME)) {
80  return $this->flagDir->delete(self::FLAG_FILENAME);
81  }
82  return true;
83  }

◆ setAddresses()

setAddresses (   $addresses)

Sets list of allowed IP addresses

Parameters
string$addresses
Returns
bool
Exceptions

Definition at line 92 of file MaintenanceMode.php.

93  {
94  $addresses = (string)$addresses;
95  if (empty($addresses)) {
96  if ($this->flagDir->isExist(self::IP_FILENAME)) {
97  return $this->flagDir->delete(self::IP_FILENAME);
98  }
99  return true;
100  }
101  if (!preg_match('/^[^\s,]+(,[^\s,]+)*$/', $addresses)) {
102  throw new \InvalidArgumentException("One or more IP-addresses is expected (comma-separated)\n");
103  }
104  $result = $this->flagDir->writeFile(self::IP_FILENAME, $addresses);
105  return false !== $result ? true : false;
106  }
$addresses
Definition: address_list.php:7

Field Documentation

◆ $flagDir

$flagDir
protected

Definition at line 39 of file MaintenanceMode.php.

◆ FLAG_DIR

const FLAG_DIR = DirectoryList::VAR_DIR

Maintenance flag dir

Definition at line 32 of file MaintenanceMode.php.

◆ FLAG_FILENAME

const FLAG_FILENAME = '.maintenance.flag'

Maintenance flag file name

DO NOT consolidate this file and the IP white list into one. It is going to work much faster in 99% of cases: the isOn() will return false whenever file doesn't exist.

Definition at line 22 of file MaintenanceMode.php.

◆ IP_FILENAME

const IP_FILENAME = '.maintenance.ip'

IP-addresses file name

Definition at line 27 of file MaintenanceMode.php.


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