silverstripe-environmentcheck/code/EnvironmentCheckSuite.php

303 lines
8.1 KiB
PHP
Raw Normal View History

<?php
2015-09-10 23:13:48 +02:00
/**
* Represents a suite of environment checks.
* Specific checks can be registered against a named instance of EnvironmentCheckSuite.
*
* Usage #1 - _config.php
* EnvironmentCheckSuite::register('health', 'MyHealthCheck("my param")', 'Title of my health check');
*
* Usage #2 - config.yml
* EnvironmentCheckSuite:
* registered_checks:
* mycheck:
* definition: 'MyHealthCheck("my param")'
* title: 'Title of my health check'
* registered_suites:
* health:
* - mycheck
*
* $result = EnvironmentCheckSuite::inst('health')->run();
*/
2015-11-21 07:18:35 +01:00
class EnvironmentCheckSuite extends Object
{
/**
* Name of this suite.
*
* @var string
*/
protected $name;
2015-11-21 07:18:35 +01:00
/**
* @var array
*/
protected $checks = array();
2015-11-21 07:18:35 +01:00
/**
* Associative array of named checks registered via the config system. Each check should specify:
* - definition (e.g. 'MyHealthCheck("my param")')
* - title (e.g. 'Is my feature working?')
* - state (setting this to 'disabled' will cause suites to skip this check entirely.
*
* @var array
*/
private static $registered_checks = array();
2015-11-21 07:18:35 +01:00
/**
* Associative array of named suites registered via the config system. Each suite should enumerate
* named checks that have been configured in 'registered_checks'.
*
* @var array
*/
private static $registered_suites = array();
2015-11-21 07:18:35 +01:00
/**
* Load checks for this suite from the configuration system. This is an alternative to the
* EnvironmentCheckSuite::register - both can be used, checks will be appended to the suite.
*
* @param string $suiteName The name of this suite.
*/
public function __construct($suiteName)
{
parent::__construct();
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
if (empty($this->config()->registered_suites[$suiteName])) {
// Not registered via config system, but it still may be configured later via self::register.
return;
}
2015-11-21 07:18:35 +01:00
foreach ($this->config()->registered_suites[$suiteName] as $checkName) {
if (empty($this->config()->registered_checks[$checkName])) {
throw new InvalidArgumentException(
"Bad EnvironmentCheck: '$checkName' - the named check has not been registered."
);
}
2015-11-21 07:18:35 +01:00
$check = $this->config()->registered_checks[$checkName];
2015-11-21 07:18:35 +01:00
// Existing named checks can be disabled by setting their 'state' to 'disabled'.
// This is handy for disabling checks mandated by modules.
if (!empty($check['state']) && $check['state']==='disabled') {
continue;
}
// Add the check to this suite.
$this->push($check['definition'], $check['title']);
}
}
2015-11-21 07:18:35 +01:00
/**
* Run this test suite and return the result code of the worst result.
*
* @return int
*/
public function run()
{
$result = new EnvironmentCheckSuiteResult();
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
foreach ($this->checkInstances() as $check) {
list($checkClass, $checkTitle) = $check;
try {
list($status, $message) = $checkClass->check();
// If the check fails, register that as an error
} catch (Exception $e) {
$status = EnvironmentCheck::ERROR;
$message = $e->getMessage();
}
$result->addResult($status, $message, $checkTitle);
}
2015-11-21 07:18:35 +01:00
return $result;
}
2015-11-21 07:18:35 +01:00
/**
* Get instances of all the environment checks.
*
* @return array
*/
protected function checkInstances()
{
$output = array();
foreach ($this->checks as $check) {
list($checkClass, $checkTitle) = $check;
if (is_string($checkClass)) {
$checkInst = Object::create_from_string($checkClass);
if ($checkInst instanceof EnvironmentCheck) {
$output[] = array($checkInst, $checkTitle);
} else {
throw new InvalidArgumentException("Bad EnvironmentCheck: '$checkClass' - the named class doesn't implement EnvironmentCheck");
}
} elseif ($checkClass instanceof EnvironmentCheck) {
$output[] = array($checkClass, $checkTitle);
} else {
throw new InvalidArgumentException("Bad EnvironmentCheck: " . var_export($check, true));
}
}
return $output;
}
2015-11-21 07:18:35 +01:00
/**
* Add a check to this suite.
*
* @param mixed $check
* @param string $title
*/
public function push($check, $title = null)
{
if (!$title) {
$title = is_string($check) ? $check : get_class($check);
}
$this->checks[] = array($check, $title);
}
2015-11-21 07:18:35 +01:00
/////////////////////////////////////////////////////////////////////////////////////////////
2015-11-21 07:18:35 +01:00
/**
* @var array
*/
protected static $instances = array();
2015-11-21 07:18:35 +01:00
/**
* Return a named instance of EnvironmentCheckSuite.
*
* @param string $name
*
* @return EnvironmentCheckSuite
*/
public static function inst($name)
{
if (!isset(self::$instances[$name])) {
self::$instances[$name] = new EnvironmentCheckSuite($name);
}
return self::$instances[$name];
}
2015-11-21 07:18:35 +01:00
/**
* Register a check against the named check suite.
*
* @param string|array $names
* @param EnvironmentCheck $check
* @param string|array
*/
public static function register($names, $check, $title = null)
{
if (!is_array($names)) {
$names = array($names);
}
foreach ($names as $name) {
self::inst($name)->push($check, $title);
}
}
2015-11-21 07:18:35 +01:00
/**
* Unregisters all checks.
*/
public static function reset()
{
self::$instances = array();
}
}
/**
* A single set of results from running an EnvironmentCheckSuite
*/
2015-11-21 07:18:35 +01:00
class EnvironmentCheckSuiteResult extends ViewableData
{
/**
* @var ArrayList
*/
protected $details;
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
/**
* @var int
*/
protected $worst = 0;
2015-09-10 23:13:48 +02:00
2015-11-21 07:18:35 +01:00
public function __construct()
{
parent::__construct();
$this->details = new ArrayList();
}
2015-11-21 07:18:35 +01:00
/**
* @param int $status
* @param string $message
* @param string $checkIdentifier
*/
public function addResult($status, $message, $checkIdentifier)
{
$this->details->push(new ArrayData(array(
'Check' => $checkIdentifier,
'Status' => $this->statusText($status),
'StatusCode' => $status,
'Message' => $message,
)));
2015-11-21 07:18:35 +01:00
$this->worst = max($this->worst, $status);
}
2015-11-21 07:18:35 +01:00
/**
* Returns true if there are no errors.
*
* @return bool
*/
public function ShouldPass()
{
return $this->worst <= EnvironmentCheck::WARNING;
}
2015-11-21 07:18:35 +01:00
/**
* Returns overall (i.e. worst) status as a string.
*
* @return string
*/
public function Status()
{
return $this->statusText($this->worst);
}
2015-11-21 07:18:35 +01:00
/**
* Returns detailed status information about each check.
*
* @return ArrayList
*/
public function Details()
{
return $this->details;
}
2015-11-21 07:18:35 +01:00
/**
* Convert the final result status and details to JSON.
*
* @return string
*/
public 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);
}
2015-11-21 07:18:35 +01:00
/**
* Return a text version of a status code.
*
* @return string
*/
protected function statusText($status)
{
switch ($status) {
case EnvironmentCheck::ERROR: return "ERROR";
case EnvironmentCheck::WARNING: return "WARNING";
case EnvironmentCheck::OK: return "OK";
case 0: return "NO CHECKS";
default: throw new InvalidArgumentException("Bad environment check status '$status'");
}
}
}