Skip to content

Commit

Permalink
Merge pull request #21400 from eileenmcnaughton/val
Browse files Browse the repository at this point in the history
dev/core#2823 Move validation into validation function
  • Loading branch information
colemanw authored Sep 11, 2021
2 parents 9662d72 + 5e8660a commit d8538fc
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->isModuleEnabled($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->isModuleRecognised($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 isModuleRecognised(string $module): bool {
return $this->isModuleDisabled($module) || $this->isModuleEnabled($module);
}

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

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

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

0 comments on commit d8538fc

Please sign in to comment.