3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Api4\StatusPreference
;
17 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 abstract class CRM_Utils_Check_Component
{
22 * Get the configured status checks.
26 * @throws \API_Exception
27 * @throws \Civi\API\Exception\UnauthorizedException
29 public function getChecksConfig() {
30 if (!isset(Civi
::$statics[__FUNCTION__
])) {
31 Civi
::$statics[__FUNCTION__
] = (array) StatusPreference
::get(FALSE)
32 ->addWhere('domain_id', '=', 'current_domain')
33 ->execute()->indexBy('name');
35 return Civi
::$statics[__FUNCTION__
];
39 * Should these checks be run?
43 public function isEnabled() {
48 * Get the names of all check functions in this class
52 public function getAllChecks() {
53 return array_filter(get_class_methods($this), function($method) {
54 return $method !== 'checkAll' && strpos($method, 'check') === 0;
59 * Run all checks in this class.
61 * @param array $requestedChecks
62 * Optionally specify the names of specific checks requested, or leave empty to run all
63 * @param bool $includeDisabled
64 * Run checks that have been explicitly disabled (default false)
66 * @return CRM_Utils_Check_Message[]
68 * @throws API_Exception
69 * @throws \Civi\API\Exception\UnauthorizedException
71 public function checkAll($requestedChecks = [], $includeDisabled = FALSE) {
73 foreach ($this->getAllChecks() as $method) {
74 // Note that we should check if the test is disabled BEFORE running it in case it's disabled for performance.
75 if ($this->isRequested($method, $requestedChecks) && ($includeDisabled ||
!$this->isDisabled($method))) {
76 $messages = array_merge($messages, $this->$method($includeDisabled));
83 * Is this check one of those requested
85 * @param string $method
86 * @param array $requestedChecks
89 private function isRequested($method, $requestedChecks) {
90 if (!$requestedChecks) {
93 foreach ($requestedChecks as $name) {
94 if (strpos($name, $method) === 0) {
102 * Is the specified check disabled.
104 * @param string $method
108 * @throws \API_Exception
109 * @throws \Civi\API\Exception\UnauthorizedException
111 public function isDisabled($method) {
112 $checks = $this->getChecksConfig();
113 if (isset($checks[$method]['is_active'])) {
114 return !$checks[$method]['is_active'];
120 * Check if file exists on given URL.
123 * @param float|bool $timeoutOverride
126 * @throws \GuzzleHttp\Exception\GuzzleException
128 public function fileExists($url, $timeoutOverride = FALSE) {
129 // Timeout past in maybe 0 in which case we should still permit it (0 is infinite).
130 if (!$timeoutOverride && $timeoutOverride !== 0) {
131 $timeoutOverride = (float) Civi
::settings()->get('http_timeout');
135 $guzzleClient = new GuzzleHttp\
Client();
136 $guzzleResponse = $guzzleClient->request('GET', $url, array(
137 'timeout' => $timeoutOverride,
139 $fileExists = ($guzzleResponse->getStatusCode() == 200);
141 catch (Exception
$e) {
142 // At this stage we are not checking for variants of not being able to receive it.
143 // However, we might later enhance this to distinguish forbidden from a 500 error.