*/
abstract class CRM_Utils_Check_Component {
- /**
- * @var array
- */
- public $checksConfig = [];
-
/**
* Get the configured status checks.
*
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function getChecksConfig() {
- if (empty($this->checksConfig)) {
- $this->checksConfig = Civi::cache('checks')->get('checksConfig', []);
- if (empty($this->checksConfig)) {
- $this->checksConfig = StatusPreference::get(FALSE)->execute()->indexBy('name');
- }
+ if (!isset(Civi::$statics[__FUNCTION__])) {
+ Civi::$statics[__FUNCTION__] = (array) StatusPreference::get(FALSE)
+ ->addWhere('domain_id', '=', 'current_domain')
+ ->execute()->indexBy('name');
}
- return $this->checksConfig;
- }
-
- /**
- * @param array $checksConfig
- */
- public function setChecksConfig(array $checksConfig) {
- $this->checksConfig = $checksConfig;
+ return Civi::$statics[__FUNCTION__];
}
/**
return TRUE;
}
+ /**
+ * Get the names of all check functions in this class
+ *
+ * @return string[]
+ */
+ public function getAllChecks() {
+ return array_filter(get_class_methods($this), function($method) {
+ return $method !== 'checkAll' && strpos($method, 'check') === 0;
+ });
+ }
+
/**
* Run all checks in this class.
*
- * @return array
- * [CRM_Utils_Check_Message]
+ * @param array $requestedChecks
+ * Optionally specify the names of specific checks requested, or leave empty to run all
+ * @param bool $includeDisabled
+ * Run checks that have been explicitly disabled (default false)
*
- * @throws \API_Exception
+ * @return CRM_Utils_Check_Message[]
+ *
+ * @throws API_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
- public function checkAll() {
+ public function checkAll($requestedChecks = [], $includeDisabled = FALSE) {
$messages = [];
- foreach (get_class_methods($this) as $method) {
+ foreach ($this->getAllChecks() as $method) {
// Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
- if ($method !== 'checkAll' && strpos($method, 'check') === 0 && !$this->isDisabled($method)) {
- $messages = array_merge($messages, $this->$method());
+ if ($this->isRequested($method, $requestedChecks) && ($includeDisabled || !$this->isDisabled($method))) {
+ $messages = array_merge($messages, $this->$method($includeDisabled));
}
}
return $messages;
}
+ /**
+ * Is this check one of those requested
+ *
+ * @param string $method
+ * @param array $requestedChecks
+ * @return bool
+ */
+ private function isRequested($method, $requestedChecks) {
+ if (!$requestedChecks) {
+ return TRUE;
+ }
+ foreach ($requestedChecks as $name) {
+ if (strpos($name, $method) === 0) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
/**
* Is the specified check disabled.
*