base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs); $this->build_dir = $build_dir; $this->worker_init_fn = $worker_init_fn; } function initWorker(bool $is_master_proc) { $GLOBALS['CONFIG_GLOBALS'] = $this; if($this->worker_init_fn !== null) { $fn = $this->worker_init_fn; $fn($is_master_proc); } } } function config_log($msg) { echo "[CFG] $msg\n"; } function config_get_cache_path(ConfigGlobals $globals, string $file) : string { return config_get_tmp_build_path($globals, $file . '.cacheb'); } function config_get_cache_payload_path(ConfigGlobals $globals, string $file) : string { return config_get_tmp_build_path($globals, $file . '.pdata'); } function config_load(ConfigGlobals $globals, string $conf_path, ?string $conf_dir = null) : object { $conf_dir ??= config_map_base_dir($globals->base_dirs, $conf_path); list($conf_id, $_) = config_ensure_header($conf_dir, $conf_path); if(!$conf_id) throw new Exception("Bad conf id: {$conf_id}"); $pres = config_parse(array($conf_dir), $conf_path); if($pres->error !== 0) throw new Exception("Error({$pres->error}) while loading JSON from {$conf_path}:\n" . $pres->error_descr); return config_load_from_kv_array($globals, $conf_dir, $conf_path, $pres->parsed_arr, $conf_id); } function config_load_from_kv_array( ConfigGlobals $globals, string $conf_dir, string $file, array $arr, ?int $id = null) : object { if(!isset($arr['class']) || !isset($arr['class'][0])) throw new Exception("Class is not set in file '$file'."); $id ??= config_file2id($conf_dir, $file); $cnf = null; try { list($klass, $class_id, $norm_arr) = config_apply_class_normalization($arr); if(!class_exists($klass)) throw new Exception("No such class '$klass'"); $cnf = new $klass; if(!is_a($cnf, $globals->base_class)) throw new Exception("'$klass' is not subclass of '".ltrim($globals->base_class, '\\')."'"); $cnf->import($norm_arr); } catch(Exception $e) { throw new Exception($e->getMessage() . " in file '{$file}'"/* . $e->getTraceAsString()*/); } $cnf->id = $id; $cnf->strid = config_file2strid($conf_dir, $file); return $cnf; } function config_bench_load(ConfigGlobals $globals, string $file) { $base_dir = config_map_base_dir($globals->base_dirs, $file); list($conf_id, $_) = config_ensure_header($base_dir, $file); if(!$conf_id) throw new Exception("Bad conf id: {$conf_id}"); $t = microtime(true); $parse_res = config_parse($globals->base_dirs, $file); if($parse_res->error !== 0) throw new Exception("Error({$parse_res->error}) while loading JSON from {$file}:\n" . $parse_res->error_descr); config_log("Parse: " . (microtime(true) - $t)); $t = microtime(true); $config = config_load_from_kv_array($globals, $base_dir, $file, $parse_res->parsed_arr, $conf_id); config_log("Load: " . (microtime(true) - $t)); }