1 | <?php |
||
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 | |||
144 |