diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-02-23 01:18:58 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-02-23 01:18:58 +0900 |
| commit | 7372dc668476cde4bcb789e586cb9a65af1afca0 (patch) | |
| tree | aeeec34820b3714698407c32fc16942002129561 /crates/mozart/src/commands/dump_autoload.rs | |
| parent | 40bd784824dc5f40e22908ff35d4ae693696ba89 (diff) | |
| download | php-mozart-7372dc668476cde4bcb789e586cb9a65af1afca0.tar.gz php-mozart-7372dc668476cde4bcb789e586cb9a65af1afca0.tar.zst php-mozart-7372dc668476cde4bcb789e586cb9a65af1afca0.zip | |
fix(dump-autoload): add config defaults, CLI validation, and class count output
Read optimize-autoloader, classmap-authoritative, apcu-autoloader from
composer.json config section. Reject --dev with --no-dev and --strict-psr/
--strict-ambiguous without --optimize. Emit pre/post generation messages
with class count in optimized mode. Track ambiguous class mappings and
exit with code 2 when --strict-ambiguous detects conflicts.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'crates/mozart/src/commands/dump_autoload.rs')
| -rw-r--r-- | crates/mozart/src/commands/dump_autoload.rs | 86 |
1 files changed, 80 insertions, 6 deletions
diff --git a/crates/mozart/src/commands/dump_autoload.rs b/crates/mozart/src/commands/dump_autoload.rs index 43108ab..0828288 100644 --- a/crates/mozart/src/commands/dump_autoload.rs +++ b/crates/mozart/src/commands/dump_autoload.rs @@ -53,6 +53,11 @@ pub async fn execute( cli: &super::Cli, console: &mozart_core::console::Console, ) -> anyhow::Result<()> { + // A: --dev / --no-dev conflict detection + if args.dev && args.no_dev { + anyhow::bail!("You can not use both --no-dev and --dev as they conflict with each other."); + } + let working_dir = match &cli.working_dir { Some(dir) => PathBuf::from(dir), None => std::env::current_dir()?, @@ -61,6 +66,59 @@ pub async fn execute( let vendor_dir = working_dir.join("vendor"); let dev_mode = !args.no_dev; + // B: Read config-driven defaults from composer.json + let composer_json_path = working_dir.join("composer.json"); + let mut composer_config = super::config::ComposerConfig::defaults(); + if composer_json_path.exists() + && let Ok(content) = std::fs::read_to_string(&composer_json_path) + && let Ok(value) = serde_json::from_str::<serde_json::Value>(&content) + && let Some(cfg_obj) = value.get("config").and_then(|v| v.as_object()) { + let overrides: std::collections::BTreeMap<String, serde_json::Value> = cfg_obj + .iter() + .map(|(k, v)| (k.clone(), v.clone())) + .collect(); + composer_config.merge(&overrides); + } + + let optimize = args.optimize + || composer_config + .get("optimize-autoloader") + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let classmap_authoritative = args.classmap_authoritative + || composer_config + .get("classmap-authoritative") + .and_then(|v| v.as_bool()) + .unwrap_or(false); + let apcu = args.apcu + || args.apcu_prefix.is_some() + || composer_config + .get("apcu-autoloader") + .and_then(|v| v.as_bool()) + .unwrap_or(false); + + // C: Validate --strict-psr and --strict-ambiguous require optimize + let effective_optimize = optimize || classmap_authoritative; + if args.strict_psr && !effective_optimize { + anyhow::bail!( + "--strict-psr mode only works with optimized autoloader, use --optimize or --classmap-authoritative." + ); + } + if args.strict_ambiguous && !effective_optimize { + anyhow::bail!( + "--strict-ambiguous mode only works with optimized autoloader, use --optimize or --classmap-authoritative." + ); + } + + // D: Pre-generation output message + if classmap_authoritative { + console.info("Generating optimized autoload files (authoritative)"); + } else if optimize { + console.info("Generating optimized autoload files"); + } else { + console.info("Generating autoload files"); + } + // Determine suffix: read from existing autoload.php, or from lock file, or generate let suffix = mozart_autoload::autoload::determine_suffix(&working_dir, &vendor_dir)?; @@ -69,21 +127,37 @@ pub async fn execute( return Ok(()); } - mozart_autoload::autoload::generate(&mozart_autoload::autoload::AutoloadConfig { + // E: AutoloadConfig construction using config-merged values + let autoload_config = mozart_autoload::autoload::AutoloadConfig { project_dir: working_dir, vendor_dir, dev_mode, suffix, - classmap_authoritative: args.classmap_authoritative, - optimize: args.optimize, - apcu: args.apcu, + classmap_authoritative, + optimize, + apcu, apcu_prefix: args.apcu_prefix.clone(), strict_psr: args.strict_psr, + strict_ambiguous: args.strict_ambiguous, platform_check: mozart_autoload::autoload::PlatformCheckMode::Full, ignore_platform_reqs: args.ignore_platform_reqs, - })?; + }; + + // F: Handle GenerateResult and post-generation messages + let result = mozart_autoload::autoload::generate(&autoload_config)?; - console.info("Generated autoload files"); + if effective_optimize || classmap_authoritative { + console.info(&format!( + "Generated optimized autoload files containing {} classes", + result.class_count + )); + } else { + console.info("Generated autoload files"); + } + + if args.strict_ambiguous && result.has_ambiguous_classes { + std::process::exit(2); + } Ok(()) } |
