Skip to content

Instantly share code, notes, and snippets.

@colemanw
Created February 9, 2024 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colemanw/86fac8d5c4c920e2e95a1cffaafefd2e to your computer and use it in GitHub Desktop.
Save colemanw/86fac8d5c4c920e2e95a1cffaafefd2e to your computer and use it in GitHub Desktop.
<?php
namespace Civi\Mixin\EntityTypesXmlV1;
class Loader {
private \CRM_Extension_MixInfo $mixInfo;
public function __construct(\CRM_Extension_MixInfo $mixInfo) {
$this->mixInfo = $mixInfo;
}
/**
* @return \SimpleXMLElement[]
*/
public function getEntityDefinitions(): array {
$definitions = [];
$files = (array) glob($this->mixInfo->getPath("xml/schema/CRM/*/*.xml"));
foreach ($files as $file) {
$dom = new \DomDocument();
$xmlString = file_get_contents($file);
$dom->loadXML($xmlString);
$definition = simplexml_import_dom($dom);
if (!$definition) {
throw new \CRM_Core_Exception("Unable to load entity file $file");
}
$definitions[] = $definition;
}
return $definitions;
}
}
/**
* Auto-register entity declarations from `xml/schema/...*.xml`.
*
* @mixinName entity-types-xml
* @mixinVersion 1.0.0
* @since 5.71
*
* @param CRM_Extension_MixInfo $mixInfo
* On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like.
* @param \CRM_Extension_BootCache $bootCache
* On newer deployments, this will be an instance of MixInfo. On older deployments, Civix may polyfill with a work-a-like.
*/
return function ($mixInfo, $bootCache) {
/**
* @param \Civi\Core\Event\GenericHookEvent $e
* @implements CRM_Utils_Hook::entityTypes
*/
Civi::dispatcher()->addListener('hook_civicrm_entityTypes', function ($e) use ($mixInfo) {
// When deactivating on a polyfill/pre-mixin system, listeners may not cleanup automatically.
if (!$mixInfo->isActive()) {
return;
}
$loader = new Loader($mixInfo);
foreach ($loader->getEntityDefinitions() as $definition) {
$shortName = trim((string) $definition->class);
$base = str_replace('/', '_', trim((string) $definition->base));
$e->entityTypes[$shortName] = [
'name' => $shortName,
'class' => $base . '_DAO_' . $shortName,
'table' => trim((string) $definition->name),
];
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment