refactor: rename lib/ to core/ for clearer core-module separation
Rename the top-level lib/ directory to core/ so the project root immediately communicates which code is core platform and which lives in modules/. PHP namespaces (MintyPHP\*) are unchanged — only the Composer PSR-4 path mapping moves from lib/ to core/. Module-internal lib/ directories (modules/*/lib/) are untouched. Updated across the full stack: - composer.json PSR-4 mapping - bootstrap entry points (web/index.php, bin/*, tests/bootstrap.php) - tooling configs (phpstan.neon, phpunit.xml, php-cs-fixer) - 26 architecture contract tests - enforcement-policy, guard-catalog, quality-gates - all documentation (CLAUDE.md, README, 25 docs/, .agents/skills/) - bin/qa-extended.sh search paths - .claude/settings.local.json permission paths Workflow: .agents/runs/CORE-LIB-RENAME-001/ (Analyst → Planner → Executor → Code Review (4 findings fixed) → Security Review → Acceptance Test → Finalizer) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
159
core/Service/Settings/SettingCacheService.php
Normal file
159
core/Service/Settings/SettingCacheService.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
namespace MintyPHP\Service\Settings;
|
||||
|
||||
class SettingCacheService
|
||||
{
|
||||
private const HOT_PATH_KEYS = [
|
||||
SettingKeys::APP_TITLE_KEY,
|
||||
SettingKeys::APP_LOCALE_KEY,
|
||||
SettingKeys::APP_THEME_KEY,
|
||||
SettingKeys::APP_THEME_USER_KEY,
|
||||
SettingKeys::APP_REGISTRATION_KEY,
|
||||
SettingKeys::APP_PRIMARY_COLOR_KEY,
|
||||
];
|
||||
|
||||
private ?array $settings = null;
|
||||
private ?string $cacheFilePath;
|
||||
private ?SettingsMetadataGateway $settingsMetadataGateway;
|
||||
|
||||
public function __construct(?string $cacheFilePath = null, ?SettingsMetadataGateway $settingsMetadataGateway = null)
|
||||
{
|
||||
$this->cacheFilePath = $cacheFilePath;
|
||||
$this->settingsMetadataGateway = $settingsMetadataGateway;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function all(): array
|
||||
{
|
||||
if ($this->settings !== null) {
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
$file = $this->filePath();
|
||||
if (!is_file($file)) {
|
||||
$this->settings = $this->hydrateColdStartCache();
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
$loaded = include $file;
|
||||
$this->settings = is_array($loaded) ? $loaded : [];
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
public function update(array $values): bool
|
||||
{
|
||||
$current = $this->all();
|
||||
foreach ($values as $key => $value) {
|
||||
$settingKey = trim((string) $key);
|
||||
if ($settingKey === '') {
|
||||
continue;
|
||||
}
|
||||
$current[$settingKey] = $value === null ? null : (string) $value;
|
||||
}
|
||||
return $this->write($current);
|
||||
}
|
||||
|
||||
public function write(array $settings): bool
|
||||
{
|
||||
$file = $this->filePath();
|
||||
$dir = dirname($file);
|
||||
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// PHP include returns an array — loaded by OPcache on subsequent requests, much faster than DB.
|
||||
$payload = "<?php\n\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;
|
||||
}
|
||||
|
||||
$this->settings = $settings;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function filePath(): string
|
||||
{
|
||||
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
|
||||
return $this->cacheFilePath;
|
||||
}
|
||||
|
||||
$storagePath = defined('APP_STORAGE_PATH')
|
||||
? (string) APP_STORAGE_PATH
|
||||
: dirname(__DIR__, 3) . '/storage';
|
||||
|
||||
return rtrim($storagePath, '/') . '/runtime/settings.php';
|
||||
}
|
||||
|
||||
private function hydrateColdStartCache(): array
|
||||
{
|
||||
if (!$this->settingsMetadataGateway instanceof SettingsMetadataGateway) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$hydrated = [];
|
||||
foreach (self::HOT_PATH_KEYS as $key) {
|
||||
$value = $this->settingsMetadataGateway->getValue($key);
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
$hydrated[$key] = (string) $value;
|
||||
}
|
||||
|
||||
if ($this->write($hydrated)) {
|
||||
return $this->settings ?? $hydrated;
|
||||
}
|
||||
|
||||
$this->settings = $hydrated;
|
||||
return $hydrated;
|
||||
}
|
||||
|
||||
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)
|
||||
. ' => '
|
||||
. $this->exportValue($item, $depth + 1)
|
||||
. ',';
|
||||
}
|
||||
|
||||
return "[\n" . implode("\n", $lines) . "\n" . $indent . ']';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user