Skip to content

Commit

Permalink
civimix-schema - Make it reloadable to work in multi-module/multi-dow…
Browse files Browse the repository at this point in the history
…nload scenarios
  • Loading branch information
totten committed Feb 17, 2024
1 parent 73f2f05 commit 266a816
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 74 deletions.
39 changes: 33 additions & 6 deletions mixin/lib/civimix-schema/pathload.main.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
<?php
namespace CiviMix\Schema;

\pathload()->activatePackage('civicrm-schema@5' , __DIR__, [
'autoload' => [
'psr-4' => [
'CiviMix\\Schema\\' => ['src/'],
]
]
\pathload()->activatePackage('civimix-schema@5', __DIR__, [
'reloadable' => TRUE,
]);

// Any classname matching `CiviMix\Schema\Automatic\*` will be aliased to our implementation.
$GLOBALS['CiviMixSchema5']['aliases'] = [
'CiviMix\\Schema\\Automatic\\' => __DIR__ . '/src/Automatic.php',
'CiviMix\\Schema\\SqlInstaller\\' => __DIR__ . '/src/SqlInstaller.php',
];

$GLOBALS['CiviMixSchema5']['loader'] = function(string $class) {
if ('CiviMix\\Schema\\' === substr($class, 0, 15)) {
// This package is designed for backported use in download/activation,
// workflows, where new revisions may become dynamically available.
// Ensure we have the latest revision.
pathload()->loadPackage('civimix-schema@5', TRUE);
foreach ($GLOBALS['CiviMixSchema5']['aliases'] as $prefix => $absPath) {
if (strpos($class, $prefix) === 0) {
class_alias(get_class(require $absPath), $class);
}
}
}
};

// \CiviMix\Schema\loader() is a facade. It is expected to remain identical across versions.
if (!function_exists(__NAMESPACE__ . '\loader')) {

function loader(string $class) {
return call_user_func($GLOBALS['CiviMixSchema5']['loader'], $class);
}

spl_autoload_register(__NAMESPACE__ . '\loader');
}
23 changes: 18 additions & 5 deletions mixin/lib/civimix-schema/src/Automatic.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
* using XML schema files (`SqlInstaller`). It also calls-out to any custom
* upgrade code (eg `CRM_Myext_Upgrader`).
*
* To simplify backport considerations, `Automatic` does not support subclassing.
* To simplify backport considerations, `Automatic` does not have formal name.
* It is accessed via aliases like "CiviMix\Schema\Automatic\Mosaico".
*
* Target: CiviCRM v5.38+
*/
final class Automatic implements \CRM_Extension_Upgrader_Interface {
return new class() implements \CRM_Extension_Upgrader_Interface {

use \CRM_Extension_Upgrader_IdentityTrait {

use IdentityTrait {
init as initIdentity;

}

/**
Expand All @@ -32,7 +35,7 @@ final class Automatic implements \CRM_Extension_Upgrader_Interface {
public function init(array $params) {
$this->initIdentity($params);
if ($info = $this->getInfo()) {
$this->sqlInstaller = new SqlInstaller();
$this->sqlInstaller = (require __DIR__ . '/SqlInstaller.php');
$this->sqlInstaller->init($params);

if ($class = $this->getDelegateUpgraderClass($info)) {
Expand Down Expand Up @@ -88,4 +91,14 @@ public function getDelegateUpgraderClass(\CRM_Extension_Info $info): ?string {
return NULL;
}

}
public function getInfo(): ?\CRM_Extension_Info {
try {
return \CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->extensionName);
}
catch (\CRM_Extension_Exception_ParseException $e) {
\Civi::log()->error("Parse error in extension " . $this->extensionName . ": " . $e->getMessage());
return NULL;
}
}

};
59 changes: 0 additions & 59 deletions mixin/lib/civimix-schema/src/IdentityTrait.php

This file was deleted.

18 changes: 14 additions & 4 deletions mixin/lib/civimix-schema/src/SqlInstaller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
* Details such as character-sets and collations are determined dynamically,
* to match the local system.
*
* To simplify backport considerations, `SqlInstaller` does not support subclassing.
* To simplify backport considerations, `SqlInstaller` does not have formal name.
*
* Target: CiviCRM v5.38+
*/
final class SqlInstaller implements \CRM_Extension_Upgrader_Interface {
return new class() implements \CRM_Extension_Upgrader_Interface {

use IdentityTrait;
use \CRM_Extension_Upgrader_IdentityTrait;

public function notify(string $event, array $params = []) {
switch ($event) {
Expand Down Expand Up @@ -165,4 +165,14 @@ private function getDefaultDatabase(): array {
];
}

}
public function getInfo(): ?\CRM_Extension_Info {
try {
return \CRM_Extension_System::singleton()->getMapper()->keyToInfo($this->extensionName);
}
catch (\CRM_Extension_Exception_ParseException $e) {
\Civi::log()->error("Parse error in extension " . $this->extensionName . ": " . $e->getMessage());
return NULL;
}
}

};

0 comments on commit 266a816

Please sign in to comment.