Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#2823 Extract code to load the declarations and call from the constructor #21399

Merged
merged 2 commits into from
Sep 11, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 56 additions & 21 deletions CRM/Core/ManagedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ function () {
* @param array $modules
* CRM_Core_Module.
* @param array $declarations
* Per hook_civicrm_managed.
* Per hook_civicrm_managed. Only ever passed in in unit tests - otherwise
* calculated.
*/
public function __construct($modules, $declarations) {
$this->moduleIndex = $this->createModuleIndex($modules);
Expand All @@ -73,7 +74,7 @@ public function __construct($modules, $declarations) {
$this->declarations = $this->cleanDeclarations($declarations);
}
else {
$this->declarations = NULL;
$this->loadDeclarations();
}
}

Expand Down Expand Up @@ -142,15 +143,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 @@ -385,15 +380,6 @@ protected function removeStaleEntity($dao) {
* @return array|null
*/
protected function getDeclarations() {
if ($this->declarations === NULL) {
$this->declarations = [];
foreach (CRM_Core_Component::getEnabledComponents() as $component) {
/** @var CRM_Core_Component_Info $component */
$this->declarations = array_merge($this->declarations, $component->getManagedEntities());
}
CRM_Utils_Hook::managed($this->declarations);
$this->declarations = $this->cleanDeclarations($this->declarations);
}
return $this->declarations;
}

Expand Down Expand Up @@ -443,18 +429,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 Expand Up @@ -508,4 +529,18 @@ private function isActivationSupported(string $entity_type): bool {
return Civi::$statics[__CLASS__][__FUNCTION__][$entity_type];
}

/**
* Load declarations into the class property.
*
* This picks it up from hooks and enabled components.
*/
protected function loadDeclarations(): void {
$this->declarations = [];
foreach (CRM_Core_Component::getEnabledComponents() as $component) {
$this->declarations = array_merge($this->declarations, $component->getManagedEntities());
}
CRM_Utils_Hook::managed($this->declarations);
$this->declarations = $this->cleanDeclarations($this->declarations);
}

}