Completed
Push — cli-error-fix ( 045f02...09496c )
by Sam
07:08
created

HTTPOutputHandler::setStatusCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Logging;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Control\HTTPResponse;
8
use Monolog\Handler\AbstractProcessingHandler;
9
use Monolog\Formatter\FormatterInterface;
10
11
/**
12
 * Output the error to the browser, with the given HTTP status code.
13
 * We recommend that you use a formatter that generates HTML with this.
14
 */
15
class HTTPOutputHandler extends AbstractProcessingHandler
16
{
17
18
    /**
19
     * @var string
20
     */
21
    private $contentType = "text/html";
22
23
    /**
24
     * @var int
25
     */
26
    private $statusCode = 500;
27
28
    /**
29
     * @var FormatterInterface
30
     */
31
    private $cliFormatter = null;
32
33
    /**
34
     * Get the mime type to use when displaying this error.
35
     *
36
     * @return string
37
     */
38
    public function getContentType()
39
    {
40
        return $this->contentType;
41
    }
42
43
    /**
44
     * Set the mime type to use when displaying this error.
45
     * Default text/html
46
     *
47
     * @param string $contentType
48
     * @return $this
49
     */
50
    public function setContentType($contentType)
51
    {
52
        $this->contentType = $contentType;
53
        return $this;
54
    }
55
56
    /**
57
     * Get the HTTP status code to use when displaying this error.
58
     *
59
     * @return int
60
     */
61
    public function getStatusCode()
62
    {
63
        return $this->statusCode;
64
    }
65
66
    /**
67
     * Set the HTTP status code to use when displaying this error.
68
     * Default 500
69
     *
70
     * @param int $statusCode
71
     * @return $this
72
     */
73
    public function setStatusCode($statusCode)
74
    {
75
        $this->statusCode = $statusCode;
76
        return $this;
77
    }
78
79
    /**
80
     * Set a formatter to use if Director::is_cli() is true
81
     *
82
     * @param $cliFormatter
83
     */
84
    public function setCLIFormatter(FormatterInterface $cliFormatter)
85
    {
86
        $this->cliFormatter = $cliFormatter;
87
88
        return $this;
89
    }
90
91
    /**
92
     * Return the formatter use if Director::is_cli() is true
93
     * If none has been set, null is returned, and the getFormatter() result will be used instead
94
     */
95
    public function getCLIFormatter()
96
    {
97
        return $this->cliFormatter;
98
    }
99
100
    /**
101
     * Return the formatter to use in this case.
102
     * May be the getCliFormatter() value if one is provided and Director::is_cli() is true.
103
     */
104
    public function getFormatter()
105
    {
106
        if (Director::is_cli() && ($cliFormatter = $this->getCLIFormatter())) {
107
            return $cliFormatter;
108
        }
109
110
        return parent::getFormatter();
111
    }
112
113
    /**
114
     * @param array $record
115
     * @return bool
116
     */
117
    protected function write(array $record)
118
    {
119
        ini_set('display_errors', 0);
120
121
        // TODO: This coupling isn't ideal
122
        // See https://github.com/silverstripe/silverstripe-framework/issues/4484
123
        if (Controller::has_curr()) {
124
            $response = Controller::curr()->getResponse();
125
        } else {
126
            $response = new HTTPResponse();
127
        }
128
129
        // If headers have been sent then these won't be used, and may throw errors that we wont' want to see.
130
        if (!headers_sent()) {
131
            $response->setStatusCode($this->statusCode);
132
            $response->addHeader("Content-Type", $this->contentType);
133
        } else {
134
            // To supress errors aboot errors
135
            $response->setStatusCode(200);
136
        }
137
138
        $response->setBody($record['formatted']);
139
        $response->output();
140
141
        return false === $this->bubble;
142
    }
143
}
144