Skip to content

Commit

Permalink
dev/core#2823 Move validation into validation function
Browse files Browse the repository at this point in the history
As the code comments suggest the handling of a module being unrecognised should
be handled in the validate not the enable function

I find the moduleIndex pretty nasty - all known modules
are indexed into either the TRUE key or the FALSE
key depending on whether they are enabled or not.

I'm focussing on adding getters to access that info for now
& will later change the structure when the code is not interacting
directly with the property
  • Loading branch information
eileenmcnaughton committed Sep 8, 2021
1 parent 13b45ac commit a76754a
Showing 1 changed file with 39 additions and 10 deletions.
49 changes: 39 additions & 10 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,9 @@ public function reconcileEnabledModules() {
// index by moduleName,name
$decls = $this->createDeclarationIndex($this->moduleIndex, $this->getDeclarations());
foreach ($decls as $moduleName => $todos) {
if (isset($this->moduleIndex[TRUE][$moduleName])) {
if ($this->moduleIsEnabled($moduleName)) {
$this->reconcileEnabledModule($this->moduleIndex[TRUE][$moduleName], $todos);
}
elseif (isset($this->moduleIndex[FALSE][$moduleName])) {
// do nothing -- module should get swept up later
}
else {
throw new Exception("Entity declaration references invalid or inactive module name [$moduleName]");
}
}
}

Expand Down Expand Up @@ -443,18 +437,53 @@ protected function createDeclarationIndex($moduleIndex, $declarations) {
* string on error, or FALSE
*/
protected function validate($declarations) {
foreach ($declarations as $declare) {
foreach ($declarations as $module => $declare) {
foreach (['name', 'module', 'entity', 'params'] as $key) {
if (empty($declare[$key])) {
$str = print_r($declare, TRUE);
return ("Managed Entity is missing field \"$key\": $str");
return ts('Managed Entity (%1) is missing field "%2": %3', [$module, $key, $str]);
}
}
// FIXME: validate that each 'module' is known
if ($this->moduleIsRecognised($declare['module'])) {
return ts('Entity declaration references invalid or inactive module name [%1]', [$declare['module']]);
}
}
return FALSE;
}

/**
* Is the module recognised (as an enabled or disabled extension in the system).
*
* @param string $module
*
* @return bool
*/
protected function moduleIsRecognised(string $module): bool {
return $this->moduleIsDisabled($module) || $this->moduleIsEnabled($module);
}

/**
* Is the module enabled.
*
* @param string $module
*
* @return bool
*/
protected function moduleIsEnabled(string $module): bool {
return isset($this->moduleIndex[TRUE][$module]);
}

/**
* Is the module disabled.
*
* @param string $module
*
* @return bool
*/
protected function moduleIsDisabled(string $module): bool {
return isset($this->moduleIndex[FALSE][$module]);
}

/**
* @param array $declarations
*
Expand Down

0 comments on commit a76754a

Please sign in to comment.