Skip to content

Commit

Permalink
Merge pull request #23 from sansecio/magento2-baseurl
Browse files Browse the repository at this point in the history
improve magento2 base url detection
  • Loading branch information
danslo authored Sep 13, 2024
2 parents 27f3ef1 + 0610817 commit a74b1ad
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: '1.20'
cache: false
- name: Lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
only-new-issues: true
only-new-issues: true
2 changes: 1 addition & 1 deletion .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: go test
- run: go test
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/sansecio/gocommerce

go 1.20
go 1.22

require (
github.com/go-sql-driver/mysql v1.5.0 // 20190901 last release is incompatible with older mysql servers
Expand Down
26 changes: 17 additions & 9 deletions magento2.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"

"github.com/sansecio/gocommerce/phpcfg"
)
Expand Down Expand Up @@ -69,13 +71,15 @@ func (m2 *Magento2) ParseConfig(cfgPath string) (*StoreConfig, error) {

func (m2 *Magento2) BaseURLs(docroot string) ([]string, error) {
cfgPath := filepath.Join(docroot, m2.ConfigPath())

urls, err := m2.getBaseURLsFromDatabase(cfgPath)
if err == nil {
return urls, nil
urls := []string{}
if ud, err := m2.getBaseURLsFromDatabase(cfgPath); err == nil {
urls = append(urls, ud...)
}

return m2.getBaseURLsFromConfig(cfgPath)
if uc, err := m2.getBaseURLsFromConfig(cfgPath); err == nil {
urls = append(urls, uc...)
}
slices.Sort(urls)
return slices.Compact(urls), nil
}

func (m2 *Magento2) Version(docroot string) (string, error) {
Expand All @@ -87,20 +91,24 @@ func (m2 *Magento2) Version(docroot string) (string, error) {
return getVersionFromJsonFile(docroot + "/composer.json")
}

func urlIsPlaceholder(url string) bool {
return strings.HasPrefix(url, "{{") || strings.HasSuffix(url, "}}")
}

func (m2 *Magento2) getBaseURLsFromConfig(cfgPath string) ([]string, error) {
cm, err := phpcfg.ParsePath(cfgPath)
if err != nil {
return nil, err
}

r, err := regexp.Compile(`root.system.\w+.web.secure.base_url`)
r, err := regexp.Compile(`root.system.\w+.web.(un)?secure.base_url`)
if err != nil {
return nil, err
}

urls := []string{}
for k, v := range cm {
if r.MatchString(k) {
if r.MatchString(k) && !urlIsPlaceholder(v) {
urls = append(urls, v)
}
}
Expand Down Expand Up @@ -135,7 +143,7 @@ func (m2 *Magento2) getBaseURLsFromDatabase(cfgPath string) ([]string, error) {
urls := []string{}
for rows.Next() {
var url string
if err := rows.Scan(&url); err == nil {
if err := rows.Scan(&url); err == nil && !urlIsPlaceholder(url) {
urls = append(urls, url)
}
}
Expand Down

0 comments on commit a74b1ad

Please sign in to comment.