1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace dominus77\maintenance; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\BootstrapInterface; |
7
|
|
|
use yii\base\InvalidConfigException; |
8
|
|
|
use yii\base\Application; |
9
|
|
|
use dominus77\maintenance\interfaces\StateInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Maintenance |
13
|
|
|
* @package dominus77\maintenance |
14
|
|
|
*/ |
15
|
|
|
class Maintenance extends BackendMaintenance implements BootstrapInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Route to maintenance action. |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
public $route; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
public $filters; |
26
|
|
|
/** |
27
|
|
|
* Default status code to send on maintenance |
28
|
|
|
* 503 = Service Unavailable |
29
|
|
|
* @var int|string |
30
|
|
|
*/ |
31
|
|
|
public $statusCode; |
32
|
|
|
/** |
33
|
|
|
* Retry-After header |
34
|
|
|
* If not set, set automatically from the set time, + 10 minutes |
35
|
|
|
* @var bool|string |
36
|
|
|
*/ |
37
|
|
|
public $retryAfter = false; |
38
|
|
|
/** |
39
|
|
|
* @var StateInterface |
40
|
|
|
*/ |
41
|
|
|
protected $state; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Maintenance constructor. |
45
|
|
|
* @param StateInterface $state |
46
|
|
|
* @param array $config |
47
|
|
|
*/ |
48
|
|
|
public function __construct(StateInterface $state, array $config = []) |
49
|
|
|
{ |
50
|
|
|
$this->state = $state; |
51
|
|
|
if ($this->state->isEnabled()) { |
52
|
|
|
$timestamp = $this->state->timestamp(); |
53
|
|
|
$this->retryAfter = $this->retryAfter ?: gmdate('D, d M Y H:i:s \G\M\T', $timestamp); // (Wed, 21 Oct 2015 07:28:00 GMT) |
54
|
|
|
} |
55
|
|
|
$this->statusCode = $this->statusCode ?: $this->state->statusCode(); |
56
|
|
|
parent::__construct($config); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Application $app |
61
|
|
|
* @throws InvalidConfigException |
62
|
|
|
*/ |
63
|
|
|
public function bootstrap($app) |
64
|
|
|
{ |
65
|
|
|
$response = $app->response; |
66
|
|
|
if ($app->request->isAjax) { |
67
|
|
|
$response->statusCode = self::STATUS_CODE_OK; |
68
|
|
|
} else { |
69
|
|
|
$response->statusCode = (int)$this->statusCode; |
70
|
|
|
if ($this->retryAfter && is_string($this->retryAfter)) { |
71
|
|
|
$response->headers->set('Retry-After', $this->retryAfter); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($this->state->isEnabled() && !$this->filtersExcepted()) { |
76
|
|
|
$app->catchAll = [$this->route]; |
77
|
|
|
} else { |
78
|
|
|
$response->statusCode = self::STATUS_CODE_OK; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
* @throws InvalidConfigException |
85
|
|
|
*/ |
86
|
|
|
protected function filtersExcepted() |
87
|
|
|
{ |
88
|
|
|
if (empty($this->filters)) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
foreach ($this->filters as $config) { |
92
|
|
|
$filter = Yii::createObject($config); |
93
|
|
|
if (!($filter instanceof Filter)) { |
94
|
|
|
throw new InvalidConfigException( |
95
|
|
|
'Class "' . get_class($filter) . '" must instance of "' . Filter::class . '".' |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
if ($filter->isAllowed()) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|