This commit is contained in:
deploy-bot committed 2026-12-06 12:46:53 +00:00
commit 1c8a903f24
306 files changed
+22212

No files matched your search

+137
View File
@@ -0,0 +1,137 @@
<?php
namespace IriksIT\Manager\Support\Settings;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use IriksIT\Manager\Support\Names;
use Throwable;
/**
* What was set from the settings screen. The table is the truth (with a history of who and when); every change also writes
* one compiled file (`bootstrap/cache/manager-settings.php`, secrets in it stay encrypted) that is laid over the config
* while the app starts, so a request reads one file from opcache and never asks the database. That works with `config:cache`
* too, because it is applied after the config was loaded. Without the file (it could not be written) the table is read once.
*/
class Store
{
public function __construct(private readonly Definitions $definitions) {}
public static function file(): string
{
return app()->bootstrapPath('cache/manager-settings.php');
}
/** Lays the saved settings over the config. Called while the provider registers; it must never fail the app. */
public static function overlay(): void
{
try {
$values = is_file(self::file()) ? (array) (include self::file()) : self::fromTable();
foreach ($values as $key => $value) {
if (is_array($value) && array_key_exists('__enc', $value)) {
$value = json_decode(Crypt::decryptString((string) $value['__enc']), true);
}
config([$key => $value]);
}
self::applyEffects(array_keys($values));
} catch (Throwable) {
// A missing table (before the first migrate), a changed APP_KEY, a broken file: the config keeps what it had.
}
}
/**
* What a changed config key has to do besides sitting in the config: the time zone and the language are also set on the running app, and
* a mail setting makes the mailer be built again with the new values.
*
* @param list<string> $keys
*/
public static function applyEffects(array $keys): void
{
if (in_array('app.timezone', $keys, true) && is_string(config('app.timezone'))) {
@date_default_timezone_set(config('app.timezone'));
}
if (in_array('app.locale', $keys, true) && is_string(config('app.locale'))) {
app()->setLocale(config('app.locale'));
}
if (array_filter($keys, fn ($key) => str_starts_with($key, 'mail.')) !== [] && app()->resolved('mail.manager')) {
app('mail.manager')->purge();
}
}
/** @return array<string, mixed> */
private static function fromTable(): array
{
$values = [];
foreach (DB::table(Names::table('settings'))->get() as $row) {
$values[$row->key] = $row->is_secret ? ['__enc' => $row->value] : json_decode((string) $row->value, true);
}
return $values;
}
/**
* Saves values (null: back to what the config says) and rewrites the compiled file. Returns whether that file could be written.
*
* @param array<string, mixed> $values by config path, already checked against the definitions
*/
public function put(array $values, mixed $userId = null): bool
{
$table = Names::table('settings');
DB::transaction(function () use ($values, $table, $userId) {
foreach ($values as $key => $value) {
$definition = $this->definitions->find($key);
if ($value === null || $definition === null) {
DB::table($table)->where('key', $key)->delete();
continue;
}
$secret = $definition['type'] === 'secret';
$stored = $secret ? Crypt::encryptString(json_encode($value)) : json_encode($value);
DB::table($table)->updateOrInsert(['key' => $key], ['value' => $stored, 'is_secret' => $secret, 'updated_by' => $userId, 'updated_at' => now(), 'created_at' => now()]);
}
});
return $this->compile();
}
/** Writes the compiled file from the table: to a temporary file first, then moved over the old one in one step. */
public function compile(): bool
{
$known = $this->definitions->all();
$values = array_filter(self::fromTable(), fn ($value, $key) => isset($known[$key]), ARRAY_FILTER_USE_BOTH);
$file = self::file();
$temporary = $file.'.'.bin2hex(random_bytes(4)).'.tmp';
try {
if (@file_put_contents($temporary, '<?php return '.var_export($values, true).';'.PHP_EOL, LOCK_EX) === false || ! @rename($temporary, $file)) {
@unlink($temporary);
return false;
}
} catch (Throwable) {
@unlink($temporary);
return false;
}
function_exists('opcache_invalidate') && @opcache_invalidate($file, true);
return true;
}
/** Whether a setting has been saved here (and so wins over .env). */
public function has(string $key): bool
{
return DB::table(Names::table('settings'))->where('key', $key)->exists();
}
}