From ed2e8a4cb3e91787cdd7526a51b0c0dda4088f72 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Thu, 17 Apr 2025 19:15:42 +0300 Subject: [PATCH] Better ConfigGlobals --- cache.inc.php | 7 ++----- config.inc.php | 29 +++++++++++++++++------------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/cache.inc.php b/cache.inc.php index 8b68317..95bd30c 100644 --- a/cache.inc.php +++ b/cache.inc.php @@ -353,25 +353,22 @@ class ConfigCacheUpdateParams public ConfigGlobals $globals; public ConfigDirFiles $affected_files; public bool $verbose = false; - public ?int $max_workers = null; public bool $check_junk = true; public int $max_errors_num = 15; public bool $return_affected_entries = false; - function __construct(ConfigGlobals $globals, ConfigDirFiles $affected_files, - bool $verbose = false, ?int $max_workers = null) + function __construct(ConfigGlobals $globals, ConfigDirFiles $affected_files, bool $verbose = false) { $this->globals = $globals; $this->affected_files = $affected_files; $this->verbose = $verbose; - $this->max_workers = $max_workers; } function calcMaxWorkers() : int { if($this->affected_files->count() < self::FILES_THRESHOLD_ONE_WORKER) return 1; - return $this->max_workers ?? self::MAX_DEFAULT_WORKERS; + return $this->globals->max_workers ?? self::MAX_DEFAULT_WORKERS; } function splitFilesByChunks(int $max_workers, bool $sort = true) : array diff --git a/config.inc.php b/config.inc.php index a374438..d652a3f 100644 --- a/config.inc.php +++ b/config.inc.php @@ -11,16 +11,25 @@ require_once(__DIR__ . '/msgpack.inc.php'); class ConfigGlobals { - public array $base_dirs = array(); - public ?string $worker_init_fn = null; - public string $base_class = '\ConfBase'; - public string $build_dir; + public readonly array $base_dirs; + public readonly ?string $worker_init_fn; + public readonly int $workers_num; + public readonly string $base_class; + public readonly string $build_dir; - function __construct(array $base_dirs, string $build_dir, ?string $worker_init_fn = null) + function __construct( + array $base_dirs, + string $build_dir, + ?string $worker_init_fn = null, + int $workers_num = 1, + string $base_class = '\ConfBase' + ) { $this->base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs); $this->build_dir = $build_dir; $this->worker_init_fn = $worker_init_fn; + $this->workers_num = $workers_num; + $this->base_class = $base_class; } function initWorker(bool $is_master_proc) @@ -79,15 +88,12 @@ class ConfigUpdateRequest class ConfigManager { private ConfigGlobals $globals; - private int $workers_num; - private ConfigCache $cache; private ?ConfigCacheFileMap $file_map; - function __construct(ConfigGlobals $globals, int $workers_num) + function __construct(ConfigGlobals $globals) { $this->globals = $globals; - $this->workers_num = $workers_num; $this->cache = new ConfigCache($globals); $this->file_map = null; @@ -115,7 +121,7 @@ class ConfigManager $this->file_map = $this->_tryLoadMap(); if($this->file_map === null) { - $this->file_map = self::_makeMap($files ?? $this->scanFiles(extension: '.js')); + $this->file_map = self::_makeMap($this->scanFiles(extension: '.js')); $this->_saveFileMap(); } } @@ -143,8 +149,7 @@ class ConfigManager $update_params = new ConfigCacheUpdateParams( globals: $this->globals, affected_files: $affected_files, - verbose: $verbose, - max_workers: $this->workers_num + verbose: $verbose ); $update_result = self::_updateCache($update_params);