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
1819const (
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
3239type 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
7589func (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
114128func (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
127141func (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
139153func (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 }
0 commit comments