-
Notifications
You must be signed in to change notification settings - Fork 1
Enumerated update statuses instead of binary #12
base: 8.x-1.x
Are you sure you want to change the base?
Changes from all commits
58f13f7
66f3162
667eb92
6d398f8
5d743c6
c68e1f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,12 @@ class Updater implements UpdaterInterface { | |
|
||
use StringTranslationTrait; | ||
|
||
const CONFIG_NOT_FOUND = 0; | ||
const CONFIG_ALREADY_APPLIED = 1; | ||
const CONFIG_NOT_EXPECTED = 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This name is a bit misleading. When I read it, I would expect it's the case when: What do you think? |
||
const CONFIG_APPLIED_SUCCESSFULLY = 3; | ||
const MODULES_FOUND = 4; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would leave out |
||
const MODULES_NOT_FOUND = 5; | ||
/** | ||
* Site configFactory object. | ||
* | ||
|
@@ -130,7 +136,7 @@ protected function logInfo($message) { | |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function executeUpdate($module, $update_definition_name) { | ||
public function executeUpdate($module, $update_definition_name, $force = FALSE) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep adding of |
||
$this->warningCount = 0; | ||
|
||
$update_definitions = $this->configHandler->loadUpdate($module, $update_definition_name); | ||
|
@@ -141,7 +147,7 @@ public function executeUpdate($module, $update_definition_name) { | |
} | ||
|
||
if (!empty($update_definitions)) { | ||
$this->executeConfigurationActions($update_definitions); | ||
$this->executeConfigurationActions($update_definitions, $force); | ||
} | ||
|
||
// Dispatch event after update has finished. | ||
|
@@ -151,6 +157,44 @@ public function executeUpdate($module, $update_definition_name) { | |
return $this->warningCount === 0; | ||
} | ||
|
||
/** | ||
* Check update status of configuration from update definitions. | ||
* | ||
* @param string $module | ||
* Module name where update definition is saved. | ||
* @param string $update_definition_name | ||
* Update definition name. Usually same name as update hook. | ||
* | ||
* @return bool | ||
* Returns update status. | ||
*/ | ||
public function checkUpdate($module, $update_definition_name) { | ||
$this->warningCount = 0; | ||
$moduleHandler = \Drupal::service('module_handler'); | ||
$modulesInstalled = []; | ||
$update_definitions = $this->configHandler->loadUpdate($module, $update_definition_name); | ||
if (isset($update_definitions[UpdateDefinitionInterface::GLOBAL_ACTIONS])) { | ||
if (isset($update_definitions[UpdateDefinitionInterface::GLOBAL_ACTIONS][UpdateDefinitionInterface::GLOBAL_ACTION_INSTALL_MODULES])) { | ||
$modules = $update_definitions[UpdateDefinitionInterface::GLOBAL_ACTIONS][UpdateDefinitionInterface::GLOBAL_ACTION_INSTALL_MODULES]; | ||
foreach ($modules as $module) { | ||
if (!$moduleHandler->moduleExists($module)) return Updater::MODULES_NOT_FOUND; | ||
$modulesInstalled[] = $module; | ||
} | ||
} | ||
unset($update_definitions[UpdateDefinitionInterface::GLOBAL_ACTIONS]); | ||
} | ||
|
||
if (!empty($update_definitions)) { | ||
return $this->executeConfigurationActions($update_definitions, FALSE, TRUE); | ||
} | ||
|
||
if (empty($update_definitions) && !empty($modulesInstalled)) { | ||
return Updater::MODULES_FOUND; | ||
} | ||
|
||
return Updater::CONFIG_NOT_FOUND; | ||
} | ||
|
||
/** | ||
* Get array with defined global actions. | ||
* | ||
|
@@ -176,8 +220,15 @@ protected function executeGlobalActions(array $global_actions) { | |
* | ||
* @param array $update_definitions | ||
* List of configurations with update definitions for them. | ||
* @param bool $force | ||
* Force the update. | ||
* @param bool $checkOnly | ||
* Check the update status and don't apply the update. | ||
* | ||
* @return bool | ||
* Returns update status if checkOnly flag is set. | ||
*/ | ||
protected function executeConfigurationActions(array $update_definitions) { | ||
protected function executeConfigurationActions(array $update_definitions, $force = FALSE, $checkOnly = FALSE) { | ||
foreach ($update_definitions as $configName => $configChange) { | ||
$update_actions = $configChange['update_actions']; | ||
|
||
|
@@ -198,15 +249,33 @@ protected function executeConfigurationActions(array $update_definitions) { | |
$new_config = NestedArray::mergeDeep($new_config, $update_actions['add']); | ||
} | ||
|
||
if ($this->updateConfig($configName, $new_config, $configChange['expected_config'], $delete_keys)) { | ||
$this->logInfo($this->t('Configuration @configName has been successfully updated.', ['@configName' => $configName])); | ||
$result = $this->updateConfig($configName, $new_config, $configChange['expected_config'], $delete_keys, $force, $checkOnly); | ||
|
||
if ($checkOnly) { | ||
return $result; | ||
} | ||
else { | ||
$this->logWarning($this->t('Unable to update configuration for @configName.', ['@configName' => $configName])); | ||
|
||
switch ($result) { | ||
case Updater::CONFIG_APPLIED_SUCCESSFULLY: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just one quick though - I'm not sure can we somehow propagate What do you think? |
||
$this->logInfo($this->t('Configuration @configName has been successfully updated.', ['@configName' => $configName])); | ||
break; | ||
|
||
case Updater::CONFIG_ALREADY_APPLIED: | ||
$this->logWarning($this->t('Configuration @configName is already updated.', ['@configName' => $configName])); | ||
break; | ||
|
||
case Updater::CONFIG_NOT_EXPECTED: | ||
$this->logWarning($this->t('Expected current configuration for @configName is not matching. Unable to apply new config.', ['@configName' => $configName])); | ||
break; | ||
|
||
case Updater::CONFIG_NOT_FOUND: | ||
$this->logWarning($this->t('Unable to find config @configName. Skipping update.', ['@configName' => $configName])); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Installs modules. | ||
* | ||
|
@@ -321,41 +390,54 @@ protected function getFlatKeys(array $nested_array) { | |
* Only if current config is same like old config we are updating. | ||
* @param array $delete_keys | ||
* List of parent keys to remove. @see NestedArray::unsetValue() | ||
* @param bool $force | ||
* Force the update. | ||
* @param bool $checkOnly | ||
* Check the update status and don't apply the update. | ||
* | ||
* @return bool | ||
* Returns TRUE if update of configuration was successful. | ||
*/ | ||
protected function updateConfig($config_name, array $configuration, array $expected_configuration = [], array $delete_keys = []) { | ||
protected function updateConfig($config_name, array $configuration, array $expected_configuration = [], array $delete_keys = [], $force = FALSE, $checkOnly = FALSE) { | ||
$config = $this->configFactory->getEditable($config_name); | ||
|
||
$config_data = $config->get(); | ||
|
||
// Reset expected_config in case of force flag. | ||
if ($force) { | ||
$expected_configuration = []; | ||
} | ||
|
||
// Check that configuration exists before executing update. | ||
if (empty($config_data)) { | ||
return FALSE; | ||
return Updater::CONFIG_NOT_FOUND; | ||
} | ||
|
||
// Check if configuration is already in new state. | ||
$merged_data = NestedArray::mergeDeep($expected_configuration, $configuration); | ||
if (empty(DiffArray::diffAssocRecursive($merged_data, $config_data))) { | ||
return TRUE; | ||
if (!$force && empty(DiffArray::diffAssocRecursive($configuration, $config_data))) { | ||
return Updater::CONFIG_ALREADY_APPLIED; | ||
} | ||
|
||
if (!empty($expected_configuration) && DiffArray::diffAssocRecursive($expected_configuration, $config_data)) { | ||
return FALSE; | ||
} | ||
if (empty($expected_configuration) || !DiffArray::diffAssocRecursive($expected_configuration, $config_data)) { | ||
// Delete configuration keys from config. | ||
if($checkOnly){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that I understand the purpose of Additionally, it's not a real representation of an update result, because |
||
return Updater::CONFIG_APPLIED_SUCCESSFULLY; | ||
} | ||
|
||
// Delete configuration keys from config. | ||
if (!empty($delete_keys)) { | ||
foreach ($delete_keys as $key_path) { | ||
NestedArray::unsetValue($config_data, $key_path); | ||
if (!empty($delete_keys)) { | ||
foreach ($delete_keys as $key_path) { | ||
NestedArray::unsetValue($config_data, $key_path); | ||
} | ||
} | ||
} | ||
|
||
$config->setData(NestedArray::mergeDeep($config_data, $configuration)); | ||
$config->save(); | ||
$config->setData(NestedArray::mergeDeep($config_data, $configuration)); | ||
$config->save(); | ||
|
||
return TRUE; | ||
return Updater::CONFIG_APPLIED_SUCCESSFULLY; | ||
}else{ | ||
return Updater::CONFIG_NOT_EXPECTED; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if we need to check for
$entry
in combination with$status
, because it's already checked before with this statement:$status = ($entry && $entry->wasSuccessfulByHook()) ? TRUE : FALSE;