2015-07-24 00:53:41 +02:00
|
|
|
<?php
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
namespace SilverStripe\Logging;
|
2015-07-24 00:53:41 +02:00
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\SS_HTTPResponse;
|
2015-07-24 00:53:41 +02:00
|
|
|
use Monolog\Handler\AbstractProcessingHandler;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output the error to the browser, with the given HTTP status code.
|
|
|
|
* We recommend that you use a formatter that generates HTML with this.
|
|
|
|
*/
|
|
|
|
class HTTPOutputHandler extends AbstractProcessingHandler
|
|
|
|
{
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
private $contentType = "text/html";
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var int
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
private $statusCode = 500;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mime type to use when displaying this error.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2015-07-24 00:53:41 +02:00
|
|
|
*/
|
|
|
|
public function getContentType() {
|
|
|
|
return $this->contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the mime type to use when displaying this error.
|
|
|
|
* Default text/html
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param string $contentType
|
|
|
|
* @return $this
|
2015-07-24 00:53:41 +02:00
|
|
|
*/
|
|
|
|
public function setContentType($contentType) {
|
|
|
|
$this->contentType = $contentType;
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this;
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the HTTP status code to use when displaying this error.
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @return int
|
2015-07-24 00:53:41 +02:00
|
|
|
*/
|
|
|
|
public function getStatusCode() {
|
|
|
|
return $this->statusCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the HTTP status code to use when displaying this error.
|
|
|
|
* Default 500
|
2016-08-19 00:51:35 +02:00
|
|
|
*
|
|
|
|
* @param int $statusCode
|
|
|
|
* @return $this
|
2015-07-24 00:53:41 +02:00
|
|
|
*/
|
|
|
|
public function setStatusCode($statusCode) {
|
|
|
|
$this->statusCode = $statusCode;
|
2016-08-19 00:51:35 +02:00
|
|
|
return $this;
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
/**
|
|
|
|
* @param array $record
|
|
|
|
* @return bool
|
|
|
|
*/
|
2015-07-24 00:53:41 +02:00
|
|
|
protected function write(array $record) {
|
|
|
|
ini_set('display_errors', 0);
|
|
|
|
|
2015-10-06 07:31:58 +02:00
|
|
|
// TODO: This coupling isn't ideal
|
2015-07-24 00:53:41 +02:00
|
|
|
// See https://github.com/silverstripe/silverstripe-framework/issues/4484
|
2016-08-19 00:51:35 +02:00
|
|
|
if(Controller::has_curr()) {
|
|
|
|
$response = Controller::curr()->getResponse();
|
2015-07-24 00:53:41 +02:00
|
|
|
} else {
|
2016-08-19 00:51:35 +02:00
|
|
|
$response = new SS_HTTPResponse();
|
2015-07-24 00:53:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If headers have been sent then these won't be used, and may throw errors that we wont' want to see.
|
|
|
|
if(!headers_sent()) {
|
|
|
|
$response->setStatusCode($this->statusCode);
|
|
|
|
$response->addHeader("Content-Type", $this->contentType);
|
|
|
|
} else {
|
|
|
|
// To supress errors aboot errors
|
|
|
|
$response->setStatusCode(200);
|
|
|
|
}
|
|
|
|
|
|
|
|
$response->setBody($record['formatted']);
|
|
|
|
$response->output();
|
|
|
|
|
|
|
|
return false === $this->bubble;
|
|
|
|
}
|
|
|
|
}
|