Files
breadcrumb-the-shire/lib/Service/Settings/SettingCacheService.php

121 lines
3.2 KiB
PHP
Raw Normal View History

<?php
namespace MintyPHP\Service\Settings;
class SettingCacheService
{
2026-02-23 12:58:19 +01:00
private ?array $settings = null;
private ?string $cacheFilePath;
2026-02-23 12:58:19 +01:00
public function __construct(?string $cacheFilePath = null)
{
2026-02-23 12:58:19 +01:00
$this->cacheFilePath = $cacheFilePath;
}
public function get(string $key): ?string
{
$settings = $this->all();
$value = $settings[$key] ?? null;
if ($value === null) {
return null;
}
$value = trim((string) $value);
return $value !== '' ? $value : null;
}
2026-02-23 12:58:19 +01:00
public function all(): array
{
2026-02-23 12:58:19 +01:00
if ($this->settings !== null) {
return $this->settings;
}
2026-02-23 12:58:19 +01:00
$file = $this->filePath();
if (!is_file($file)) {
2026-02-23 12:58:19 +01:00
$this->settings = [];
return $this->settings;
}
$loaded = include $file;
2026-02-23 12:58:19 +01:00
$this->settings = is_array($loaded) ? $loaded : [];
return $this->settings;
}
2026-02-23 12:58:19 +01:00
public function update(array $values): bool
{
2026-02-23 12:58:19 +01:00
$current = $this->all();
foreach ($values as $key => $value) {
$settingKey = trim((string) $key);
if ($settingKey === '') {
continue;
}
$current[$settingKey] = $value === null ? null : (string) $value;
}
2026-02-23 12:58:19 +01:00
return $this->write($current);
}
2026-02-23 12:58:19 +01:00
public function write(array $settings): bool
{
2026-02-23 12:58:19 +01:00
$file = $this->filePath();
$dir = dirname($file);
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
return false;
}
2026-03-06 00:44:52 +01:00
// Write to a temp file first, then rename — rename is atomic on most filesystems,
// so readers never see a half-written file. LOCK_EX prevents concurrent write corruption.
$tmp = tempnam($dir, 'settings_');
if ($tmp === false) {
return false;
}
2026-03-06 00:44:52 +01:00
// PHP include returns an array — loaded by OPcache on subsequent requests, much faster than DB.
2026-02-23 12:58:19 +01:00
$payload = "<?php\nreturn " . $this->exportValue($settings) . ";\n";
$written = @file_put_contents($tmp, $payload, LOCK_EX);
if ($written === false) {
@unlink($tmp);
return false;
}
if (!@rename($tmp, $file)) {
@unlink($tmp);
return false;
}
2026-02-23 12:58:19 +01:00
$this->settings = $settings;
return true;
}
2026-02-23 12:58:19 +01:00
public function filePath(): string
{
2026-02-23 12:58:19 +01:00
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
return $this->cacheFilePath;
}
return dirname(__DIR__, 3) . '/config/settings.php';
}
2026-02-23 12:58:19 +01:00
private function exportValue(mixed $value, int $depth = 0): string
{
if (!is_array($value)) {
return var_export($value, true);
}
if ($value === []) {
return '[]';
}
$indent = str_repeat(' ', $depth);
$childIndent = str_repeat(' ', $depth + 1);
$lines = [];
foreach ($value as $key => $item) {
$lines[] = $childIndent
. var_export($key, true)
. ' => '
2026-02-23 12:58:19 +01:00
. $this->exportValue($item, $depth + 1)
. ',';
}
return "[\n" . implode("\n", $lines) . "\n" . $indent . ']';
}
}