-
Notifications
You must be signed in to change notification settings - Fork 0
/
php.go
91 lines (68 loc) · 1.6 KB
/
php.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package analysis
import (
"fmt"
"regexp"
"strings"
)
//PhpAnalysis the default analysis struct
type PhpAnalysis struct {
}
const (
regexNeedlePhp = `(?im)((?:\\{1,2}\w+|\w+\\{1,2})(?:\w+\\{0,2})+)`
regexNamespace = `(?im)(?:namespace\s+)(.*)(?:[;])`
regexEntryPoint = `(?im)(?:class\s+)(\w+)|(?:trait\s+)(\w+)|(?:interface\s+)(\w+)`
)
func (a PhpAnalysis) GetNeedleKey() string {
return regexNeedlePhp
}
func (a PhpAnalysis) ExtractMainEntrypointSrc(text string) (res string, err error) {
re, err := regexp.Compile(regexNamespace)
if err != nil {
return
}
matches := re.FindStringSubmatch(text)
if len(matches) != 2 {
return
}
return strings.TrimSpace(matches[1]), nil
}
func (a PhpAnalysis) ExtractImportUsage(text string) (res string, err error) {
re, err := regexp.Compile(regexNeedlePhp)
if err != nil {
return
}
matches := re.FindStringSubmatch(text)
if len(matches) != 2 {
return
}
if strings.Contains(matches[1], "namespace") {
return
}
return matches[1], nil
}
func (a PhpAnalysis) FindMainEntrypointTitle(text string) (res string, err error) {
re, err := regexp.Compile(regexEntryPoint)
if err != nil {
return
}
matches := re.FindStringSubmatch(text)
if len(matches) < 2 {
return
}
//The class condition
if matches[1] != "" {
return matches[1], nil
}
//The trait condition
if matches[2] != "" {
return matches[2], nil
}
//The interface condition
if matches[3] != "" {
return matches[3], nil
}
return
}
func (a PhpAnalysis) GenerateEntryPointSrc(src string, entrypointTitle string) string {
return fmt.Sprintf("%s\\%s", src, entrypointTitle)
}