mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-11 21:26:44 +00:00
0fd3e3b364 added seed_group_email_account.php beside seed_config.php, and
test_prefix_grouped_files reads a shared delimiter-bounded prefix as a
folder in disguise. Both move into files/php/seed/ with the prefix
dropped, and the two source references plus the README link follow.
The deployed file keeps its name: ESPOCRM_SCRIPT_SEED_FILE also feeds
ESPOCRM_SCRIPT_SEED_HOST_REL, which the Dockerfile takes as a build arg
for its COPY, so renaming it would reach into the image build for no gain
the lint asks for.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
166 lines
4.0 KiB
PHP
166 lines
4.0 KiB
PHP
<?php
|
|
// nocheck: mirrored-unit-test - requires /var/www/html/bootstrap.php at the top level
|
|
// and drives EspoCRM's own config container; nothing here runs without the install
|
|
/**
|
|
* Generic EspoCRM config seeder.
|
|
*
|
|
* Automatically scans all environment variables starting with ESPOCRM_SEED_,
|
|
* converts them into EspoCRM camelCase config keys, and writes them via ConfigWriter.
|
|
*
|
|
* Example:
|
|
* ESPOCRM_SEED_RECAPTCHA_SECRET_KEY=xyz
|
|
* becomes:
|
|
* recaptchaSecretKey => "xyz"
|
|
*/
|
|
|
|
require "/var/www/html/bootstrap.php";
|
|
|
|
$app = new \Espo\Core\Application();
|
|
$c = $app->getContainer();
|
|
$config = $c->get("config");
|
|
$writer = $c->get("injectableFactory")->create("\Espo\Core\Utils\Config\ConfigWriter");
|
|
|
|
/**
|
|
* Convert an ENV suffix like "RECAPTCHA_SECRET_KEY" to camelCase "recaptchaSecretKey".
|
|
*/
|
|
function to_camel_case(string $input): string
|
|
{
|
|
$input = strtolower($input);
|
|
$parts = explode('_', $input);
|
|
$result = array_shift($parts);
|
|
|
|
foreach ($parts as $part) {
|
|
$result .= ucfirst($part);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Normalize booleans if the value looks boolean-like, and decode a JSON object
|
|
* into a PHP array so nested config keys can travel in a single ENV value.
|
|
* Returns true/false for typical boolean strings, an array for a JSON object,
|
|
* otherwise the original string.
|
|
*/
|
|
function cast_value(string $value)
|
|
{
|
|
$normalized = strtolower(trim($value));
|
|
|
|
if (in_array($normalized, ['1', 'true', 'yes', 'on'], true)) {
|
|
return true;
|
|
}
|
|
|
|
if (in_array($normalized, ['0', 'false', 'no', 'off'], true)) {
|
|
return false;
|
|
}
|
|
|
|
if (str_starts_with(trim($value), '{')) {
|
|
$decoded = json_decode($value, true);
|
|
|
|
if (is_array($decoded)) {
|
|
return $decoded;
|
|
}
|
|
}
|
|
|
|
return $value; // keep as string
|
|
}
|
|
|
|
/**
|
|
* Simple debug logger to STDERR.
|
|
* This keeps STDOUT clean so automation can rely on "CHANGED"/"UNCHANGED".
|
|
*/
|
|
function seed_debug(string $message): void
|
|
{
|
|
fwrite(STDERR, "[seed] " . $message . PHP_EOL);
|
|
}
|
|
|
|
$debugEnv = getenv('ESPOCRM_SEEDER_DEBUG');
|
|
$debug = false;
|
|
if ($debugEnv !== false) {
|
|
$normalized = strtolower(trim($debugEnv));
|
|
$debug = in_array($normalized, ['1', 'true', 'yes', 'on'], true);
|
|
}
|
|
|
|
if ($debug) {
|
|
seed_debug("Seeder started, scanning ESPOCRM_SEED_* variables …");
|
|
}
|
|
|
|
$changed = false;
|
|
|
|
foreach ($_ENV as $envKey => $envValue) {
|
|
if (strpos($envKey, 'ESPOCRM_SEED_') !== 0) {
|
|
continue;
|
|
}
|
|
|
|
$rawKey = substr($envKey, strlen('ESPOCRM_SEED_')); // e.g. "RECAPTCHA_SECRET_KEY"
|
|
|
|
if ($rawKey === '') {
|
|
continue;
|
|
}
|
|
|
|
$configKey = to_camel_case($rawKey);
|
|
|
|
$value = cast_value((string) $envValue);
|
|
|
|
if ($debug) {
|
|
seed_debug(sprintf(
|
|
"ENV %s -> config key '%s' = %s",
|
|
$envKey,
|
|
$configKey,
|
|
is_array($value) ? '<redacted array>' : var_export($value, true)
|
|
));
|
|
}
|
|
|
|
$current = $config->get($configKey);
|
|
|
|
if ($current !== $value) {
|
|
if ($debug) {
|
|
seed_debug(sprintf(
|
|
"Updating '%s': %s -> %s",
|
|
$configKey,
|
|
var_export($current, true),
|
|
var_export($value, true)
|
|
));
|
|
}
|
|
|
|
$writer->set($configKey, $value);
|
|
$changed = true;
|
|
} else {
|
|
if ($debug) {
|
|
seed_debug(sprintf(
|
|
"No change for '%s' (already %s)",
|
|
$configKey,
|
|
var_export($current, true)
|
|
));
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($changed) {
|
|
if ($debug) {
|
|
seed_debug("Changes detected, saving configuration …");
|
|
}
|
|
try {
|
|
$writer->save();
|
|
} catch (\Throwable $exc) {
|
|
fwrite(STDERR, sprintf(
|
|
"[seed] save failed: %s: %s (%s:%d)" . PHP_EOL,
|
|
get_class($exc),
|
|
$exc->getMessage(),
|
|
$exc->getFile(),
|
|
$exc->getLine()
|
|
));
|
|
exit(1);
|
|
}
|
|
echo "CHANGED\n";
|
|
} else {
|
|
if ($debug) {
|
|
seed_debug("No changes detected.");
|
|
}
|
|
echo "UNCHANGED\n";
|
|
}
|
|
|
|
if ($debug) {
|
|
seed_debug("Seeder finished.");
|
|
}
|