2017-12-01 13:29:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-12-01 16:04:31 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-12-01 13:29:23 +00:00
|
|
|
"os"
|
2018-01-09 22:03:53 +00:00
|
|
|
"path/filepath"
|
2018-04-10 17:15:15 +00:00
|
|
|
"sync"
|
2017-12-01 13:29:23 +00:00
|
|
|
|
2018-06-25 15:20:16 +00:00
|
|
|
"github.com/karrick/godirwalk"
|
2018-06-21 17:37:34 +00:00
|
|
|
"github.com/pkg/profile"
|
2017-12-01 16:04:31 +00:00
|
|
|
"github.com/yookoala/realpath"
|
2018-04-10 20:59:57 +00:00
|
|
|
"github.com/z7zmey/php-parser/parser"
|
2018-01-26 13:24:56 +00:00
|
|
|
"github.com/z7zmey/php-parser/php5"
|
2018-02-06 10:39:42 +00:00
|
|
|
"github.com/z7zmey/php-parser/php7"
|
2018-02-04 19:35:46 +00:00
|
|
|
"github.com/z7zmey/php-parser/visitor"
|
2017-12-01 16:04:31 +00:00
|
|
|
)
|
2017-12-01 13:29:23 +00:00
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
var usePhp5 *bool
|
2018-06-19 20:55:12 +00:00
|
|
|
var dumpType string
|
2018-06-21 17:37:34 +00:00
|
|
|
var profiler string
|
2018-06-29 21:51:11 +00:00
|
|
|
var withMeta *bool
|
2018-06-19 20:55:12 +00:00
|
|
|
var showResolvedNs *bool
|
2018-02-06 10:39:42 +00:00
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
func main() {
|
2018-06-19 20:55:12 +00:00
|
|
|
usePhp5 = flag.Bool("php5", false, "parse as PHP5")
|
2018-06-29 21:51:11 +00:00
|
|
|
withMeta = flag.Bool("meta", false, "show meta")
|
2018-06-19 20:55:12 +00:00
|
|
|
showResolvedNs = flag.Bool("r", false, "resolve names")
|
|
|
|
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
|
2018-06-21 17:37:34 +00:00
|
|
|
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem]")
|
2018-06-19 20:55:12 +00:00
|
|
|
|
2017-12-01 16:04:31 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-06-21 17:37:34 +00:00
|
|
|
switch profiler {
|
|
|
|
case "cpu":
|
|
|
|
defer profile.Start(profile.ProfilePath("."), profile.NoShutdownHook).Stop()
|
|
|
|
case "mem":
|
|
|
|
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
pathCh := make(chan string)
|
2018-04-10 20:59:57 +00:00
|
|
|
resultCh := make(chan parser.Parser)
|
2018-04-10 17:15:15 +00:00
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
// run 4 concurrent parserWorkers
|
2018-04-10 17:15:15 +00:00
|
|
|
for i := 0; i < 4; i++ {
|
2018-04-10 20:59:57 +00:00
|
|
|
go parserWorker(pathCh, resultCh)
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run printer goroutine
|
|
|
|
go printer(resultCh)
|
|
|
|
|
|
|
|
// process files
|
|
|
|
processPath(flag.Args(), pathCh)
|
|
|
|
|
|
|
|
// wait the all files done
|
|
|
|
wg.Wait()
|
|
|
|
close(pathCh)
|
|
|
|
close(resultCh)
|
|
|
|
}
|
|
|
|
|
|
|
|
func processPath(pathList []string, pathCh chan<- string) {
|
|
|
|
for _, path := range pathList {
|
2017-12-01 16:04:31 +00:00
|
|
|
real, err := realpath.Realpath(path)
|
|
|
|
checkErr(err)
|
|
|
|
|
2018-06-25 15:20:16 +00:00
|
|
|
s, err := os.Stat(real)
|
2018-01-09 22:03:53 +00:00
|
|
|
checkErr(err)
|
2018-06-25 15:20:16 +00:00
|
|
|
|
|
|
|
if !s.IsDir() {
|
|
|
|
wg.Add(1)
|
|
|
|
pathCh <- real
|
|
|
|
} else {
|
|
|
|
godirwalk.Walk(real, &godirwalk.Options{
|
|
|
|
Unsorted: true,
|
|
|
|
Callback: func(osPathname string, de *godirwalk.Dirent) error {
|
|
|
|
if !de.IsDir() && filepath.Ext(osPathname) == ".php" {
|
|
|
|
wg.Add(1)
|
|
|
|
pathCh <- osPathname
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
ErrorCallback: func(osPathname string, err error) godirwalk.ErrorAction {
|
|
|
|
return godirwalk.SkipNode
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2017-12-01 14:04:53 +00:00
|
|
|
}
|
2017-12-01 16:04:31 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
func parserWorker(pathCh <-chan string, result chan<- parser.Parser) {
|
|
|
|
var parserWorker parser.Parser
|
2018-04-10 17:15:15 +00:00
|
|
|
|
|
|
|
for {
|
2018-06-06 16:47:28 +00:00
|
|
|
path, ok := <-pathCh
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:14:36 +00:00
|
|
|
src, err := os.Open(path)
|
|
|
|
checkErr(err)
|
2018-04-10 17:15:15 +00:00
|
|
|
|
|
|
|
if *usePhp5 {
|
2018-04-10 20:59:57 +00:00
|
|
|
parserWorker = php5.NewParser(src, path)
|
2018-04-10 17:15:15 +00:00
|
|
|
} else {
|
2018-04-10 20:59:57 +00:00
|
|
|
parserWorker = php7.NewParser(src, path)
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 21:51:11 +00:00
|
|
|
if *withMeta {
|
|
|
|
parserWorker.WithMeta()
|
|
|
|
}
|
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
parserWorker.Parse()
|
2018-06-25 17:14:36 +00:00
|
|
|
|
|
|
|
src.Close()
|
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
result <- parserWorker
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
func printer(result <-chan parser.Parser) {
|
2018-06-21 22:00:02 +00:00
|
|
|
var counter int
|
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
for {
|
2018-06-06 16:47:28 +00:00
|
|
|
parserWorker, ok := <-result
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:00:02 +00:00
|
|
|
counter++
|
|
|
|
|
|
|
|
fmt.Printf("==> [%d] %s\n", counter, parserWorker.GetPath())
|
2018-04-10 17:15:15 +00:00
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
for _, e := range parserWorker.GetErrors() {
|
2018-04-10 17:15:15 +00:00
|
|
|
fmt.Println(e)
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:55:12 +00:00
|
|
|
var nsResolver *visitor.NamespaceResolver
|
|
|
|
if *showResolvedNs {
|
|
|
|
nsResolver = visitor.NewNamespaceResolver()
|
2018-06-05 09:22:36 +00:00
|
|
|
parserWorker.GetRootNode().Walk(nsResolver)
|
2018-06-19 20:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch dumpType {
|
|
|
|
case "custom":
|
2018-06-18 20:29:52 +00:00
|
|
|
dumper := &visitor.Dumper{
|
2018-06-05 09:22:36 +00:00
|
|
|
Writer: os.Stdout,
|
2018-06-19 20:55:12 +00:00
|
|
|
Indent: "| ",
|
|
|
|
NsResolver: nsResolver,
|
|
|
|
}
|
|
|
|
parserWorker.GetRootNode().Walk(dumper)
|
|
|
|
case "json":
|
|
|
|
dumper := &visitor.JsonDumper{
|
|
|
|
Writer: os.Stdout,
|
|
|
|
NsResolver: nsResolver,
|
|
|
|
}
|
|
|
|
parserWorker.GetRootNode().Walk(dumper)
|
|
|
|
case "pretty_json":
|
|
|
|
dumper := &visitor.PrettyJsonDumper{
|
|
|
|
Writer: os.Stdout,
|
2018-06-05 09:22:36 +00:00
|
|
|
NsResolver: nsResolver,
|
|
|
|
}
|
|
|
|
parserWorker.GetRootNode().Walk(dumper)
|
2018-06-19 20:55:12 +00:00
|
|
|
case "go":
|
|
|
|
dumper := &visitor.GoDumper{Writer: os.Stdout}
|
|
|
|
parserWorker.GetRootNode().Walk(dumper)
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
2018-06-19 20:55:12 +00:00
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 16:04:31 +00:00
|
|
|
func checkErr(err error) {
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
2017-12-01 14:04:53 +00:00
|
|
|
}
|
|
|
|
}
|