mirror of
https://github.com/silverstripe/silverstripe-environmentcheck
synced 2024-10-22 17:05:40 +02:00
Merge pull request #14 from halkyon/json-output
Output check result and details as JSON if requested
This commit is contained in:
commit
20f0a2bf88
@ -12,4 +12,4 @@ class DevHealthController extends Controller {
|
|||||||
$e->setErrorCode(404);
|
$e->setErrorCode(404);
|
||||||
return $e;
|
return $e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -192,6 +192,22 @@ class EnvironmentCheckSuiteResult extends ViewableData {
|
|||||||
return $this->details;
|
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
|
* Return a text version of a status code
|
||||||
*/
|
*/
|
||||||
|
@ -88,11 +88,11 @@ class EnvironmentChecker extends RequestHandler {
|
|||||||
function index() {
|
function index() {
|
||||||
$response = new SS_HTTPResponse;
|
$response = new SS_HTTPResponse;
|
||||||
$result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run();
|
$result = EnvironmentCheckSuite::inst($this->checkSuiteName)->run();
|
||||||
|
|
||||||
if(!$result->ShouldPass()) {
|
if(!$result->ShouldPass()) {
|
||||||
$response->setStatusCode($this->errorCode);
|
$response->setStatusCode($this->errorCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
$resultText = $result->customise(array(
|
$resultText = $result->customise(array(
|
||||||
"URL" => Director::absoluteBaseURL(),
|
"URL" => Director::absoluteBaseURL(),
|
||||||
"Title" => $this->title,
|
"Title" => $this->title,
|
||||||
@ -105,6 +105,16 @@ class EnvironmentChecker extends RequestHandler {
|
|||||||
$email->send();
|
$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);
|
$response->setBody($resultText);
|
||||||
|
|
||||||
return $response;
|
return $response;
|
||||||
|
Loading…
Reference in New Issue
Block a user