feat(extgen): automatically add "runtime/cgo" to the imports if necessary

This commit is contained in:
Alexandre Daubois
2025-11-14 10:03:40 +01:00
committed by Kévin Dunglas
parent 861b345b05
commit f7298557aa

View File

@@ -47,7 +47,7 @@ func (gg *GoFileGenerator) buildContent() (string, error) {
filteredImports := make([]string, 0, len(imports))
for _, imp := range imports {
if imp != `"C"` && imp != `"unsafe"` && imp != `"github.com/dunglas/frankenphp"` {
if imp != `"C"` && imp != `"unsafe"` && imp != `"github.com/dunglas/frankenphp"` && imp != `"runtime/cgo"` {
filteredImports = append(filteredImports, imp)
}
}
@@ -55,6 +55,19 @@ func (gg *GoFileGenerator) buildContent() (string, error) {
classes := make([]phpClass, len(gg.generator.Classes))
copy(classes, gg.generator.Classes)
if len(classes) > 0 {
hasCgo := false
for _, imp := range imports {
if imp == `"runtime/cgo"` {
hasCgo = true
break
}
}
if !hasCgo {
filteredImports = append(filteredImports, `"runtime/cgo"`)
}
}
templateContent, err := gg.getTemplateContent(goTemplateData{
PackageName: SanitizePackageName(gg.generator.BaseName),
BaseName: gg.generator.BaseName,