@@ -264,10 +264,7 @@ impl Composer {
264264
265265 let link = sandbox_dir. join ( relative_path) ;
266266 if link. symlink_metadata ( ) . is_ok ( ) {
267- debug ! (
268- "Skipping autoload symlink, path already exists: {:?}" ,
269- link
270- ) ;
267+ debug ! ( "Skipping autoload symlink, path already exists: {:?}" , link) ;
271268 continue ;
272269 }
273270
@@ -291,54 +288,63 @@ impl Composer {
291288 let mut paths = vec ! [ ] ;
292289
293290 for section in & [ "autoload" , "autoload-dev" ] {
294- if let Some ( autoload) = composer_json. get ( section) {
295- for key in & [ "psr-4" , "psr-0" ] {
296- if let Some ( mappings) = autoload. get ( key) . and_then ( |v| v. as_object ( ) ) {
297- for ( _, path_value) in mappings {
298- match path_value {
299- Value :: String ( s) if !s. is_empty ( ) => {
300- paths. push ( s. trim_end_matches ( '/' ) . to_string ( ) ) ;
301- }
302- Value :: Array ( arr) => {
303- for v in arr {
304- if let Some ( s) = v. as_str ( ) {
305- if !s. is_empty ( ) {
306- paths. push (
307- s. trim_end_matches ( '/' ) . to_string ( ) ,
308- ) ;
309- }
310- }
311- }
312- }
313- _ => { }
314- }
315- }
316- }
317- }
291+ let Some ( autoload) = composer_json. get ( section) else {
292+ continue ;
293+ } ;
318294
319- if let Some ( classmap) = autoload. get ( "classmap" ) . and_then ( |v| v. as_array ( ) ) {
320- for v in classmap {
321- if let Some ( s) = v. as_str ( ) {
322- if !s. is_empty ( ) {
323- paths. push ( s. trim_end_matches ( '/' ) . to_string ( ) ) ;
324- }
325- }
295+ for key in & [ "psr-4" , "psr-0" ] {
296+ if let Some ( mappings) = autoload. get ( key) . and_then ( |v| v. as_object ( ) ) {
297+ for ( _, path_value) in mappings {
298+ Self :: collect_psr_paths ( path_value, & mut paths) ;
326299 }
327300 }
301+ }
328302
329- if let Some ( files) = autoload. get ( "files" ) . and_then ( |v| v. as_array ( ) ) {
330- for v in files {
331- if let Some ( s) = v. as_str ( ) {
332- if !s. is_empty ( ) {
333- paths. push ( s. to_string ( ) ) ;
334- }
335- }
336- }
303+ Self :: collect_string_array_paths ( autoload. get ( "classmap" ) , true , & mut paths) ;
304+ Self :: collect_string_array_paths ( autoload. get ( "files" ) , false , & mut paths) ;
305+ }
306+
307+ paths
308+ }
309+
310+ fn collect_psr_paths ( path_value : & Value , paths : & mut Vec < String > ) {
311+ match path_value {
312+ Value :: String ( s) if !s. is_empty ( ) => {
313+ paths. push ( s. trim_end_matches ( '/' ) . to_string ( ) ) ;
314+ }
315+ Value :: Array ( arr) => {
316+ for s in arr
317+ . iter ( )
318+ . filter_map ( |v| v. as_str ( ) )
319+ . filter ( |s| !s. is_empty ( ) )
320+ {
321+ paths. push ( s. trim_end_matches ( '/' ) . to_string ( ) ) ;
337322 }
338323 }
324+ _ => { }
339325 }
326+ }
340327
341- paths
328+ fn collect_string_array_paths (
329+ value : Option < & Value > ,
330+ trim_trailing_slash : bool ,
331+ paths : & mut Vec < String > ,
332+ ) {
333+ let Some ( arr) = value. and_then ( |v| v. as_array ( ) ) else {
334+ return ;
335+ } ;
336+
337+ for s in arr
338+ . iter ( )
339+ . filter_map ( |v| v. as_str ( ) )
340+ . filter ( |s| !s. is_empty ( ) )
341+ {
342+ if trim_trailing_slash {
343+ paths. push ( s. trim_end_matches ( '/' ) . to_string ( ) ) ;
344+ } else {
345+ paths. push ( s. to_string ( ) ) ;
346+ }
347+ }
342348 }
343349}
344350
@@ -802,8 +808,18 @@ pub mod test {
802808 Composer :: create_autoload_symlinks ( pkg) ?;
803809
804810 let sandbox_dir = PathBuf :: from ( pkg. directory ( ) ) ;
805- assert ! ( sandbox_dir. join( "app" ) . symlink_metadata( ) . unwrap( ) . file_type( ) . is_symlink( ) ) ;
806- assert ! ( sandbox_dir. join( "tests" ) . symlink_metadata( ) . unwrap( ) . file_type( ) . is_symlink( ) ) ;
811+ assert ! ( sandbox_dir
812+ . join( "app" )
813+ . symlink_metadata( )
814+ . unwrap( )
815+ . file_type( )
816+ . is_symlink( ) ) ;
817+ assert ! ( sandbox_dir
818+ . join( "tests" )
819+ . symlink_metadata( )
820+ . unwrap( )
821+ . file_type( )
822+ . is_symlink( ) ) ;
807823 assert_eq ! (
808824 std:: fs:: read_link( sandbox_dir. join( "app" ) ) ?,
809825 project_dir. join( "app" )
@@ -878,7 +894,12 @@ pub mod test {
878894 Composer :: create_autoload_symlinks ( pkg) ?;
879895
880896 let sandbox_dir = PathBuf :: from ( pkg. directory ( ) ) ;
881- assert ! ( sandbox_dir. join( "app" ) . symlink_metadata( ) . unwrap( ) . file_type( ) . is_symlink( ) ) ;
897+ assert ! ( sandbox_dir
898+ . join( "app" )
899+ . symlink_metadata( )
900+ . unwrap( )
901+ . file_type( )
902+ . is_symlink( ) ) ;
882903 assert ! ( !sandbox_dir. join( "does-not-exist" ) . exists( ) ) ;
883904
884905 Ok ( ( ) )
0 commit comments