diff --git a/code/DevHealthController.php b/code/DevHealthController.php index 6ef31bd..9f91c53 100644 --- a/code/DevHealthController.php +++ b/code/DevHealthController.php @@ -12,4 +12,4 @@ class DevHealthController extends Controller { $e->setErrorCode(404); return $e; } -} \ No newline at end of file +} diff --git a/code/EnvironmentCheckSuite.php b/code/EnvironmentCheckSuite.php index f9357b4..1726139 100644 --- a/code/EnvironmentCheckSuite.php +++ b/code/EnvironmentCheckSuite.php @@ -192,6 +192,22 @@ class EnvironmentCheckSuiteResult extends ViewableData { return $this->details; } + /** + * Convert the final result status and details to JSON. + * @return string + */ + function toJSON() { + $result = array( + 'Status' => $this->Status(), + 'ShouldPass' => $this->ShouldPass(), + 'Checks' => array() + ); + foreach($this->details as $detail) { + $result['Checks'][] = $detail->toMap(); + } + return json_encode($result); + } + /** * Return a text version of a status code */ diff --git a/code/EnvironmentChecker.php b/code/EnvironmentChecker.php index ba02bdb..b0051dc 100644 --- a/code/EnvironmentChecker.php +++ b/code/EnvironmentChecker.php @@ -88,11 +88,11 @@ class EnvironmentChecker extends RequestHandler { function index() { $response = new SS_HTTPResponse; $result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run(); - + if(!$result->ShouldPass()) { $response->setStatusCode($this->errorCode); } - + $resultText = $result->customise(array( "URL" => Director::absoluteBaseURL(), "Title" => $this->title, @@ -105,6 +105,16 @@ class EnvironmentChecker extends RequestHandler { $email->send(); } + // output the result as JSON if requested + if( + $this->getRequest()->getExtension() == 'json' + || strpos($this->getRequest()->getHeader('Accept'), 'application/json') !== false + ) { + $response->setBody($result->toJSON()); + $response->addHeader('Content-Type', 'application/json'); + return $response; + } + $response->setBody($resultText); return $response;