1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* Copyright © 2013-2017 Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Update;
use Magento\Update\Status;
/**
* Class for handling Magento maintenance mode.
*/
class MaintenanceMode
{
/**
* Path to the maintenance flag file
*
* @var string
*/
protected $flagFile;
/**
* Path to the file with white-listed IP addresses
*
* @var string
*/
protected $ipFile;
/**
* @var Status
*/
protected $status;
/**
* Initialize.
*
* @param string|null $flagFile
* @param string|null $ipFile
* @param Status|null $status
*/
public function __construct($flagFile = null, $ipFile = null, Status $status = null)
{
$this->flagFile = $flagFile ? $flagFile : MAGENTO_BP . '/var/.maintenance.flag';
$this->ipFile = $ipFile ? $ipFile : MAGENTO_BP . '/var/.maintenance.ip';
$this->status = $status ? $status : new Status();
}
/**
* Check whether Magento maintenance mode is on.
*
* @return bool
*/
public function isOn()
{
return file_exists($this->flagFile);
}
/**
* Set maintenance mode.
*
* @param bool $isOn
* @return $this
* @throws \RuntimeException
*/
public function set($isOn)
{
if ($isOn) {
if (touch($this->flagFile)) {
$this->status->add("Magento maintenance mode is enabled.", \Psr\Log\LogLevel::INFO);
} else {
throw new \RuntimeException("Magento maintenance mode cannot be enabled.");
}
} else if (file_exists($this->flagFile)) {
if (file_exists($this->ipFile)) {
/** Maintenance mode should not be unset from updater application if it was set manually by the admin */
$this->status->add(
"Magento maintenance mode was not disabled. It can be disabled from the Magento Backend.",
\Psr\Log\LogLevel::INFO
);
} else if (unlink($this->flagFile)) {
$this->status->add("Magento maintenance mode is disabled.", \Psr\Log\LogLevel::INFO);
} else {
throw new \RuntimeException("Magento maintenance mode cannot be disabled.");
}
}
return $this;
}
}