88 "go.uber.org/zap"
99 "io/fs"
1010 "path/filepath"
11+ "sort"
1112 "strings"
1213)
1314
@@ -25,27 +26,48 @@ func (Inspector) String() string {
2526}
2627
2728func (Inspector ) CheckDir (ctx context.Context , dir string ) bool {
28- return utils .IsFile (filepath .Join (dir , "composer.json" ))
29+ return utils .IsFile (filepath .Join (dir , "composer.json" )) ||
30+ utils .IsFile (filepath .Join (dir , "composer.lock" )) ||
31+ utils .IsFile (filepath .Join (dir , "installed.json" )) ||
32+ utils .IsFile (filepath .Join (dir , "vendor" , "composer" , "installed.json" ))
2933}
3034
3135func (Inspector ) InspectProject (ctx context.Context ) error {
3236 logger := logctx .Use (ctx )
3337 task := model .UseInspectionTask (ctx )
3438 dir := task .Dir ()
35- manifest , e := readManifest (ctx , filepath .Join (dir , "composer.json" ))
36- if e != nil {
37- return e
39+ manifestPath := filepath .Join (dir , "composer.json" )
40+ modulePath := manifestPath
41+ manifest := & Manifest {}
42+ var e error
43+ if utils .IsFile (manifestPath ) {
44+ manifest , e = readManifest (ctx , manifestPath )
45+ if e != nil {
46+ return e
47+ }
48+ } else {
49+ logger .Sugar ().Infof ("composer.json not found, fallback to installed/lock files. dir=%s" , dir )
50+ modulePath = filepath .Join (dir , "installed.json" )
51+ if ! utils .IsFile (modulePath ) {
52+ modulePath = filepath .Join (dir , "vendor" , "composer" , "installed.json" )
53+ if ! utils .IsFile (modulePath ) {
54+ modulePath = filepath .Join (dir , "composer.lock" )
55+ }
56+ }
3857 }
3958 module := & model.Module {
4059 PackageManager : "composer" ,
4160 ModuleName : manifest .Name ,
4261 ModuleVersion : manifest .Version ,
43- ModulePath : filepath .Join (dir , "composer.json" ),
62+ ModulePath : modulePath ,
63+ }
64+ if module .ModuleName == "" {
65+ module .ModuleName = filepath .Base (dir )
4466 }
4567 lockfilePkgs := map [string ]Package {}
4668
4769 {
48- if ! utils .IsPathExist (filepath .Join (dir , "composer.lock" )) {
70+ if utils . IsFile ( manifestPath ) && ! utils .IsPathExist (filepath .Join (dir , "composer.lock" )) {
4971 logger .Info ("composer.lock doesn't exists. Try to generate it" )
5072 if e := doComposerInstall (context .TODO (), dir ); e != nil {
5173 logger .Sugar ().Warnf ("Do composer install fail. %s" , e .Error ())
@@ -59,6 +81,18 @@ func (Inspector) InspectProject(ctx context.Context) error {
5981 if e != nil {
6082 logger .Sugar ().Infof ("Read composer lock file failed: %s" , e .Error ())
6183 }
84+ installedPaths := []string {
85+ filepath .Join (dir , "installed.json" ),
86+ filepath .Join (dir , "vendor" , "composer" , "installed.json" ),
87+ }
88+ for _ , installedPath := range installedPaths {
89+ installedPkgs , ie := readComposerInstalledFile (installedPath )
90+ if ie != nil {
91+ logger .Sugar ().Debugf ("Read installed.json failed: %s" , ie .Error ())
92+ continue
93+ }
94+ pkgs = append (pkgs , installedPkgs ... )
95+ }
6296 pkgs = append (pkgs , vendorScan (ctx , filepath .Join (dir , "vendor" ))... )
6397 for _ , it := range pkgs {
6498 if it .Version == "" || isVersionConstrain (it .Version ) {
@@ -68,8 +102,22 @@ func (Inspector) InspectProject(ctx context.Context) error {
68102 }
69103 }
70104
105+ roots := map [string ]string {}
71106 for _ , requiredPkg := range manifest .Require {
72- node := _buildDepTree (lockfilePkgs , map [string ]struct {}{}, requiredPkg .Name , requiredPkg .Version )
107+ roots [requiredPkg .Name ] = requiredPkg .Version
108+ }
109+ if len (roots ) == 0 {
110+ for name := range lockfilePkgs {
111+ roots [name ] = ""
112+ }
113+ }
114+ var rootNames []string
115+ for name := range roots {
116+ rootNames = append (rootNames , name )
117+ }
118+ sort .Strings (rootNames )
119+ for _ , name := range rootNames {
120+ node := _buildDepTree (lockfilePkgs , map [string ]struct {}{}, name , roots [name ])
73121 if node != nil {
74122 module .Dependencies = append (module .Dependencies , * node )
75123 }
0 commit comments