diff --git a/src/Libs/Initializer.php b/src/Libs/Initializer.php index 2191d8a1..47a6d382 100644 --- a/src/Libs/Initializer.php +++ b/src/Libs/Initializer.php @@ -64,7 +64,7 @@ final class Initializer } // -- This is the official place where users are supposed to store .env file. - $dataPath = env('WS_DATA_PATH', fn() => inContainer() ? '/config' : __DIR__ . '/../../var'); + $dataPath = env('WS_DATA_PATH', fn () => inContainer() ? '/config' : __DIR__ . '/../../var'); if (file_exists($dataPath . '/config/.env')) { loadEnvFile(file: $dataPath . '/config/.env', usePutEnv: true, override: true); } @@ -107,7 +107,7 @@ final class Initializer $path = Config::get('path') . '/config/config.yaml'; if (file_exists($path)) { - Config::init(fn() => array_replace_recursive(Config::getAll(), Yaml::parseFile($path))); + Config::init(fn () => array_replace_recursive(Config::getAll(), Yaml::parseFile($path))); } $path = Config::get('path') . '/config/servers.yaml'; @@ -160,10 +160,10 @@ final class Initializer $logger->error(message: "{class}: {error} at '{file}:{line}'. '{URI}'", context: [ 'class' => 'PHP.Engine', - 'error' => $error['message'], - 'file' => $error['file'], - 'line' => $error['line'], - 'URI' => before($_SERVER['REQUEST_URI'], '?'), + 'error' => $error['message'] ?? 'Unknown error', + 'file' => $error['file'] ?? 'Unknown file', + 'line' => $error['line'] ?? 'Unknown line', + 'URI' => before($_SERVER['REQUEST_URI'] ?? '', '?'), ]); }); @@ -509,6 +509,7 @@ final class Initializer ->pushHandler($wrap->withHandler(new StreamHandler($logfile, Level::Info, true))); if (true === $inContainer) { + assert($this->accessLog instanceof Logger); $this->accessLog->pushHandler($wrap->withHandler(new StreamHandler('php://stderr', Level::Info, true))); } } diff --git a/src/Libs/helpers.php b/src/Libs/helpers.php index 59cecb52..0ed0d399 100644 --- a/src/Libs/helpers.php +++ b/src/Libs/helpers.php @@ -703,7 +703,7 @@ if (!function_exists('makeBackend')) { backendUrl: new Uri(ag($backend, 'url')), cache: $cache, userContext: $userContext ?? Container::get(UserContext::class), - logger: ag($options, iLogger::class, fn() => Container::get(iLogger::class)), + logger: ag($options, iLogger::class, fn () => Container::get(iLogger::class)), backendId: ag($backend, 'uuid', null), backendToken: ag($backend, 'token', null), backendUser: ag($backend, 'user', null), @@ -823,16 +823,22 @@ if (!function_exists('getAppVersion')) { */ function getAppVersion(): string { + static $_version; + + if (null !== $_version) { + return $_version; + } + $version = Config::get('version', 'dev-master'); if ('$(version_via_ci)' === $version) { - $gitDir = ROOT_PATH . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR; - - if (false === is_dir($gitDir)) { - return 'dev-master'; - } - try { + $gitDir = ROOT_PATH . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR; + + if (false === is_dir($gitDir)) { + throw new RuntimeException('Git directory not found.'); + } + $cmdBranch = 'git -C {cwd} rev-parse --abbrev-ref HEAD'; $procBranch = Process::fromShellCommandline(r($cmdBranch, ['cwd' => escapeshellarg($gitDir)])); $procBranch->run(); @@ -848,16 +854,21 @@ if (!function_exists('getAppVersion')) { $procDate->run(); $commitDate = $procDate->isSuccessful() ? trim($procDate->getOutput()) : date('Ymd'); - return r('{branch}-{date}-{commit}', [ + $_version = r('{branch}-{date}-{commit}', [ 'branch' => $branch, 'date' => $commitDate, 'commit' => $commit, ]); + + return $_version; } catch (Throwable) { + $_version = 'dev-master'; return 'dev-master'; } } + $_version = $version; + return $version; } } @@ -910,7 +921,7 @@ if (!function_exists('array_keys_diff')) { */ function array_keys_diff(array $base, array $list, bool $has = true): array { - return array_filter($base, fn($key) => $has === in_array($key, $list), ARRAY_FILTER_USE_KEY); + return array_filter($base, fn ($key) => $has === in_array($key, $list), ARRAY_FILTER_USE_KEY); } } @@ -1320,7 +1331,7 @@ if (!function_exists('parseConfigValue')) { function parseConfigValue(mixed $value, Closure|null $callback = null): mixed { if (is_string($value) && preg_match('#%{(.+?)}#s', $value)) { - $val = preg_replace_callback('#%{(.+?)}#s', fn($match) => Config::get($match[1], $match[1]), $value); + $val = preg_replace_callback('#%{(.+?)}#s', fn ($match) => Config::get($match[1], $match[1]), $value); return null !== $callback && null !== $val ? $callback($val) : $val; }