diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-24 00:23:30 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-24 00:23:30 +0900 |
| commit | af0df92e1ecc82823a510646b7545278caeac4b8 (patch) | |
| tree | 4b735eed50dd369895600c2f7cfd0039d274b2af /crates/mozart-registry | |
| parent | 2622fa3089d1df249276083d157e43b080a59100 (diff) | |
| download | php-mozart-af0df92e1ecc82823a510646b7545278caeac4b8.tar.gz php-mozart-af0df92e1ecc82823a510646b7545278caeac4b8.tar.zst php-mozart-af0df92e1ecc82823a510646b7545278caeac4b8.zip | |
feat(cache): enable repo cache for all Packagist API calls
Remove the Option wrapper from repo_cache in ResolveRequest,
LockFileGenerationRequest, and fetch_package_versions. All commands
now initialize a Cache via build_cache_config(cli.no_cache), ensuring
Packagist metadata is cached to disk (respecting --no-cache flag).
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'crates/mozart-registry')
| -rw-r--r-- | crates/mozart-registry/src/lockfile.rs | 13 | ||||
| -rw-r--r-- | crates/mozart-registry/src/packagist.rs | 17 | ||||
| -rw-r--r-- | crates/mozart-registry/src/resolver.rs | 27 |
3 files changed, 26 insertions, 31 deletions
diff --git a/crates/mozart-registry/src/lockfile.rs b/crates/mozart-registry/src/lockfile.rs index 8f27fbf..eea9b29 100644 --- a/crates/mozart-registry/src/lockfile.rs +++ b/crates/mozart-registry/src/lockfile.rs @@ -242,8 +242,8 @@ pub struct LockFileGenerationRequest { pub composer_json: RawPackageData, /// Whether require-dev was included in resolution. pub include_dev: bool, - /// Optional repo cache for Packagist API calls made during generation. - pub repo_cache: Option<Cache>, + /// Repo cache for Packagist API calls made during generation. + pub repo_cache: Cache, } /// Convert a `PackagistSource` to a `LockedSource`. @@ -396,8 +396,7 @@ pub async fn generate_lock_file(request: &LockFileGenerationRequest) -> anyhow:: // 1. Fetch full metadata for all resolved packages let mut package_metadata: HashMap<String, PackagistVersion> = HashMap::new(); for pkg in &request.resolved_packages { - let versions = - packagist::fetch_package_versions(&pkg.name, request.repo_cache.as_ref()).await?; + let versions = packagist::fetch_package_versions(&pkg.name, &request.repo_cache).await?; // Find the exact version matching pkg.version_normalized let matching = versions .into_iter() @@ -925,7 +924,7 @@ mod tests { composer_json_content: composer_json_content.clone(), composer_json, include_dev: true, - repo_cache: None, + repo_cache: Cache::new(std::env::temp_dir().join("mozart-test-cache"), false), }; let lock = generate_lock_file(&request).await.unwrap(); @@ -1030,7 +1029,7 @@ mod tests { platform: PlatformConfig::new(), ignore_platform_reqs: false, ignore_platform_req_list: vec![], - repo_cache: None, + repo_cache: Cache::new(std::env::temp_dir().join("mozart-test-cache"), false), temporary_constraints: HashMap::new(), repositories: vec![], }; @@ -1049,7 +1048,7 @@ mod tests { composer_json_content: composer_json_content.clone(), composer_json, include_dev: false, - repo_cache: None, + repo_cache: Cache::new(std::env::temp_dir().join("mozart-test-cache"), false), }; let lock = generate_lock_file(&gen_request) diff --git a/crates/mozart-registry/src/packagist.rs b/crates/mozart-registry/src/packagist.rs index 8a5f205..64ff11a 100644 --- a/crates/mozart-registry/src/packagist.rs +++ b/crates/mozart-registry/src/packagist.rs @@ -212,21 +212,20 @@ pub fn parse_p2_response(json: &str, package_name: &str) -> anyhow::Result<Vec<P /// Fetch package version metadata from the Packagist p2 API. /// -/// If `repo_cache` is provided, the JSON response is cached on disk under the -/// key `"provider-{vendor}~{package}.json"`. Subsequent calls for the same -/// package are served from cache without a network request. +/// The JSON response is cached on disk under the key +/// `"provider-{vendor}~{package}.json"`. Subsequent calls for the same +/// package are served from cache without a network request (unless the +/// cache is disabled). #[tracing::instrument(skip(repo_cache))] pub async fn fetch_package_versions( package_name: &str, - repo_cache: Option<&Cache>, + repo_cache: &Cache, ) -> anyhow::Result<Vec<PackagistVersion>> { // Build cache key: replace `/` with `~` per cache key convention let cache_key = format!("provider-{}.json", package_name.replace('/', "~")); // Check cache first - if let Some(cache) = repo_cache - && let Some(cached) = cache.read(&cache_key) - { + if let Some(cached) = repo_cache.read(&cache_key) { tracing::debug!("cache hit"); return parse_p2_response(&cached, package_name); } @@ -250,9 +249,7 @@ pub async fn fetch_package_versions( let body = response.text().await?; // Write to cache - if let Some(cache) = repo_cache { - let _ = cache.write(&cache_key, &body); - } + let _ = repo_cache.write(&cache_key, &body); parse_p2_response(&body, package_name) } diff --git a/crates/mozart-registry/src/resolver.rs b/crates/mozart-registry/src/resolver.rs index 1cd5bb6..fe59824 100644 --- a/crates/mozart-registry/src/resolver.rs +++ b/crates/mozart-registry/src/resolver.rs @@ -344,8 +344,8 @@ pub struct ResolveRequest { pub ignore_platform_reqs: bool, /// Specific platform requirements to ignore. pub ignore_platform_req_list: Vec<String>, - /// Optional on-disk repo cache for Packagist API responses. - pub repo_cache: Option<Cache>, + /// On-disk repo cache for Packagist API responses. + pub repo_cache: Cache, /// Temporary version constraint overrides (from --with flag). /// Maps package name (lowercase) to constraint string. pub temporary_constraints: HashMap<String, String>, @@ -475,7 +475,7 @@ pub async fn resolve(request: &ResolveRequest) -> Result<Vec<ResolvedPackage>, R } // Fetch available versions from Packagist - let versions = packagist::fetch_package_versions(name, request.repo_cache.as_ref()) + let versions = packagist::fetch_package_versions(name, &request.repo_cache) .await .map_err(|e| { ResolveError::DependencyFetchError(format!("Failed to fetch {}: {}", name, e)) @@ -506,16 +506,15 @@ pub async fn resolve(request: &ResolveRequest) -> Result<Vec<ResolvedPackage>, R continue; } - let versions = - match packagist::fetch_package_versions(&name, request.repo_cache.as_ref()).await { - Ok(v) => v, - Err(_) => { - // Virtual/meta packages (e.g. "psr/http-client-implementation") - // don't exist on Packagist. They are resolved via provides/replaces - // from other packages already in the pool. - continue; - } - }; + let versions = match packagist::fetch_package_versions(&name, &request.repo_cache).await { + Ok(v) => v, + Err(_) => { + // Virtual/meta packages (e.g. "psr/http-client-implementation") + // don't exist on Packagist. They are resolved via provides/replaces + // from other packages already in the pool. + continue; + } + }; for pv in &versions { let inputs = packagist_to_pool_inputs( @@ -965,7 +964,7 @@ mod tests { platform: PlatformConfig::new(), ignore_platform_reqs: false, ignore_platform_req_list: vec![], - repo_cache: None, + repo_cache: Cache::new(std::env::temp_dir().join("mozart-test-cache"), false), temporary_constraints: HashMap::new(), repositories: vec![], }; |
