diff --git a/automatic_updates.install b/automatic_updates.install index cac4498..d7ac9f8 100644 --- a/automatic_updates.install +++ b/automatic_updates.install @@ -14,9 +14,25 @@ use Drupal\Core\Url; */ function automatic_updates_requirements() { $requirements = []; + _automatic_updates_checker_requirements($requirements); + _automatic_updates_psa_requirements($requirements); + return $requirements; +} - $checker_results = \Drupal::service('automatic_updates.readiness_checker')->results(); - $last_check_timestamp = \Drupal::service('automatic_updates.readiness_checker')->timestamp(); +/** + * Display requirements from results of readiness checker. + * + * @param array $requirements + * The requirements array. + */ +function _automatic_updates_checker_requirements(array &$requirements) { + /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */ + $checker = \Drupal::service('automatic_updates.readiness_checker'); + if (!$checker->isEnabled()) { + return; + } + $checker_results = $checker->results(); + $last_check_timestamp = $checker->timestamp(); $requirements['automatic_updates_readiness'] = [ 'title' => t('Update readiness checks'), 'severity' => REQUIREMENT_OK, @@ -50,9 +66,17 @@ function automatic_updates_requirements() { $requirements['automatic_updates_readiness']['description'] = t('Readiness checks were last run @time ago.', ['@time' => $time_ago]); } } +} +/** + * Display requirements from public service announcements. + * + * @param array $requirements + * The requirements array. + */ +function _automatic_updates_psa_requirements(array &$requirements) { if (!\Drupal::config('automatic_updates.settings')->get('enable_psa')) { - return $requirements; + return; } /** @var \Drupal\automatic_updates\Services\AutomaticUpdatesPsa $psa */ $psa = \Drupal::service('automatic_updates.psa'); @@ -70,5 +94,4 @@ function automatic_updates_requirements() { '#items' => $messages, ]; } - return $requirements; } diff --git a/automatic_updates.module b/automatic_updates.module index aa075e7..ea7c5ea 100644 --- a/automatic_updates.module +++ b/automatic_updates.module @@ -54,5 +54,7 @@ function automatic_updates_page_top(array &$page_top) { * Implements hook_cron(). */ function automatic_updates_cron() { - \Drupal::service('automatic_updates.readiness_checker')->run(); + /** @var \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker */ + $checker = \Drupal::service('automatic_updates.readiness_checker'); + $checker->run(); } diff --git a/automatic_updates.services.yml b/automatic_updates.services.yml index 4a5ffe0..f3c0964 100644 --- a/automatic_updates.services.yml +++ b/automatic_updates.services.yml @@ -19,7 +19,9 @@ services: - '@logger.channel.automatic_updates' automatic_updates.readiness_checker: class: Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManager - arguments: ['@state'] + arguments: + - '@state' + - '@config.factory' tags: - { name: service_collector, tag: readiness_checker, call: addChecker } automatic_updates.readonly_checker: diff --git a/config/install/automatic_updates.settings.yml b/config/install/automatic_updates.settings.yml index 085f065..01c1bf1 100644 --- a/config/install/automatic_updates.settings.yml +++ b/config/install/automatic_updates.settings.yml @@ -3,3 +3,4 @@ # https://www.drupal.org/project/automatic_updates/issues/3045273 psa_endpoint: 'http://localhost/automatic_updates/test-json' enable_psa: true +enable_readiness_checks: true diff --git a/config/schema/automatic_updates.schema.yml b/config/schema/automatic_updates.schema.yml index ca29403..eeeb8c8 100644 --- a/config/schema/automatic_updates.schema.yml +++ b/config/schema/automatic_updates.schema.yml @@ -8,3 +8,6 @@ automatic_updates.settings: enable_psa: type: boolean label: 'Enable PSA notices' + enable_readiness_checks: + type: boolean + label: 'Enable readiness checks' diff --git a/src/Controller/ReadinessCheckerController.php b/src/Controller/ReadinessCheckerController.php index 342267f..e91e35a 100644 --- a/src/Controller/ReadinessCheckerController.php +++ b/src/Controller/ReadinessCheckerController.php @@ -4,14 +4,13 @@ namespace Drupal\automatic_updates\Controller; use Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface; use Drupal\Core\Controller\ControllerBase; -use Drupal\Core\StringTranslation\StringTranslationTrait; +use Drupal\Core\StringTranslation\TranslationInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Class ReadinessCheckerController. */ class ReadinessCheckerController extends ControllerBase { - use StringTranslationTrait; /** * The readiness checker. @@ -25,9 +24,12 @@ class ReadinessCheckerController extends ControllerBase { * * @param \Drupal\automatic_updates\ReadinessChecker\ReadinessCheckerManagerInterface $checker * The readiness checker. + * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation + * The string translation service. */ - public function __construct(ReadinessCheckerManagerInterface $checker) { + public function __construct(ReadinessCheckerManagerInterface $checker, TranslationInterface $string_translation) { $this->checker = $checker; + $this->stringTranslation = $string_translation; } /** @@ -35,7 +37,8 @@ class ReadinessCheckerController extends ControllerBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('automatic_updates.readiness_checker') + $container->get('automatic_updates.readiness_checker'), + $container->get('string_translation') ); } diff --git a/src/Form/AdminForm.php b/src/Form/AdminForm.php index d3979f4..ed51f7d 100644 --- a/src/Form/AdminForm.php +++ b/src/Form/AdminForm.php @@ -30,9 +30,7 @@ class AdminForm extends ConfigFormBase { * {@inheritdoc} */ public static function create(ContainerInterface $container) { - $instance = new static( - $container->get('config.factory') - ); + $instance = parent::create($container); $instance->checker = $container->get('automatic_updates.readiness_checker'); $instance->dateFormatter = $container->get('date.formatter'); return $instance; @@ -65,12 +63,17 @@ class AdminForm extends ConfigFormBase { '#default_value' => $config->get('enable_psa'), ]; $last_check_timestamp = $this->checker->timestamp(); - $form['run_checks'] = [ - '#markup' => '

' . t('Readiness checks were last run @time ago. Run the readiness checks manually.', [ + $form['enable_readiness_checks'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Check the readiness of automatically updating the site.'), + '#default_value' => $config->get('enable_readiness_checks'), + ]; + if ($this->checker->isEnabled()) { + $form['enable_readiness_checks']['#description'] = $this->t('Readiness checks were last run @time ago. Run the readiness checks manually.', [ '@time' => $this->dateFormatter->formatTimeDiffSince($last_check_timestamp), '@link' => Url::fromRoute('automatic_updates.update_readiness')->toString(), - ]) . '

', - ]; + ]); + } return parent::buildForm($form, $form_state); } diff --git a/src/ReadinessChecker/ReadinessCheckerManager.php b/src/ReadinessChecker/ReadinessCheckerManager.php index 8ca7af8..21f33e5 100644 --- a/src/ReadinessChecker/ReadinessCheckerManager.php +++ b/src/ReadinessChecker/ReadinessCheckerManager.php @@ -2,6 +2,7 @@ namespace Drupal\automatic_updates\ReadinessChecker; +use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\State\StateInterface; /** @@ -16,6 +17,13 @@ class ReadinessCheckerManager implements ReadinessCheckerManagerInterface { */ protected $keyValue; + /** + * The config factory. + * + * @var \Drupal\Core\Config\ConfigFactoryInterface + */ + protected $configFactory; + /** * An unsorted array of arrays of active checkers. * @@ -31,9 +39,12 @@ class ReadinessCheckerManager implements ReadinessCheckerManagerInterface { * * @param \Drupal\Core\State\StateInterface $key_value * The key/value service. + * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The config factory. */ - public function __construct(StateInterface $key_value) { + public function __construct(StateInterface $key_value, ConfigFactoryInterface $config_factory) { $this->keyValue = $key_value; + $this->configFactory = $config_factory; } /** @@ -49,6 +60,9 @@ class ReadinessCheckerManager implements ReadinessCheckerManagerInterface { */ public function run() { $messages = []; + if (!$this->isEnabled()) { + return $messages; + } foreach ($this->getSortedCheckers() as $checker) { $messages = array_merge($messages, $checker->run()); } @@ -71,6 +85,13 @@ class ReadinessCheckerManager implements ReadinessCheckerManagerInterface { return $this->keyValue->get('readiness_check_timestamp', 0); } + /** + * {@inheritdoc} + */ + public function isEnabled() { + return $this->configFactory->get('automatic_updates.settings')->get('enable_readiness_checks'); + } + /** * Sorts checkers according to priority. * diff --git a/src/ReadinessChecker/ReadinessCheckerManagerInterface.php b/src/ReadinessChecker/ReadinessCheckerManagerInterface.php index 739f06b..05870f1 100644 --- a/src/ReadinessChecker/ReadinessCheckerManagerInterface.php +++ b/src/ReadinessChecker/ReadinessCheckerManagerInterface.php @@ -48,4 +48,12 @@ interface ReadinessCheckerManagerInterface { */ public function timestamp(); + /** + * Determine if readiness checks is enabled. + * + * @return bool + * TRUE if enabled, otherwise FALSE. + */ + public function isEnabled(); + }