Files
WKVS/setup/init-env.php
T

90 lines
3.1 KiB
PHP

<?php
/**
* WKVS Automated Environment Initialization Script
* Auto-creates missing config/.env files from .example templates
* and configures them with valid Docker container hostnames and default dev credentials.
*/
$configDir = dirname(__DIR__) . '/config';
$envMappings = [
'env.redis' => [
'example' => 'env.redis.example',
'replacements' => [
'/REDDIS_HOST=127\.0\.0\.1/' => 'REDDIS_HOST=redis',
'/REDDIS_HOST=localhost/' => 'REDDIS_HOST=redis',
'/REDDIS_HOST=YOUR_REDDIS_IP.*/' => 'REDDIS_HOST=redis',
'/REDDIS_PORT=YOUR_REDDIS_PORT.*/' => 'REDDIS_PORT=6379',
'/YOUR_REDDIS_PASSWORD/' => base64_encode(random_bytes(64)),
]
],
'env.db' => [
'example' => 'env.db.example',
'replacements' => [
'/MYSQL_HOST=localhost/' => 'MYSQL_HOST=mariadb',
'/MYSQL_HOST=127\.0\.0\.1/' => 'MYSQL_HOST=mariadb',
'/YOUR_PASSWORD_FOR_ROOT/' => base64_encode(random_bytes(64)),
'/YOUR_DATABASE_NAME/' => 'devdb_',
]
],
'env.db-tables' => [
'example' => 'env.db-tables.example',
'replacements' => [
'/DB_PREFIX=PREFIX/' => 'DB_PREFIX=wkvs_',
]
],
'env.pw-encryption-key' => [
'example' => 'env.pw-encryption-key.example',
'replacements' => [
'/YOUR_CUSTOM_PASSWORD_ENCRYPTION_KEY/' => base64_encode(random_bytes(64)),
]
],
'env.wk-id' => [
'example' => 'env.wk-id.example',
'replacements' => []
],
'env.db-wkvs-user' => [
'example' => 'env.db-wkvs-user.example',
'replacements' => [
'/DB_HOST=localhost/' => 'DB_HOST=mariadb',
'/DB_HOST=127\.0\.0\.1/' => 'DB_HOST=mariadb',
'/DATABASE_NAME/' => 'devdb_',
]
],
'env.db-guest' => [
'example' => 'env.db-guest.example',
'replacements' => [
'/DB_HOST=localhost/' => 'DB_HOST=mariadb',
'/DB_HOST=127\.0\.0\.1/' => 'DB_HOST=mariadb',
'/DATABASE_NAME/' => 'devdb_',
]
],
];
echo "========================================\n";
echo " WKVS Environment Initializer \n";
echo "========================================\n\n";
foreach ($envMappings as $targetName => $config) {
$targetPath = $configDir . '/.' . $targetName;
$examplePath = $configDir . '/.' . $config['example'];
if (!file_exists($targetPath)) {
if (file_exists($examplePath)) {
echo "Creating missing ." . $targetName . " from ." . $config['example'] . "...\n";
$content = file_get_contents($examplePath);
foreach ($config['replacements'] as $pattern => $replacement) {
$content = preg_replace($pattern, $replacement, $content);
}
file_put_contents($targetPath, $content);
} else {
echo "⚠️ Warning: Example file ." . $config['example'] . " not found.\n";
}
} else {
echo "Skipping ." . $targetName . " (file already exists).\n";
}
}
echo "✓ Environment initialization complete.\n\n";