Completed
Push — master ( 8d6c5f...6f0c85 )
by Martijn van
02:23
created

code/Extensions/MaintenanceModeExtension.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Class MaintenanceModeExtension
5
 *
6
 * @property Controller $owner
7
 */
8
class MaintenanceModeExtension extends Extension
9
{
10
11
    /**
12
     * @throws SS_HTTPResponse_Exception
13
     */
14
    public function onBeforeInit()
15
    {
16
        if($this->isDownForMaintenance())
17
        {
18
            $this->respond503();
19
        }
20
    }
21
22
    /**
23
     * @return bool
24
     */
25
    public function isDownForMaintenance()
26
    {
27
        return file_exists(BASE_PATH.'/mysite/down');
28
    }
29
30
    /**
31
     * Somehow $this->owner->httpError puts a burden on de Database.
32
     *
33
     * Maybe not very Silverstripe'sch, but at least this works
34
     */
35
    protected function respond503()
36
    {
37
        $message = 'Website is down for maintenance';
38
        $content = '<h1>'.$message.'</h1>';
39
40
        header($message, true, 503);
41
42
        $errorFile = BASE_PATH.'/assets/error-503.html';
43
        if(is_file($errorFile)) {
44
            $content = file_get_contents($errorFile);
45
        }
46
47
        echo $content;
48
        exit();
0 ignored issues
show
Coding Style Compatibility introduced by
The method respond503() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
49
    }
50
51
}
52