aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/autoload
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-05-14 23:39:36 +0900
committernsfisis <nsfisis@gmail.com>2026-05-14 23:39:36 +0900
commitf98490f28bd784417ac3079599564726573c7702 (patch)
treea927e98075de2f33bf61efe89cfeefe4509a9319 /crates/shirabe/src/autoload
parentdb4a28d83693c6bde974395d71431d217eeb307f (diff)
downloadphp-shirabe-f98490f28bd784417ac3079599564726573c7702.tar.gz
php-shirabe-f98490f28bd784417ac3079599564726573c7702.tar.zst
php-shirabe-f98490f28bd784417ac3079599564726573c7702.zip
feat(port): port ClassMapGenerator.php
Diffstat (limited to 'crates/shirabe/src/autoload')
-rw-r--r--crates/shirabe/src/autoload/class_map_generator.rs81
1 files changed, 81 insertions, 0 deletions
diff --git a/crates/shirabe/src/autoload/class_map_generator.rs b/crates/shirabe/src/autoload/class_map_generator.rs
index df2ebd3..9a3fc0a 100644
--- a/crates/shirabe/src/autoload/class_map_generator.rs
+++ b/crates/shirabe/src/autoload/class_map_generator.rs
@@ -1 +1,82 @@
//! ref: composer/src/Composer/Autoload/ClassMapGenerator.php
+
+use indexmap::IndexMap;
+
+use shirabe_external_packages::composer::class_map_generator::class_map_generator::ClassMapGenerator as ExternalClassMapGenerator;
+use shirabe_external_packages::composer::class_map_generator::file_list::FileList;
+use shirabe_php_shim::PhpMixed;
+
+use crate::io::io_interface::IOInterface;
+
+#[derive(Debug)]
+pub struct ClassMapGenerator;
+
+impl ClassMapGenerator {
+ pub fn dump(dirs: Vec<String>, file: &str) -> anyhow::Result<()> {
+ let mut maps: IndexMap<String, String> = IndexMap::new();
+ for dir in dirs {
+ maps.extend(ClassMapGenerator::create_map(
+ PhpMixed::String(dir),
+ None,
+ None,
+ None,
+ None,
+ &mut IndexMap::new(),
+ )?);
+ }
+ let maps_php = PhpMixed::Array(
+ maps.into_iter()
+ .map(|(k, v)| (k, Box::new(PhpMixed::String(v))))
+ .collect(),
+ );
+ std::fs::write(file, format!("<?php return {};", shirabe_php_shim::var_export(&maps_php, true)))?;
+ Ok(())
+ }
+
+ pub fn create_map(
+ path: PhpMixed,
+ excluded: Option<String>,
+ io: Option<Box<dyn IOInterface>>,
+ namespace: Option<String>,
+ autoload_type: Option<String>,
+ scanned_files: &mut IndexMap<String, bool>,
+ ) -> anyhow::Result<IndexMap<String, String>> {
+ let generator = ExternalClassMapGenerator::new(vec!["php".to_string(), "inc".to_string(), "hh".to_string()]);
+ let mut file_list = FileList::new();
+ file_list.files = scanned_files.clone();
+ generator.avoid_duplicate_scans(&file_list);
+
+ generator.scan_paths(path, excluded.as_deref(), autoload_type.as_deref().unwrap_or("classmap"), namespace.as_deref())?;
+
+ let class_map = generator.get_class_map();
+
+ *scanned_files = file_list.files;
+
+ if let Some(io) = &io {
+ for msg in class_map.get_psr_violations() {
+ io.write_error(&format!("<warning>{}</warning>", msg));
+ }
+
+ for (class, paths) in class_map.get_ambiguous_classes() {
+ if paths.len() > 1 {
+ io.write_error(&format!(
+ "<warning>Warning: Ambiguous class resolution, \"{}\" was found {}x: in \"{}\" and \"{}\", the first will be used.</warning>",
+ class,
+ paths.len() + 1,
+ class_map.get_class_path(&class),
+ paths.join("\", \""),
+ ));
+ } else {
+ io.write_error(&format!(
+ "<warning>Warning: Ambiguous class resolution, \"{}\" was found in both \"{}\" and \"{}\", the first will be used.</warning>",
+ class,
+ class_map.get_class_path(&class),
+ paths.join("\", \""),
+ ));
+ }
+ }
+ }
+
+ Ok(class_map.get_map())
+ }
+}