diff --git a/config.inc.php b/config.inc.php index 6fceae4..6361981 100644 --- a/config.inc.php +++ b/config.inc.php @@ -12,11 +12,13 @@ require_once(__DIR__ . '/msgpack.inc.php'); class ConfigGlobals { public readonly array $base_dirs; + //NOTE: it's a string since is serialized public readonly ?string $worker_init_fn; public readonly int $workers_num; public readonly string $base_class; public readonly string $build_dir; - public readonly mixed $files_overrider; + //NOTE: it's a string since is serialized + public readonly ?string $files_overrider; function __construct( array $base_dirs, @@ -24,7 +26,7 @@ class ConfigGlobals ?string $worker_init_fn = null, int $workers_num = 1, string $base_class = '\ConfBase', - callable $files_overrider = null + ?string $files_overrider = null ) { $this->base_dirs = array_map(fn($path) => normalize_path($path), $base_dirs); @@ -65,10 +67,9 @@ class ConfigUpdateRequest { public ConfigUpdateMode $mode; public ?ConfigDirFiles $files; - public ?string $result_file; public ?\taskman\TaskmanFileChanges $file_changes; public bool $verbose = false; - public bool $return_entries = false; + public bool $return_affected = false; private function __construct() {} @@ -83,7 +84,7 @@ class ConfigUpdateRequest return $req; } - static function detectChanged(string $result_file, \taskman\artefact\TaskmanDirFiles|ConfigDirFiles|null $files = null) : ConfigUpdateRequest + static function detectChanged(\taskman\artefact\TaskmanDirFiles|ConfigDirFiles|null $files = null) : ConfigUpdateRequest { if($files instanceof \taskman\artefact\TaskmanDirFiles) $files = ConfigDirFiles::makeFromArtefactFiles($files); @@ -91,7 +92,6 @@ class ConfigUpdateRequest $req = new ConfigUpdateRequest(); $req->mode = ConfigUpdateMode::DetectChanged; $req->files = $files; - $req->result_file = $result_file; return $req; } @@ -110,7 +110,7 @@ class ConfigUpdateRequest class ConfigUpdateResult { - public ConfigUpdateMode $mode; + public ConfigUpdateRequest $request; public ConfigDirFiles $affected_files; public array $affected_entries = array(); @@ -125,8 +125,8 @@ class ConfigUpdateResult function isPatchPossible() : bool { - return ($this->mode == ConfigUpdateMode::Selected || - $this->mode === ConfigUpdateMode::DetectChanged) && + return ($this->request->mode == ConfigUpdateMode::Selected || + $this->request->mode === ConfigUpdateMode::DetectChanged) && count(array_filter($this->added_files, fn($f) => config_is_file($f))) == 0 && count(array_filter($this->removed_files, fn($f) => config_is_file($f))) == 0; } @@ -151,14 +151,8 @@ class ConfigManager return $this->globals; } - function getCache(ConfigGetMode $mode = ConfigGetMode::CacheOnly) : ConfigCache + function getCache() : ConfigCache { - if($mode != ConfigGetMode::CacheOnly) - { - foreach($this->getIterator($mode) as $ce) - {} - } - return $this->cache; } @@ -181,40 +175,32 @@ class ConfigManager return $this->file_map; } - function getByPath(string $file, ConfigGetMode $mode = ConfigGetMode::Auto, bool $normalize_path = true) : ConfigCacheEntry + function fetchAll(bool $force = false) : \Generator { - if($normalize_path) - $file = normalize_path($file); - - if($mode != ConfigGetMode::CacheOnly) - { - $cache_file = config_get_cache_path($this->globals, $file); - - if($mode == ConfigGetMode::ForceUpdate || need_to_regen($cache_file, [$file])) - { - $base_dir = config_map_base_dir($this->globals->base_dirs, $file); - $parser_type = null; - $ce = _config_update_cache_entry($this->globals, $base_dir, $file, $cache_file, $parser_type); - $this->cache->update($ce); - } - } - - return $this->cache->getOrLoadByPath($file); + $result = $this->updateAll($force); + return $this->iterateCache($result->request->files); } - function getIterator(ConfigGetMode $mode = ConfigGetMode::Auto, mixed $files = null, bool $normalize_path = false) : \Generator + function fetchByPath(string $path) : ConfigCacheEntry { - if($files === null) - { - $files = $this->scanFiles(); - $normalize_path = false; - } + $path = realpath($path); + $dir_files = ConfigDirFiles::makeFor($this); + $dir_files->addFile($path); + $request = ConfigUpdateRequest::selected($dir_files); + $request->return_affected = true; + $result = $this->update($request); + return $result->affected_entries[0]; + } - $cache_entries = []; + function iterateCache(iterable $files) : \Generator + { foreach($files as $file) { - $ce = $this->getByPath($file, $mode, normalize_path: $normalize_path); - yield $ce; + if(config_is_file($file)) + { + $ce = $this->cache->getOrLoadByPath($file); + yield $ce; + } } } @@ -237,7 +223,7 @@ class ConfigManager config_log("Affected files: {$affected_files->count()}"); $update_result = new ConfigUpdateResult(); - $update_result->mode = $req->mode; + $update_result->request = $req; $update_result->affected_files = $affected_files; $update_result->added_files = $added_files; $update_result->removed_files = $removed_files; @@ -254,7 +240,7 @@ class ConfigManager $this->_updateFileMap($req, $affected_files, $added_files, $removed_files); - if($req->return_entries) + if($req->return_affected) { foreach($affected_files as $file) { @@ -263,9 +249,26 @@ class ConfigManager } } + touch($this->_getLastUpdateFile()); + return $update_result; } + function updateAll(bool $force = false) : ConfigUpdateResult + { + $files = $this->scanFiles(extension: '.js'); + $result = $this->update($force ? + ConfigUpdateRequest::force($files) : + ConfigUpdateRequest::detectChanged($files) + ); + return $result; + } + + function _getLastUpdateFile() : string + { + return $this->globals->build_dir . '/update.last'; + } + private function _updateCache(ConfigUpdateResult $result, ConfigCacheUpdateParams $params) { if($params->affected_files->isEmpty()) @@ -357,11 +360,13 @@ class ConfigManager { $affected_files = ConfigDirFiles::makeFor($this); + $last_run_file = $this->_getLastUpdateFile(); + foreach($req->files->getMap() as $base_dir => $files) { foreach($files as $file) { - if(need_to_regen($req->result_file, [$file])) + if(need_to_regen($last_run_file, [$file])) { $affected_files->add($base_dir, $file); diff --git a/pack.inc.php b/pack.inc.php index fb924a7..0344a25 100644 --- a/pack.inc.php +++ b/pack.inc.php @@ -42,10 +42,12 @@ function config_pack_bundle(ConfigPackParams $params) : string $packed_data = null; + $cache_entries = iterator_to_array($params->cache_entries); + if($params->binary_format == 1) { $packed_data = _config_pack_bundle_fmt1( - $params->cache_entries, + $cache_entries, $params->use_lz4, $params->use_config_refs, $params->version @@ -54,7 +56,7 @@ function config_pack_bundle(ConfigPackParams $params) : string else if($params->binary_format == 2) { $packed_data = _config_pack_bundle_fmt2( - $params->cache_entries, + $cache_entries, $params->use_lz4, $params->use_config_refs, $params->version, @@ -63,7 +65,7 @@ function config_pack_bundle(ConfigPackParams $params) : string else if($params->binary_format == 3) { $packed_data = _config_pack_bundle_fmt3( - $params->cache_entries, + $cache_entries, $params->use_lz4, $params->use_config_refs, $params->version, @@ -75,7 +77,7 @@ function config_pack_bundle(ConfigPackParams $params) : string throw new Exception("Unknown binary format: {$params->binary_format}"); if($params->debug) - config_log("Packed entries: " . sizeof($params->cache_entries) . ", total: " . + config_log("Packed entries: " . count($cache_entries) . ", total: " . kb($packed_data) . ", format: {$params->binary_format}, lz4: " . var_export($params->use_lz4, true) . ", refs: " . var_export($params->use_config_refs, true) . ", CRC: " . crc32($packed_data) . @@ -115,7 +117,7 @@ function config_patch_bundle(ConfigPackParams $params, string $packed_data) : st //NOTE: strids are stored as CRCs, potential collision may happen (error will be raised during build) function _config_pack_bundle_fmt1( - iterable $cache_entries, + array $cache_entries, bool $use_lz4, bool $use_config_refs, int $version) : string @@ -173,7 +175,7 @@ function _config_pack_bundle_fmt1( //NOTE: strids are stored as lookup strings, and actually an array of lookup string indices // (each path item separated by '/' is stored as an array item) function _config_pack_bundle_fmt2( - iterable $cache_entries, + array $cache_entries, bool $use_lz4, bool $use_config_refs, int $version, @@ -287,7 +289,7 @@ function _config_pack_bundle_fmt3( $payloads_offset += $payload_size; - if($payloads_offset >= $chunk_size || $idx == ($count_entries - 1)) + if($payloads_offset >= $chunk_size || $idx == ($count_entries - 1)) { if($payloads_offset > $max_chunk_size) $max_chunk_size = $payloads_offset;