| 14 | var namespaceRegex = regexp.MustCompile(`//\s*export_php:namespace\s+(.+)`) |
| 15 | |
| 16 | func (np *NamespaceParser) parse(filename string) (foundNamespace string, err error) { |
| 17 | file, err := os.Open(filename) |
| 18 | if err != nil { |
| 19 | return "", err |
| 20 | } |
| 21 | |
| 22 | defer func() { |
| 23 | err = errors.Join(err, file.Close()) |
| 24 | }() |
| 25 | |
| 26 | var lineNumber, foundLineNumber int |
| 27 | |
| 28 | scanner := bufio.NewScanner(file) |
| 29 | for scanner.Scan() { |
| 30 | lineNumber++ |
| 31 | line := strings.TrimSpace(scanner.Text()) |
| 32 | if matches := namespaceRegex.FindStringSubmatch(line); matches != nil { |
| 33 | namespace := strings.TrimSpace(matches[1]) |
| 34 | if foundNamespace != "" { |
| 35 | return "", fmt.Errorf("multiple namespace declarations found: first at line %d, second at line %d", foundLineNumber, lineNumber) |
| 36 | } |
| 37 | |
| 38 | foundNamespace = namespace |
| 39 | foundLineNumber = lineNumber |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | return foundNamespace, scanner.Err() |
| 44 | } |