Refactor lambda functions for consistent spacing and improve error handling in version retrieval

This commit is contained in:
arabcoders
2025-05-13 19:41:35 +03:00
parent b2cac31804
commit d38b3c71f2
2 changed files with 28 additions and 16 deletions

View File

@@ -64,7 +64,7 @@ final class Initializer
} }
// -- This is the official place where users are supposed to store .env file. // -- 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')) { if (file_exists($dataPath . '/config/.env')) {
loadEnvFile(file: $dataPath . '/config/.env', usePutEnv: true, override: true); loadEnvFile(file: $dataPath . '/config/.env', usePutEnv: true, override: true);
} }
@@ -107,7 +107,7 @@ final class Initializer
$path = Config::get('path') . '/config/config.yaml'; $path = Config::get('path') . '/config/config.yaml';
if (file_exists($path)) { 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'; $path = Config::get('path') . '/config/servers.yaml';
@@ -160,10 +160,10 @@ final class Initializer
$logger->error(message: "{class}: {error} at '{file}:{line}'. '{URI}'", context: [ $logger->error(message: "{class}: {error} at '{file}:{line}'. '{URI}'", context: [
'class' => 'PHP.Engine', 'class' => 'PHP.Engine',
'error' => $error['message'], 'error' => $error['message'] ?? 'Unknown error',
'file' => $error['file'], 'file' => $error['file'] ?? 'Unknown file',
'line' => $error['line'], 'line' => $error['line'] ?? 'Unknown line',
'URI' => before($_SERVER['REQUEST_URI'], '?'), 'URI' => before($_SERVER['REQUEST_URI'] ?? '', '?'),
]); ]);
}); });
@@ -509,6 +509,7 @@ final class Initializer
->pushHandler($wrap->withHandler(new StreamHandler($logfile, Level::Info, true))); ->pushHandler($wrap->withHandler(new StreamHandler($logfile, Level::Info, true)));
if (true === $inContainer) { if (true === $inContainer) {
assert($this->accessLog instanceof Logger);
$this->accessLog->pushHandler($wrap->withHandler(new StreamHandler('php://stderr', Level::Info, true))); $this->accessLog->pushHandler($wrap->withHandler(new StreamHandler('php://stderr', Level::Info, true)));
} }
} }

View File

@@ -703,7 +703,7 @@ if (!function_exists('makeBackend')) {
backendUrl: new Uri(ag($backend, 'url')), backendUrl: new Uri(ag($backend, 'url')),
cache: $cache, cache: $cache,
userContext: $userContext ?? Container::get(UserContext::class), 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), backendId: ag($backend, 'uuid', null),
backendToken: ag($backend, 'token', null), backendToken: ag($backend, 'token', null),
backendUser: ag($backend, 'user', null), backendUser: ag($backend, 'user', null),
@@ -823,16 +823,22 @@ if (!function_exists('getAppVersion')) {
*/ */
function getAppVersion(): string function getAppVersion(): string
{ {
static $_version;
if (null !== $_version) {
return $_version;
}
$version = Config::get('version', 'dev-master'); $version = Config::get('version', 'dev-master');
if ('$(version_via_ci)' === $version) { if ('$(version_via_ci)' === $version) {
try {
$gitDir = ROOT_PATH . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR; $gitDir = ROOT_PATH . DIRECTORY_SEPARATOR . '.git' . DIRECTORY_SEPARATOR;
if (false === is_dir($gitDir)) { if (false === is_dir($gitDir)) {
return 'dev-master'; throw new RuntimeException('Git directory not found.');
} }
try {
$cmdBranch = 'git -C {cwd} rev-parse --abbrev-ref HEAD'; $cmdBranch = 'git -C {cwd} rev-parse --abbrev-ref HEAD';
$procBranch = Process::fromShellCommandline(r($cmdBranch, ['cwd' => escapeshellarg($gitDir)])); $procBranch = Process::fromShellCommandline(r($cmdBranch, ['cwd' => escapeshellarg($gitDir)]));
$procBranch->run(); $procBranch->run();
@@ -848,16 +854,21 @@ if (!function_exists('getAppVersion')) {
$procDate->run(); $procDate->run();
$commitDate = $procDate->isSuccessful() ? trim($procDate->getOutput()) : date('Ymd'); $commitDate = $procDate->isSuccessful() ? trim($procDate->getOutput()) : date('Ymd');
return r('{branch}-{date}-{commit}', [ $_version = r('{branch}-{date}-{commit}', [
'branch' => $branch, 'branch' => $branch,
'date' => $commitDate, 'date' => $commitDate,
'commit' => $commit, 'commit' => $commit,
]); ]);
return $_version;
} catch (Throwable) { } catch (Throwable) {
$_version = 'dev-master';
return 'dev-master'; return 'dev-master';
} }
} }
$_version = $version;
return $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 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 function parseConfigValue(mixed $value, Closure|null $callback = null): mixed
{ {
if (is_string($value) && preg_match('#%{(.+?)}#s', $value)) { 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; return null !== $callback && null !== $val ? $callback($val) : $val;
} }