|
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 HTTPOutputHandler Return $this to allow chainable calls |
|
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
|
|
|
* @return HTTPOutputHandler Return $this to allow chainable calls |
|
84
|
|
|
*/ |
|
85
|
|
|
public function setCLIFormatter(FormatterInterface $cliFormatter) |
|
86
|
|
|
{ |
|
87
|
|
|
$this->cliFormatter = $cliFormatter; |
|
88
|
|
|
|
|
89
|
|
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the formatter use if Director::is_cli() is true |
|
94
|
|
|
* If none has been set, null is returned, and the getFormatter() result will be used instead |
|
95
|
|
|
* |
|
96
|
|
|
* @return FormatterInterface |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getCLIFormatter() |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->cliFormatter; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return the formatter to use in this case. |
|
105
|
|
|
* May be the getCliFormatter() value if one is provided and Director::is_cli() is true. |
|
106
|
|
|
* |
|
107
|
|
|
* @return FormatterInterface |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getFormatter() |
|
110
|
|
|
{ |
|
111
|
|
|
if (Director::is_cli() && ($cliFormatter = $this->getCLIFormatter())) { |
|
112
|
|
|
return $cliFormatter; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return parent::getFormatter(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param array $record |
|
120
|
|
|
* @return bool |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function write(array $record) |
|
123
|
|
|
{ |
|
124
|
|
|
ini_set('display_errors', 0); |
|
125
|
|
|
|
|
126
|
|
|
// TODO: This coupling isn't ideal |
|
127
|
|
|
// See https://github.com/silverstripe/silverstripe-framework/issues/4484 |
|
128
|
|
|
if (Controller::has_curr()) { |
|
129
|
|
|
$response = Controller::curr()->getResponse(); |
|
130
|
|
|
} else { |
|
131
|
|
|
$response = new HTTPResponse(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// If headers have been sent then these won't be used, and may throw errors that we wont' want to see. |
|
135
|
|
|
if (!headers_sent()) { |
|
136
|
|
|
$response->setStatusCode($this->statusCode); |
|
137
|
|
|
$response->addHeader("Content-Type", $this->contentType); |
|
138
|
|
|
} else { |
|
139
|
|
|
// To supress errors aboot errors |
|
140
|
|
|
$response->setStatusCode(200); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$response->setBody($record['formatted']); |
|
144
|
|
|
$response->output(); |
|
145
|
|
|
|
|
146
|
|
|
return false === $this->bubble; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|