chore: sync repository snapshot

This commit is contained in:
GuavaPopper
2026-06-11 14:53:35 +07:00
parent 529d8bcc70
commit 3bed7457ec
88 changed files with 13102 additions and 152 deletions
+21
View File
@@ -0,0 +1,21 @@
#!/bin/sh
set -e
if [ ! -f .env ]; then
cp .env.example .env
fi
php docker/sync-env.php
php artisan optimize:clear
if [ -n "${APP_KEY:-}" ]; then
echo "APP_KEY provided by environment, skipping key generation."
elif ! grep -q '^APP_KEY=base64:' .env; then
php artisan key:generate --force
fi
php docker/wait-for-database.php
php artisan migrate --force
php artisan db:seed --force
exec "$@"
+101
View File
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
function docker_sync_env_keys(): array
{
return [
'APP_NAME',
'APP_ENV',
'APP_KEY',
'APP_DEBUG',
'APP_URL',
'DB_CONNECTION',
'DB_HOST',
'DB_PORT',
'DB_DATABASE',
'DB_USERNAME',
'DB_PASSWORD',
'SESSION_DRIVER',
'CACHE_STORE',
'QUEUE_CONNECTION',
];
}
function docker_sync_env_format_value(string $value): string
{
if ($value === '') {
return '';
}
if (preg_match('/\A[A-Za-z0-9_\.\/:@+\-]+\z/', $value) === 1) {
return $value;
}
$escaped = str_replace(
["\\", "\"", "\n", "\r"],
["\\\\", "\\\"", "\\n", ''],
$value
);
return "\"{$escaped}\"";
}
function docker_sync_env_file(string $path, array $keys, ?array $values = null): void
{
$lines = file_exists($path) ? file($path, FILE_IGNORE_NEW_LINES) : [];
if ($lines === false) {
throw new RuntimeException("Unable to read {$path}.");
}
$updates = [];
foreach ($keys as $key) {
$value = $values === null ? getenv($key) : ($values[$key] ?? false);
if ($value === false) {
continue;
}
$updates[$key] = docker_sync_env_format_value((string) $value);
}
if ($updates === []) {
return;
}
$seen = [];
foreach ($lines as $index => $line) {
foreach ($updates as $key => $value) {
if (preg_match('/^\s*'.preg_quote($key, '/').'=/i', $line) === 1) {
$lines[$index] = "{$key}={$value}";
$seen[$key] = true;
break;
}
}
}
foreach ($updates as $key => $value) {
if (! isset($seen[$key])) {
$lines[] = "{$key}={$value}";
}
}
$content = implode(PHP_EOL, $lines).PHP_EOL;
if (file_put_contents($path, $content) === false) {
throw new RuntimeException("Unable to write {$path}.");
}
}
$script = isset($_SERVER['SCRIPT_FILENAME']) ? realpath($_SERVER['SCRIPT_FILENAME']) : false;
if ($script !== false && $script === realpath(__FILE__)) {
$path = $argv[1] ?? dirname(__DIR__).'/.env';
docker_sync_env_file($path, docker_sync_env_keys());
fwrite(STDOUT, "Docker environment synchronized into .env.\n");
}
+48
View File
@@ -0,0 +1,48 @@
<?php
function env_value(string $key, string $default = ''): string
{
$value = getenv($key);
if ($value !== false && $value !== '') {
return $value;
}
static $fileVars = null;
if ($fileVars === null) {
$fileVars = is_file(__DIR__.'/../.env')
? parse_ini_file(__DIR__.'/../.env', false, INI_SCANNER_RAW)
: [];
}
return isset($fileVars[$key]) && $fileVars[$key] !== ''
? (string) $fileVars[$key]
: $default;
}
$host = env_value('DB_HOST', '127.0.0.1');
$port = env_value('DB_PORT', '3306');
$database = env_value('DB_DATABASE', 'spbu_baru');
$username = env_value('DB_USERNAME', 'root');
$password = env_value('DB_PASSWORD', '');
$dsn = "mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4";
echo "Waiting for database at {$host}:{$port}...\n";
for ($attempt = 1; $attempt <= 30; $attempt++) {
try {
new PDO($dsn, $username, $password, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_TIMEOUT => 3,
]);
echo "Database connection OK.\n";
exit(0);
} catch (PDOException $exception) {
if ($attempt === 30) {
fwrite(STDERR, "Database unreachable after 30 attempts: {$exception->getMessage()}\n");
exit(1);
}
sleep(2);
}
}