instances added god may help

This commit is contained in:
2026-02-23 12:58:19 +01:00
parent 25370a1a55
commit 99db252f60
290 changed files with 9858 additions and 4914 deletions

View File

@@ -4,11 +4,17 @@ namespace MintyPHP\Service\Settings;
class SettingCacheService
{
private static ?array $settings = null;
private ?array $settings = null;
private ?string $cacheFilePath;
public static function get(string $key): ?string
public function __construct(?string $cacheFilePath = null)
{
$settings = self::all();
$this->cacheFilePath = $cacheFilePath;
}
public function get(string $key): ?string
{
$settings = $this->all();
$value = $settings[$key] ?? null;
if ($value === null) {
return null;
@@ -17,26 +23,26 @@ class SettingCacheService
return $value !== '' ? $value : null;
}
public static function all(): array
public function all(): array
{
if (self::$settings !== null) {
return self::$settings;
if ($this->settings !== null) {
return $this->settings;
}
$file = self::filePath();
$file = $this->filePath();
if (!is_file($file)) {
self::$settings = [];
return self::$settings;
$this->settings = [];
return $this->settings;
}
$loaded = include $file;
self::$settings = is_array($loaded) ? $loaded : [];
return self::$settings;
$this->settings = is_array($loaded) ? $loaded : [];
return $this->settings;
}
public static function update(array $values): bool
public function update(array $values): bool
{
$current = self::all();
$current = $this->all();
foreach ($values as $key => $value) {
$settingKey = trim((string) $key);
if ($settingKey === '') {
@@ -44,12 +50,12 @@ class SettingCacheService
}
$current[$settingKey] = $value === null ? null : (string) $value;
}
return self::write($current);
return $this->write($current);
}
public static function write(array $settings): bool
public function write(array $settings): bool
{
$file = self::filePath();
$file = $this->filePath();
$dir = dirname($file);
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
return false;
@@ -60,7 +66,7 @@ class SettingCacheService
return false;
}
$payload = "<?php\nreturn " . self::exportValue($settings) . ";\n";
$payload = "<?php\nreturn " . $this->exportValue($settings) . ";\n";
$written = @file_put_contents($tmp, $payload, LOCK_EX);
if ($written === false) {
@unlink($tmp);
@@ -72,16 +78,19 @@ class SettingCacheService
return false;
}
self::$settings = $settings;
$this->settings = $settings;
return true;
}
public static function filePath(): string
public function filePath(): string
{
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
return $this->cacheFilePath;
}
return dirname(__DIR__, 3) . '/config/settings.php';
}
private static function exportValue(mixed $value, int $depth = 0): string
private function exportValue(mixed $value, int $depth = 0): string
{
if (!is_array($value)) {
return var_export($value, true);
@@ -99,7 +108,7 @@ class SettingCacheService
$lines[] = $childIndent
. var_export($key, true)
. ' => '
. self::exportValue($item, $depth + 1)
. $this->exportValue($item, $depth + 1)
. ',';
}