Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Phaseolies/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ class Application extends Container
*
* Initializes the application by:
* - Setting the application instance in the container.
* - Loading environment variables from .env before anything else.
* - Setting up exception handling.
* - Loading configuration.
* - Defining necessary folder paths.
Expand Down
4 changes: 1 addition & 3 deletions src/Phaseolies/Auth/Security/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,7 @@ public function user(): ?Model
if (session()->has($cacheKey)) {
$cache = session($cacheKey);
if ($this->isUserCacheValid($cache)) {
if ($this->isUserCacheValid($cache)) {
return $this->resolvedUser = $cache['user'];
}
return $this->resolvedUser = $cache['user'];
}
}

Expand Down
69 changes: 45 additions & 24 deletions src/Phaseolies/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,27 @@ final class Config
*/
protected static bool $loadedFromCache = false;

/**
* Tracks whether config was modified after loading from cache.
*
* @var bool
*/
protected static bool $configModified = false;

/**
* Stores hashes of configuration files used to detect changes.
*
* @var array<string, string>
*/
protected static array $fileHashes = [];

/**
* Cached list of config file paths
*
* @var array|null
*/
protected static ?array $configFiles = null;

/**
* Initialize the configuration system.
*
Expand All @@ -48,7 +62,17 @@ public static function initialize(): void
}

/**
* Generate a unique cache key based on all configuration files.
* Get all config file paths, cached for the request lifetime.
*
* @return array
*/
protected static function getConfigFiles(): array
{
return self::$configFiles ??= glob(base_path('config/*.php')) ?: [];
}

/**
* Generate a unique cache key based on all configuration files and .env.
*
* @return string
*/
Expand All @@ -57,9 +81,9 @@ protected static function getCacheKey(): string
static $cacheKey = null;

if ($cacheKey === null) {
$files = glob(base_path('config/*.php'));
$hashes = [];
foreach ($files as $file) {

foreach (self::getConfigFiles() as $file) {
$hashes[] = md5_file($file) . '|' . filemtime($file);
}

Expand All @@ -83,7 +107,7 @@ public static function loadAll(): void
{
self::$config = [];

foreach (glob(base_path('config/*.php')) as $file) {
foreach (self::getConfigFiles() as $file) {
$key = basename($file, '.php');
self::$config[$key] = include $file;
}
Expand All @@ -98,13 +122,23 @@ public static function loadAll(): void
*/
public static function loadFromCache(): void
{
if (!file_exists(self::$cacheFile) || !self::isCacheValid()) {
if (!file_exists(self::$cacheFile)) {
self::$loadedFromCache = false;
self::loadAll();
return;
}

$cached = include self::$cacheFile;

if (
!isset($cached['_meta']['cache_key']) ||
$cached['_meta']['cache_key'] !== self::getCacheKey()
) {
self::$loadedFromCache = false;
self::loadAll();
return;
}

self::$config = $cached['data'] ?? [];
self::$fileHashes = $cached['_meta']['file_hashes'] ?? [];
self::$loadedFromCache = true;
Expand All @@ -121,7 +155,7 @@ public static function cacheConfig(): void
return;
}

if (self::$loadedFromCache && !self::configWasModified()) {
if (self::$loadedFromCache && !self::$configModified) {
return;
}

Expand All @@ -130,10 +164,8 @@ public static function cacheConfig(): void
throw new RuntimeException("Failed to create cache directory: {$cacheDir}");
}

$files = glob(base_path('config/*.php'));

$hashes = [];
foreach ($files as $file) {
foreach (self::getConfigFiles() as $file) {
$hashes[basename($file)] = md5_file($file) . '|' . filemtime($file);
}

Expand All @@ -151,23 +183,10 @@ public static function cacheConfig(): void
rename($tempFile, self::$cacheFile);

self::$loadedFromCache = true;
self::$configModified = false;
self::$fileHashes = $hashes;
}

/**
* Check if the configuration data has been modified compared to cache.
*
* @return bool
*/
protected static function configWasModified(): bool
{
if (!file_exists(self::$cacheFile)) return true;

$cached = include self::$cacheFile;

return $cached['data'] !== self::$config;
}

/**
* Get a configuration value using dot notation.
*
Expand Down Expand Up @@ -225,6 +244,7 @@ public static function set(string $key, mixed $value): void
$current = $value;
}

self::$configModified = true;
self::cacheConfig();
}

Expand Down Expand Up @@ -261,8 +281,9 @@ public static function clearCache(): void
if (file_exists(self::$cacheFile)) @unlink(self::$cacheFile);

self::$config = [];

self::$fileHashes = [];
self::$configFiles = null;
self::$configModified = false;
}

/**
Expand Down
Loading