Skip to content

Commit afb7656

Browse files
committed
[TASK] Refactor path mapper factory to support module registry
This change introduce a PathMapperRegistry to cleanup the PathMapper factory. All available PathMapper must be imported in main.go and each PathMapper register in their own init method (check dummypathmapper for an example)
1 parent d5d007c commit afb7656

5 files changed

Lines changed: 70 additions & 49 deletions

File tree

dummypathmapper/dummypathmapper.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,29 @@ package dummypathmapper
33
import (
44
"github.com/dfeyer/flow-debugproxy/config"
55
"github.com/dfeyer/flow-debugproxy/logger"
6+
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
67
"github.com/dfeyer/flow-debugproxy/pathmapping"
78
)
89

10+
const framework = "dummy"
11+
12+
func init() {
13+
p := &PathMapper{}
14+
pathmapperfactory.Register(framework, p)
15+
}
16+
917
// PathMapper handle the mapping between real code and proxy
1018
type PathMapper struct {
11-
Config *config.Config
12-
Logger *logger.Logger
13-
PathMapping *pathmapping.PathMapping
19+
config *config.Config
20+
logger *logger.Logger
21+
pathMapping *pathmapping.PathMapping
22+
}
23+
24+
// Initialize the path mapper dependencies
25+
func (p *PathMapper) Initialize(c *config.Config, l *logger.Logger, m *pathmapping.PathMapping) {
26+
p.config = c
27+
p.logger = l
28+
p.pathMapping = m
1429
}
1530

1631
// ApplyMappingToTextProtocol change file path in xDebug text protocol

flowpathmapper/flowpathmapper.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/dfeyer/flow-debugproxy/config"
55
"github.com/dfeyer/flow-debugproxy/errorhandler"
66
"github.com/dfeyer/flow-debugproxy/logger"
7+
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
78
"github.com/dfeyer/flow-debugproxy/pathmapping"
89

910
"bytes"
@@ -17,6 +18,7 @@ import (
1718

1819
const (
1920
h = "%s"
21+
framework = "flow"
2022
cachePathPattern = "@base@/Data/Temporary/@context@/Cache/Code/Flow_Object_Classes/@filename@.php"
2123
)
2224

@@ -28,11 +30,23 @@ var (
2830
regexpDot = regexp.MustCompile(`[\./]`)
2931
)
3032

33+
func init() {
34+
p := &PathMapper{}
35+
pathmapperfactory.Register(framework, p)
36+
}
37+
3138
// PathMapper handle the mapping between real code and proxy
3239
type PathMapper struct {
33-
Config *config.Config
34-
Logger *logger.Logger
35-
PathMapping *pathmapping.PathMapping
40+
config *config.Config
41+
logger *logger.Logger
42+
pathMapping *pathmapping.PathMapping
43+
}
44+
45+
// Initialize the path mapper dependencies
46+
func (p *PathMapper) Initialize(c *config.Config, l *logger.Logger, m *pathmapping.PathMapping) {
47+
p.config = c
48+
p.logger = l
49+
p.pathMapping = m
3650
}
3751

3852
// ApplyMappingToTextProtocol change file path in xDebug text protocol
@@ -47,7 +61,7 @@ func (p *PathMapper) ApplyMappingToXML(message []byte) []byte {
4761
// update xml length count
4862
s := strings.Split(string(message), "\x00")
4963
i, err := strconv.Atoi(s[0])
50-
errorhandler.PanicHandling(err, p.Logger)
64+
errorhandler.PanicHandling(err, p.logger)
5165
l := len(s[1])
5266
if i != l {
5367
message = bytes.Replace(message, []byte(strconv.Itoa(i)), []byte(strconv.Itoa(l)), 1)
@@ -61,7 +75,7 @@ func (p *PathMapper) doTextPathMapping(message []byte) []byte {
6175
for _, match := range regexpPhpFile.FindAllStringSubmatch(string(message), -1) {
6276
originalPath := match[1]
6377
path := p.mapPath(originalPath)
64-
p.Logger.Debug("doTextPathMapping %s >>> %s", path, originalPath)
78+
p.logger.Debug("doTextPathMapping %s >>> %s", path, originalPath)
6579
processedMapping[path] = originalPath
6680
}
6781

@@ -74,7 +88,7 @@ func (p *PathMapper) doTextPathMapping(message []byte) []byte {
7488

7589
func (p *PathMapper) getCachePath(base, filename string) string {
7690
cachePath := strings.Replace(cachePathPattern, "@base@", base, 1)
77-
cachePath = strings.Replace(cachePath, "@context@", p.Config.Context, 1)
91+
cachePath = strings.Replace(cachePath, "@context@", p.config.Context, 1)
7892
return strings.Replace(cachePath, "@filename@", filename, 1)
7993
}
8094

@@ -83,16 +97,16 @@ func (p *PathMapper) doXMLPathMapping(b []byte) []byte {
8397
for _, match := range regexpFilename.FindAllStringSubmatch(string(b), -1) {
8498
path := p.getCachePath(match[1], match[2])
8599
if _, ok := processedMapping[path]; ok == false {
86-
if originalPath, exist := p.PathMapping.Get(path); exist {
87-
if p.Config.VeryVerbose {
88-
p.Logger.Info("Umpa Lumpa can help you, he know the mapping\n%s\n%s\n", p.Logger.Colorize(">>> "+fmt.Sprintf(h, path), "yellow"), p.Logger.Colorize(">>> "+fmt.Sprintf(h, p.getRealFilename(originalPath)), "green"))
100+
if originalPath, exist := p.pathMapping.Get(path); exist {
101+
if p.config.VeryVerbose {
102+
p.logger.Info("Umpa Lumpa can help you, he know the mapping\n%s\n%s\n", p.logger.Colorize(">>> "+fmt.Sprintf(h, path), "yellow"), p.logger.Colorize(">>> "+fmt.Sprintf(h, p.getRealFilename(originalPath)), "green"))
89103
}
90104
processedMapping[path] = originalPath
91-
p.Logger.Debug("doXMLPathMapping mapping exist %s >>> %s", path, originalPath)
105+
p.logger.Debug("doXMLPathMapping mapping exist %s >>> %s", path, originalPath)
92106
} else {
93107
originalPath = p.readOriginalPathFromCache(path)
94108
processedMapping[path] = originalPath
95-
p.Logger.Debug("doXMLPathMapping missing mapping %s >>> %s", path, originalPath)
109+
p.logger.Debug("doXMLPathMapping missing mapping %s >>> %s", path, originalPath)
96110
}
97111
}
98112
}
@@ -113,7 +127,7 @@ func (p *PathMapper) getRealFilename(path string) string {
113127

114128
func (p *PathMapper) mapPath(originalPath string) string {
115129
if strings.Contains(originalPath, "/Packages/") {
116-
p.Logger.Debug("Path %s is a Flow Package file", originalPath)
130+
p.logger.Debug("Path %s is a Flow Package file", originalPath)
117131
cachePath := p.getCachePath(p.buildClassNameFromPath(originalPath))
118132
realPath := p.getRealFilename(cachePath)
119133
if _, err := os.Stat(realPath); err == nil {
@@ -125,28 +139,28 @@ func (p *PathMapper) mapPath(originalPath string) string {
125139
}
126140

127141
func (p *PathMapper) setPathMapping(path string, originalPath string) string {
128-
if p.Config.Verbose {
129-
p.Logger.Info("%s", "Our Umpa Lumpa take care of your mapping and they did a great job, they found a proxy for you:")
130-
p.Logger.Info(">>> %s\n", path)
142+
if p.config.Verbose {
143+
p.logger.Info("%s", "Our Umpa Lumpa take care of your mapping and they did a great job, they found a proxy for you:")
144+
p.logger.Info(">>> %s\n", path)
131145
}
132146

133-
if p.PathMapping.Has(path) == false {
134-
p.PathMapping.Set(path, originalPath)
147+
if p.pathMapping.Has(path) == false {
148+
p.pathMapping.Set(path, originalPath)
135149
}
136150
return path
137151
}
138152

139153
func (p *PathMapper) readOriginalPathFromCache(path string) string {
140154
dat, err := ioutil.ReadFile(path)
141-
errorhandler.PanicHandling(err, p.Logger)
155+
errorhandler.PanicHandling(err, p.logger)
142156
match := regexpPathAndFilename.FindStringSubmatch(string(dat))
143-
p.Logger.Debug("readOriginalPathFromCache %s", path)
157+
p.logger.Debug("readOriginalPathFromCache %s", path)
144158
if len(match) == 2 {
145159
originalPath := match[1]
146-
if p.Config.VeryVerbose {
147-
p.Logger.Info("Umpa Lumpa need to work harder, need to reverse this one\n>>> %s\n>>> %s\n", p.Logger.Colorize(fmt.Sprintf(h, path), "yellow"), p.Logger.Colorize(fmt.Sprintf(h, originalPath), "green"))
160+
if p.config.VeryVerbose {
161+
p.logger.Info("Umpa Lumpa need to work harder, need to reverse this one\n>>> %s\n>>> %s\n", p.logger.Colorize(fmt.Sprintf(h, path), "yellow"), p.logger.Colorize(fmt.Sprintf(h, originalPath), "green"))
148162
}
149-
p.Logger.Debug("readOriginalPathFromCache %s >>> %s", path, originalPath)
163+
p.logger.Debug("readOriginalPathFromCache %s >>> %s", path, originalPath)
150164
p.setPathMapping(path, originalPath)
151165
return originalPath
152166
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,17 @@ package main
22

33
import (
44
"github.com/dfeyer/flow-debugproxy/config"
5+
56
"github.com/dfeyer/flow-debugproxy/errorhandler"
67
"github.com/dfeyer/flow-debugproxy/logger"
78
"github.com/dfeyer/flow-debugproxy/pathmapperfactory"
89
"github.com/dfeyer/flow-debugproxy/pathmapping"
910
"github.com/dfeyer/flow-debugproxy/xdebugproxy"
1011

12+
// Register available path mapper
13+
_ "github.com/dfeyer/flow-debugproxy/dummypathmapper"
14+
_ "github.com/dfeyer/flow-debugproxy/flowpathmapper"
15+
1116
"github.com/codegangsta/cli"
1217

1318
"net"

pathmapperfactory/pathmapperfactory.go

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,26 @@ package pathmapperfactory
22

33
import (
44
"github.com/dfeyer/flow-debugproxy/config"
5-
"github.com/dfeyer/flow-debugproxy/dummypathmapper"
6-
"github.com/dfeyer/flow-debugproxy/flowpathmapper"
75
"github.com/dfeyer/flow-debugproxy/logger"
86
"github.com/dfeyer/flow-debugproxy/pathmapping"
97
"github.com/dfeyer/flow-debugproxy/xdebugproxy"
108

119
"errors"
1210
)
1311

14-
const (
15-
flow = "flow"
16-
goaop = "goaop"
17-
dummy = "dummy"
18-
)
12+
var pathMapperRegistry = map[string]xdebugproxy.XDebugProcessorPlugin{}
13+
14+
// Register a path mapper
15+
func Register(f string, p xdebugproxy.XDebugProcessorPlugin) {
16+
pathMapperRegistry[f] = p
17+
}
1918

2019
// Create return a pathmapper for the given framework
2120
func Create(c *config.Config, p *pathmapping.PathMapping, l *logger.Logger) (xdebugproxy.XDebugProcessorPlugin, error) {
22-
var pathmapper xdebugproxy.XDebugProcessorPlugin
23-
switch {
24-
case c.Framework == flow:
25-
pathmapper = &flowpathmapper.PathMapper{
26-
Config: c,
27-
Logger: l,
28-
PathMapping: p,
29-
}
30-
return pathmapper, nil
31-
case c.Framework == dummy:
32-
pathmapper = &dummypathmapper.PathMapper{
33-
Config: c,
34-
Logger: l,
35-
PathMapping: p,
36-
}
21+
if _, exist := pathMapperRegistry[c.Framework]; exist {
22+
pathmapper := pathMapperRegistry[c.Framework]
23+
pathmapper.Initialize(c, l, p)
3724
return pathmapper, nil
38-
case c.Framework == goaop:
39-
return nil, errors.New("Go! AOP Framework support is under development ...")
4025
}
4126

4227
return nil, errors.New("Unsupported framework")

xdebugproxy/xdebugproxy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package xdebugproxy
33
import (
44
"github.com/dfeyer/flow-debugproxy/config"
55
"github.com/dfeyer/flow-debugproxy/logger"
6+
"github.com/dfeyer/flow-debugproxy/pathmapping"
67

78
"fmt"
89
"io"
@@ -13,6 +14,7 @@ const h = "%s"
1314

1415
// XDebugProcessorPlugin process message in xDebug protocol
1516
type XDebugProcessorPlugin interface {
17+
Initialize(c *config.Config, l *logger.Logger, m *pathmapping.PathMapping)
1618
ApplyMappingToTextProtocol(message []byte) []byte
1719
ApplyMappingToXML(message []byte) []byte
1820
}

0 commit comments

Comments
 (0)