2012-01-20 18:10:48 +01:00
|
|
|
<?php
|
2015-09-10 23:13:48 +02:00
|
|
|
|
2017-01-04 23:16:12 +01:00
|
|
|
namespace SilverStripe\EnvironmentCheck;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use InvalidArgumentException;
|
2017-05-26 18:40:25 +02:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use SilverStripe\Core\Extensible;
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2017-05-26 18:11:35 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2012-01-20 18:10:48 +01:00
|
|
|
/**
|
|
|
|
* Represents a suite of environment checks.
|
2015-03-12 02:58:03 +01:00
|
|
|
* 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
|
|
|
|
*
|
2012-01-20 18:10:48 +01:00
|
|
|
* $result = EnvironmentCheckSuite::inst('health')->run();
|
2017-01-04 23:16:12 +01:00
|
|
|
*
|
|
|
|
* @package environmentcheck
|
2012-01-20 18:10:48 +01:00
|
|
|
*/
|
2017-05-26 18:40:25 +02:00
|
|
|
class EnvironmentCheckSuite
|
2015-11-21 07:18:35 +01:00
|
|
|
{
|
2017-05-26 18:40:25 +02:00
|
|
|
use Configurable;
|
|
|
|
use Injectable;
|
|
|
|
use Extensible;
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* Name of this suite.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $name;
|
2015-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-21 11:57:07 +01:00
|
|
|
protected $checks = [];
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2017-03-21 11:57:07 +01:00
|
|
|
private static $registered_checks = [];
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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
|
|
|
|
*/
|
2017-03-21 11:57:07 +01:00
|
|
|
private static $registered_suites = [];
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (empty($this->config()->registered_suites[$suiteName])) {
|
|
|
|
// Not registered via config system, but it still may be configured later via self::register.
|
|
|
|
return;
|
|
|
|
}
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
$check = $this->config()->registered_checks[$checkName];
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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.
|
2017-01-04 23:16:12 +01:00
|
|
|
if (!empty($check['state']) && $check['state'] === 'disabled') {
|
2015-11-21 07:18:35 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
// Add the check to this suite.
|
|
|
|
$this->push($check['definition'], $check['title']);
|
|
|
|
}
|
|
|
|
}
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
return $result;
|
|
|
|
}
|
2015-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* Get instances of all the environment checks.
|
|
|
|
*
|
|
|
|
* @return array
|
2017-01-04 23:16:12 +01:00
|
|
|
* @throws InvalidArgumentException
|
2015-11-21 07:18:35 +01:00
|
|
|
*/
|
|
|
|
protected function checkInstances()
|
|
|
|
{
|
2017-03-21 11:57:07 +01:00
|
|
|
$output = [];
|
2015-11-21 07:18:35 +01:00
|
|
|
foreach ($this->checks as $check) {
|
|
|
|
list($checkClass, $checkTitle) = $check;
|
|
|
|
if (is_string($checkClass)) {
|
2017-05-26 18:11:35 +02:00
|
|
|
$checkInst = Injector::inst()->create($checkClass);
|
2015-11-21 07:18:35 +01:00
|
|
|
if ($checkInst instanceof EnvironmentCheck) {
|
2017-03-21 11:57:07 +01:00
|
|
|
$output[] = [$checkInst, $checkTitle];
|
2015-11-21 07:18:35 +01:00
|
|
|
} else {
|
2017-01-04 23:16:12 +01:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
"Bad EnvironmentCheck: '$checkClass' - the named class doesn't implement EnvironmentCheck"
|
|
|
|
);
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
|
|
|
} elseif ($checkClass instanceof EnvironmentCheck) {
|
2017-03-21 11:57:07 +01:00
|
|
|
$output[] = [$checkClass, $checkTitle];
|
2015-11-21 07:18:35 +01:00
|
|
|
} else {
|
|
|
|
throw new InvalidArgumentException("Bad EnvironmentCheck: " . var_export($check, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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);
|
|
|
|
}
|
2017-03-21 11:57:07 +01:00
|
|
|
$this->checks[] = [$check, $title];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2015-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////
|
2015-03-12 02:58:03 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-03-21 11:57:07 +01:00
|
|
|
protected static $instances = [];
|
2015-03-12 02:58:03 +01:00
|
|
|
|
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];
|
|
|
|
}
|
2012-01-20 18:10:48 +01:00
|
|
|
|
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)) {
|
2017-03-21 11:57:07 +01:00
|
|
|
$names = [$names];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2017-01-04 23:16:12 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
foreach ($names as $name) {
|
|
|
|
self::inst($name)->push($check, $title);
|
|
|
|
}
|
|
|
|
}
|
2015-11-20 00:01:21 +01:00
|
|
|
|
2015-11-21 07:18:35 +01:00
|
|
|
/**
|
|
|
|
* Unregisters all checks.
|
|
|
|
*/
|
|
|
|
public static function reset()
|
|
|
|
{
|
2017-03-21 11:57:07 +01:00
|
|
|
self::$instances = [];
|
2015-11-21 07:18:35 +01:00
|
|
|
}
|
2012-01-20 18:10:48 +01:00
|
|
|
}
|