From 12dd444b5ccae03d11cbe7473085cffc4743c187 Mon Sep 17 00:00:00 2001 From: Dominique Feyer Date: Tue, 29 Dec 2015 19:57:51 +0100 Subject: [PATCH] TASK: Add support for PSR4 Flow package Fixes #3 --- flowpathmapper/flowpathmapper.go | 29 ++++++++++++++++++++------- flowpathmapper/flowpathmapper_test.go | 18 +++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 flowpathmapper/flowpathmapper_test.go diff --git a/flowpathmapper/flowpathmapper.go b/flowpathmapper/flowpathmapper.go index 3a96ceb..c38da28 100644 --- a/flowpathmapper/flowpathmapper.go +++ b/flowpathmapper/flowpathmapper.go @@ -30,7 +30,7 @@ var ( regexpPhpFile = regexp.MustCompile(`(?://)?(/[^ ]*\.php)`) regexpFilename = regexp.MustCompile(`filename=["]?file://(\S+)/Data/Temporary/[^/]*/Cache/Code/Flow_Object_Classes/([^"]*)\.php`) regexpPathAndFilename = regexp.MustCompile(`(?m)^# PathAndFilename: (.*)$`) - regexpPackageClass = regexp.MustCompile(`(.*?)/Packages/(.*?)/Classes/(.*).php`) + regexpPackageClass = regexp.MustCompile(`(.*?)/Packages/[^/]*/(.*?)/Classes/(.*).php`) regexpDot = regexp.MustCompile(`[\./]`) ) @@ -172,21 +172,36 @@ func (p *PathMapper) readOriginalPathFromCache(path string) string { } func (p *PathMapper) buildClassNameFromPath(path string) (string, string) { + basePath, className := pathToClassPath(path) + if className == "" { + // Other (vendor) packages, todo add support for vendor package with Flow proxy class + p.logger.Warn(h, "Vendor package detected") + p.logger.Warn("Class mapping not supported currently for path: %s, \n", path) + } + return basePath, className +} + +// Convert absolute path to class path (internal use only) +func pathToClassPath(path string) (string, string) { var ( basePath string - className string + classPath string ) match := regexpPackageClass.FindStringSubmatch(path) if len(match) == 4 { // Flow standard packages + packagePath := regexpDot.ReplaceAllString(match[2], "/") + classPath = match[3] + if strings.Contains(classPath, packagePath) == false { + // Quick'n dirty PSR4 support + classPath = packagePath + "/" + classPath + } basePath = match[1] - className = regexpDot.ReplaceAllString(match[3], "_") + classPath = regexpDot.ReplaceAllString(classPath, "_") } else { // Other (vendor) packages, todo add support for vendor package with Flow proxy class - p.logger.Warn(h, "Vendor package detected") - p.logger.Warn("Class mapping not supported currently for path: %s, \n", path) basePath = path - className = "" + classPath = "" } - return basePath, className + return basePath, classPath } diff --git a/flowpathmapper/flowpathmapper_test.go b/flowpathmapper/flowpathmapper_test.go new file mode 100644 index 0000000..334c069 --- /dev/null +++ b/flowpathmapper/flowpathmapper_test.go @@ -0,0 +1,18 @@ +package flowpathmapper + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestBuildClassNameFromPathSupportPSR2(t *testing.T) { + basePath, className := pathToClassPath("/your/path/sites/dev/master-dev.neos-workplace.dev/Packages/Application/Ttree.FlowDebugProxyHelper/Classes/Ttree/FlowDebugProxyHelper/ProxyClassMapperComponent.php") + assert.Equal(t, "/your/path/sites/dev/master-dev.neos-workplace.dev", basePath, "they should be equal") + assert.Equal(t, "Ttree_FlowDebugProxyHelper_ProxyClassMapperComponent", className, "they should be equal") +} + +func TestBuildClassNameFromPathSupportPSR4(t *testing.T) { + basePath, className := pathToClassPath("/your/path/sites/dev/master-dev.neos-workplace.dev/Packages/Application/Ttree.FlowDebugProxyHelper/Classes/ProxyClassMapperComponent.php") + assert.Equal(t, "/your/path/sites/dev/master-dev.neos-workplace.dev", basePath, "they should be equal") + assert.Equal(t, "Ttree_FlowDebugProxyHelper_ProxyClassMapperComponent", className, "they should be equal") +}