2018-02-23 18:26:13 +00:00
|
|
|
// Package visitor contains walker.visitor implementations
|
|
|
|
package visitor
|
|
|
|
|
|
|
|
import (
|
2018-02-23 21:59:26 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2018-02-23 18:26:13 +00:00
|
|
|
"github.com/z7zmey/php-parser/node"
|
|
|
|
"github.com/z7zmey/php-parser/node/name"
|
|
|
|
"github.com/z7zmey/php-parser/node/stmt"
|
|
|
|
"github.com/z7zmey/php-parser/walker"
|
|
|
|
)
|
|
|
|
|
2018-02-23 21:59:26 +00:00
|
|
|
type Namespace struct {
|
|
|
|
Namespace string
|
|
|
|
Aliases map[string]map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewNamespace(NSName string) *Namespace {
|
|
|
|
return &Namespace{
|
|
|
|
Namespace: "",
|
|
|
|
Aliases: map[string]map[string]string{
|
|
|
|
"": {},
|
|
|
|
"const": {},
|
|
|
|
"function": {},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ns *Namespace) AddAlias(aliasType string, aliasName string, alias string) {
|
|
|
|
aliasType = strings.ToLower(aliasType)
|
|
|
|
|
|
|
|
if aliasType == "const" {
|
|
|
|
ns.Aliases[aliasType][alias] = aliasName
|
|
|
|
} else {
|
|
|
|
ns.Aliases[aliasType][strings.ToLower(alias)] = aliasName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:26:13 +00:00
|
|
|
type NsResolver struct {
|
2018-02-23 21:59:26 +00:00
|
|
|
Namespace *Namespace
|
2018-02-23 18:26:13 +00:00
|
|
|
ResolvedNames map[node.Node]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNsResolver NsResolver type constructor
|
|
|
|
func NewNsResolver() *NsResolver {
|
|
|
|
return &NsResolver{
|
2018-02-23 21:59:26 +00:00
|
|
|
Namespace: NewNamespace(""),
|
2018-02-23 18:26:13 +00:00
|
|
|
ResolvedNames: map[node.Node]string{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnterNode is invoked at every node in heirerchy
|
|
|
|
func (nsr *NsResolver) EnterNode(w walker.Walkable) bool {
|
|
|
|
switch n := w.(type) {
|
|
|
|
case *stmt.Namespace:
|
2018-02-23 21:59:26 +00:00
|
|
|
if n.NamespaceName == nil {
|
|
|
|
nsr.Namespace = NewNamespace("")
|
|
|
|
} else {
|
|
|
|
NSParts := n.NamespaceName.(*name.Name).Parts
|
|
|
|
nsr.Namespace = NewNamespace(concatNameParts(NSParts))
|
|
|
|
}
|
|
|
|
case *stmt.UseList:
|
|
|
|
useType := ""
|
|
|
|
if n.UseType != nil {
|
|
|
|
useType = n.UseType.(*node.Identifier).Value
|
|
|
|
}
|
|
|
|
for _, nn := range n.Uses {
|
2018-02-23 22:11:53 +00:00
|
|
|
nsr.AddAlias(useType, nn, nil)
|
2018-02-23 21:59:26 +00:00
|
|
|
}
|
|
|
|
case *stmt.GroupUse:
|
|
|
|
useType := ""
|
|
|
|
if n.UseType != nil {
|
|
|
|
useType = n.UseType.(*node.Identifier).Value
|
|
|
|
}
|
|
|
|
for _, nn := range n.UseList {
|
2018-02-23 22:11:53 +00:00
|
|
|
nsr.AddAlias(useType, nn, n.Prefix.(*name.Name).Parts)
|
2018-02-23 21:59:26 +00:00
|
|
|
}
|
2018-02-23 18:26:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-02-23 22:11:53 +00:00
|
|
|
func (nsr *NsResolver) AddAlias(useType string, nn node.Node, prefix []node.Node) {
|
|
|
|
switch use := nn.(type) {
|
|
|
|
case *stmt.Use:
|
|
|
|
if use.UseType != nil {
|
|
|
|
useType = use.UseType.(*node.Identifier).Value
|
|
|
|
}
|
|
|
|
|
|
|
|
useNameParts := use.Use.(*name.Name).Parts
|
|
|
|
var alias string
|
|
|
|
if use.Alias == nil {
|
|
|
|
alias = useNameParts[len(useNameParts)-1].(*name.NamePart).Value
|
|
|
|
} else {
|
|
|
|
alias = use.Alias.(*node.Identifier).Value
|
|
|
|
}
|
|
|
|
|
|
|
|
nsr.Namespace.AddAlias(useType, concatNameParts(prefix, useNameParts), alias)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 18:26:13 +00:00
|
|
|
// GetChildrenVisitor is invoked at every node parameter that contains children nodes
|
|
|
|
func (nsr *NsResolver) GetChildrenVisitor(key string) walker.Visitor {
|
|
|
|
return nsr
|
|
|
|
}
|
|
|
|
|
|
|
|
// LeaveNode is invoked after node process
|
|
|
|
func (nsr *NsResolver) LeaveNode(w walker.Walkable) {
|
|
|
|
switch n := w.(type) {
|
|
|
|
case *stmt.Namespace:
|
|
|
|
if n.Stmts != nil {
|
2018-02-23 21:59:26 +00:00
|
|
|
fmt.Printf("%+v \n", nsr.Namespace.Aliases)
|
|
|
|
nsr.Namespace = NewNamespace("")
|
2018-02-23 18:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-23 21:59:26 +00:00
|
|
|
func concatNameParts(parts ...[]node.Node) string {
|
2018-02-23 18:26:13 +00:00
|
|
|
str := ""
|
|
|
|
|
2018-02-23 21:59:26 +00:00
|
|
|
for _, p := range parts {
|
|
|
|
for _, n := range p {
|
|
|
|
if str == "" {
|
|
|
|
str = n.(*name.NamePart).Value
|
|
|
|
} else {
|
|
|
|
str = str + "\\" + n.(*name.NamePart).Value
|
|
|
|
}
|
2018-02-23 18:26:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|