Skip to content

Commit afcfa90

Browse files
committed
fix: PHP - Add composer autoload when a cached vendor folder is used as the autoload files include a list of classes outside of the vendor folder
1 parent 45b269d commit afcfa90

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

pkg/php/php.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,16 @@ func composerInstall(ctx *gcp.Context, flags []string) error {
147147
return nil
148148
}
149149

150-
// ComposerInstall runs `composer install`, using the cache iff a lock file is present.
150+
// composerDumpAutoload runs `composer dump-autoload` with the given flags.
151+
func composerDumpAutoload(ctx *gcp.Context, flags []string) error {
152+
cmd := append([]string{"composer", "dump-autoload"}, flags...)
153+
if _, err := ctx.Exec(cmd, gcp.WithUserAttribution); err != nil {
154+
return err
155+
}
156+
return nil
157+
}
158+
159+
// ComposerInstall runs `composer install`, using the cache if a lock file is present.
151160
// It creates a layer, so it returns the layer so that the caller may further modify it
152161
// if they desire.
153162
func ComposerInstall(ctx *gcp.Context, cacheTag string) (*libcnb.Layer, error) {
@@ -201,6 +210,12 @@ func ComposerInstall(ctx *gcp.Context, cacheTag string) (*libcnb.Layer, error) {
201210
if _, err := ctx.Exec([]string{"cp", "--archive", layerVendor, Vendor}, gcp.WithUserTimingAttribution); err != nil {
202211
return nil, err
203212
}
213+
// Why re-generate the autoload files? Since these autoload files include list of all PSR auto-loaded files (including local workspace files)
214+
// This will cause issues for files added/removed since the last cache as these won't be within the classmap
215+
ctx.Logf("Re-generating autoload files.")
216+
if err := composerDumpAutoload(ctx, []string{"--optimize"}); err != nil {
217+
return nil, err
218+
}
204219
} else {
205220
ctx.Logf("Installing application dependencies.")
206221
// Clear layer so we don't end up with outdated dependencies (e.g. something was removed from composer.json).

0 commit comments

Comments
 (0)