chore: bootstrap module from working instance and add install guide

This commit is contained in:
Kelin Rescue Hub
2026-04-09 14:18:10 -04:00
parent 97ad7da6f4
commit 6cda47760e
35 changed files with 6267 additions and 4 deletions

View File

@@ -0,0 +1,81 @@
<?php
namespace humhub\modules\donations\services;
use humhub\modules\donations\models\DonationProviderConfig;
use humhub\modules\space\models\Space;
use Yii;
class ModuleSetupService
{
public static function runForSpace(Space $space): array
{
$result = static::applyModuleMigrations();
$config = DonationProviderConfig::findOne(['contentcontainer_id' => $space->contentcontainer_id]);
if (!$config instanceof DonationProviderConfig) {
$config = new DonationProviderConfig();
$config->contentcontainer_id = $space->contentcontainer_id;
$config->sandbox_mode = 1;
$config->default_currency = 'USD';
$config->paypal_enabled = 0;
$config->stripe_enabled = 0;
$config->paypal_recurring_enabled = 0;
$config->stripe_recurring_enabled = 0;
$config->save(false);
$result['providerConfigCreated'] = true;
} else {
$result['providerConfigCreated'] = false;
}
return $result;
}
private static function applyModuleMigrations(): array
{
$migrationDir = dirname(__DIR__) . '/migrations';
$files = glob($migrationDir . '/m*.php') ?: [];
sort($files, SORT_NATURAL);
$existingVersions = Yii::$app->db->createCommand('SELECT version FROM migration')->queryColumn();
$history = array_fill_keys($existingVersions, true);
$applied = [];
$skipped = [];
foreach ($files as $file) {
$version = pathinfo($file, PATHINFO_FILENAME);
if (isset($history[$version])) {
$skipped[] = $version;
continue;
}
if (!class_exists($version, false)) {
require_once $file;
}
if (!class_exists($version, false)) {
throw new \RuntimeException('Migration class not found: ' . $version);
}
$migration = new $version();
$ok = method_exists($migration, 'safeUp') ? $migration->safeUp() : $migration->up();
if ($ok === false) {
throw new \RuntimeException('Migration failed: ' . $version);
}
Yii::$app->db->createCommand()->insert('migration', [
'version' => $version,
'apply_time' => time(),
])->execute();
$applied[] = $version;
$history[$version] = true;
}
return [
'applied' => $applied,
'skipped' => $skipped,
];
}
}