*/
class CRM_Core_ManagedEntities {
+ /**
+ * Get clean up options.
+ *
+ * @return array
+ */
public static function getCleanupOptions() {
return array(
'always' => ts('Always'),
}
/**
- * @var array($status => array($name => CRM_Core_Module))
+ * @var array
+ * Array($status => array($name => CRM_Core_Module)).
*/
protected $moduleIndex;
/**
- * @var array per hook_civicrm_managed
+ * @var array
+ * List of all entity declarations.
+ * @see CRM_Utils_Hook::managed()
*/
protected $declarations;
/**
- * Get an instance
+ * Get an instance.
* @param bool $fresh
* @return \CRM_Core_ManagedEntities
*/
}
/**
- * Read the managed entity
+ * Read a managed entity using APIv3.
*
- * @return array|NULL API representation, or NULL if the entity does not exist
+ * @param string $moduleName
+ * The name of the module which declared entity.
+ * @param string $name
+ * The symbolic name of the entity.
+ * @return array|NULL
+ * API representation, or NULL if the entity does not exist
*/
public function get($moduleName, $name) {
$dao = new CRM_Core_DAO_Managed();
}
}
+ /**
+ * Identify any enabled/disabled modules. Add new entities, update
+ * existing entities, and remove orphaned (stale) entities.
+ *
+ * @throws Exception
+ */
public function reconcile() {
if ($error = $this->validate($this->getDeclarations())) {
throw new Exception($error);
$this->reconcileUnknownModules();
}
-
+ /**
+ * For all enabled modules, add new entities, update
+ * existing entities, and remove orphaned (stale) entities.
+ *
+ * @throws Exception
+ */
public function reconcileEnabledModules() {
// Note: any thing currently declared is necessarily from
// an active module -- because we got it from a hook!
}
/**
- * Create, update, and delete entities declared by an active module
+ * For one enabled module, add new entities, update existing entities,
+ * and remove orphaned (stale) entities.
*
- * @param \CRM_Core_Module|string $module string
+ * @param \CRM_Core_Module $module
* @param array $todos
- * $name => array().
+ * List of entities currently declared by this module.
+ * array(string $name => array $entityDef).
*/
public function reconcileEnabledModule(CRM_Core_Module $module, $todos) {
$dao = new CRM_Core_DAO_Managed();
}
}
+ /**
+ * For all disabled modules, disable any managed entities.
+ */
public function reconcileDisabledModules() {
if (empty($this->moduleIndex[FALSE])) {
return;
}
}
+ /**
+ * Remove any orphaned (stale) entities that are linked to
+ * unknown modules.
+ */
public function reconcileUnknownModules() {
$knownModules = array();
if (array_key_exists(0, $this->moduleIndex) && is_array($this->moduleIndex[0])) {
}
if (array_key_exists(1, $this->moduleIndex) && is_array($this->moduleIndex[1])) {
$knownModules = array_merge($knownModules, array_keys($this->moduleIndex[1]));
-
}
$dao = new CRM_Core_DAO_Managed();
}
/**
- * Create a new entity
+ * Create a new entity.
*
* @param array $todo
* Entity specification (per hook_civicrm_managedEntities).
$params = array_merge($defaults, $todo['params']);
$result = civicrm_api($dao->entity_type, 'create', $params);
if ($result['is_error']) {
- $this->onApiError($dao->entity_type, 'create',$params, $result);
+ $this->onApiError($dao->entity_type, 'create', $params, $result);
}
}
);
$result = civicrm_api($dao->entity_type, 'create', $params);
if ($result['is_error']) {
- $this->onApiError($dao->entity_type, 'create',$params, $result);
+ $this->onApiError($dao->entity_type, 'create', $params, $result);
}
}
}
/**
- * Remove a stale entity (if policy allows)
+ * Remove a stale entity (if policy allows).
*
* @param CRM_Core_DAO_Managed $dao
+ * @throws Exception
*/
public function removeStaleEntity($dao) {
$policy = empty($dao->cleanup) ? 'always' : $dao->cleanup;
case 'always':
$doDelete = TRUE;
break;
+
case 'never':
$doDelete = FALSE;
break;
+
case 'unused':
$getRefCount = civicrm_api3($dao->entity_type, 'getrefcount', array(
'debug' => 1,
$doDelete = ($total == 0);
break;
+
default:
throw new \Exception('Unrecognized cleanup policy: ' . $policy);
}
}
}
+ /**
+ * Get declarations.
+ *
+ * @return array|null
+ */
public function getDeclarations() {
if ($this->declarations === NULL) {
$this->declarations = array();
}
/**
- * @param $modules
+ * @param array $modules
+ * Array<CRM_Core_Module>.
*
* @return array
* indexed by is_active,name
}
/**
- * @param $moduleIndex
- * @param $declarations
+ * @param array $moduleIndex
+ * @param array $declarations
*
* @return array
* indexed by module,name
/**
* @param $declarations
*
- * @return mixed string on error, or FALSE
+ * @return string|bool
+ * string on error, or FALSE
*/
protected static function validate($declarations) {
foreach ($declarations as $declare) {
}
/**
- * @param $declarations
+ * @param array $declarations
*
- * @return mixed
+ * @return array
*/
protected static function cleanDeclarations($declarations) {
foreach ($declarations as $name => &$declare) {
));
throw new Exception('API error: ' . $result['error_message']);
}
+
}