Merge pull request #19063 from christianwach/lab-core-2213
[civicrm-core.git] / CRM / Utils / Check / Component.php
index 812d75fea9f9840b0c8a342dd3dd85ef50958d24..2f57004adeac08cab6f600a86befab37e61c55f3 100644 (file)
@@ -18,11 +18,6 @@ use Civi\Api4\StatusPreference;
  */
 abstract class CRM_Utils_Check_Component {
 
-  /**
-   * @var array
-   */
-  public $checksConfig = [];
-
   /**
    * Get the configured status checks.
    *
@@ -32,20 +27,12 @@ abstract class CRM_Utils_Check_Component {
    * @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__];
   }
 
   /**
@@ -57,26 +44,60 @@ abstract class CRM_Utils_Check_Component {
     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.
    *