|
| 1 | +package extgen |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "os" |
| 6 | + "regexp" |
| 7 | + "strings" |
| 8 | +) |
| 9 | + |
| 10 | +var phpModuleParser = regexp.MustCompile(`//\s*export_php:module\s*(.*)`) |
| 11 | + |
| 12 | +// phpModule represents a PHP module with optional init and shutdown functions |
| 13 | +type phpModule struct { |
| 14 | + InitFunc string // Name of the init function |
| 15 | + ShutdownFunc string // Name of the shutdown function |
| 16 | +} |
| 17 | + |
| 18 | +// ModuleParser parses PHP module directives from Go source files |
| 19 | +type ModuleParser struct{} |
| 20 | + |
| 21 | +// parse parses the source file for PHP module directives |
| 22 | +func (mp *ModuleParser) parse(filename string) (*phpModule, error) { |
| 23 | + file, err := os.Open(filename) |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + defer file.Close() |
| 28 | + |
| 29 | + scanner := bufio.NewScanner(file) |
| 30 | + for scanner.Scan() { |
| 31 | + line := strings.TrimSpace(scanner.Text()) |
| 32 | + if matches := phpModuleParser.FindStringSubmatch(line); matches != nil { |
| 33 | + moduleInfo := strings.TrimSpace(matches[1]) |
| 34 | + return mp.parseModuleInfo(moduleInfo) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // No module directive found |
| 39 | + return nil, nil |
| 40 | +} |
| 41 | + |
| 42 | +// parseModuleInfo parses the module info string to extract init and shutdown function names |
| 43 | +func (mp *ModuleParser) parseModuleInfo(moduleInfo string) (*phpModule, error) { |
| 44 | + module := &phpModule{} |
| 45 | + |
| 46 | + // Split the module info by commas |
| 47 | + parts := strings.Split(moduleInfo, ",") |
| 48 | + |
| 49 | + for _, part := range parts { |
| 50 | + part = strings.TrimSpace(part) |
| 51 | + if part == "" { |
| 52 | + continue |
| 53 | + } |
| 54 | + |
| 55 | + // Split each part by equals sign |
| 56 | + keyValue := strings.SplitN(part, "=", 2) |
| 57 | + if len(keyValue) != 2 { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + key := strings.TrimSpace(keyValue[0]) |
| 62 | + value := strings.TrimSpace(keyValue[1]) |
| 63 | + |
| 64 | + switch key { |
| 65 | + case "init": |
| 66 | + module.InitFunc = value |
| 67 | + case "shutdown": |
| 68 | + module.ShutdownFunc = value |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return module, nil |
| 73 | +} |
0 commit comments