2017-12-01 13:29:23 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-07-11 15:59:04 +00:00
|
|
|
"bytes"
|
2017-12-01 16:04:31 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-07-11 15:59:04 +00:00
|
|
|
"io/ioutil"
|
2017-12-01 16:04:31 +00:00
|
|
|
"log"
|
2017-12-01 13:29:23 +00:00
|
|
|
"os"
|
2018-01-09 22:03:53 +00:00
|
|
|
"path/filepath"
|
2018-07-11 15:59:04 +00:00
|
|
|
"runtime"
|
2018-04-10 17:15:15 +00:00
|
|
|
"sync"
|
2017-12-01 13:29:23 +00:00
|
|
|
|
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-07-29 08:44:38 +00:00
|
|
|
"github.com/z7zmey/php-parser/printer"
|
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
|
2019-12-26 15:57:56 +00:00
|
|
|
var phpVersion string
|
2018-06-19 20:55:12 +00:00
|
|
|
var dumpType string
|
2018-06-21 17:37:34 +00:00
|
|
|
var profiler string
|
2019-02-25 14:52:47 +00:00
|
|
|
var withFreeFloating *bool
|
2018-06-19 20:55:12 +00:00
|
|
|
var showResolvedNs *bool
|
2018-07-29 08:44:38 +00:00
|
|
|
var printBack *bool
|
2020-03-09 10:44:10 +00:00
|
|
|
var printPath *bool
|
2018-02-06 10:39:42 +00:00
|
|
|
|
2018-07-11 15:59:04 +00:00
|
|
|
type file struct {
|
|
|
|
path string
|
|
|
|
content []byte
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
type result struct {
|
|
|
|
path string
|
|
|
|
parser parser.Parser
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
func main() {
|
2019-02-25 14:52:47 +00:00
|
|
|
withFreeFloating = flag.Bool("ff", false, "parse and show free floating strings")
|
2018-06-19 20:55:12 +00:00
|
|
|
showResolvedNs = flag.Bool("r", false, "resolve names")
|
2018-07-29 08:44:38 +00:00
|
|
|
printBack = flag.Bool("pb", false, "print AST back into the parsed file")
|
2020-03-09 10:44:10 +00:00
|
|
|
printPath = flag.Bool("p", false, "print filepath")
|
2018-06-19 20:55:12 +00:00
|
|
|
flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]")
|
2018-07-11 14:01:31 +00:00
|
|
|
flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem, trace]")
|
2019-12-26 15:57:56 +00:00
|
|
|
flag.StringVar(&phpVersion, "phpver", "7.4", "php version")
|
2018-06-19 20:55:12 +00:00
|
|
|
|
2017-12-01 16:04:31 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
if len(flag.Args()) == 0 {
|
2019-03-06 20:28:15 +00:00
|
|
|
flag.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-07-11 14:01:31 +00:00
|
|
|
case "trace":
|
|
|
|
defer profile.Start(profile.TraceProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop()
|
2018-06-21 17:37:34 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
numCpu := runtime.GOMAXPROCS(0)
|
2018-07-11 15:59:04 +00:00
|
|
|
|
|
|
|
fileCh := make(chan *file, numCpu)
|
2019-03-10 21:37:01 +00:00
|
|
|
resultCh := make(chan result, numCpu)
|
2018-04-10 17:15:15 +00:00
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
// run 4 concurrent parserWorkers
|
2018-07-11 15:59:04 +00:00
|
|
|
for i := 0; i < numCpu; i++ {
|
|
|
|
go parserWorker(fileCh, resultCh)
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run printer goroutine
|
2018-07-29 08:44:38 +00:00
|
|
|
go printerWorker(resultCh)
|
2018-04-10 17:15:15 +00:00
|
|
|
|
|
|
|
// process files
|
2018-07-11 15:59:04 +00:00
|
|
|
processPath(flag.Args(), fileCh)
|
2018-04-10 17:15:15 +00:00
|
|
|
|
|
|
|
// wait the all files done
|
|
|
|
wg.Wait()
|
2018-07-11 15:59:04 +00:00
|
|
|
close(fileCh)
|
2018-04-10 17:15:15 +00:00
|
|
|
close(resultCh)
|
|
|
|
}
|
|
|
|
|
2018-07-11 15:59:04 +00:00
|
|
|
func processPath(pathList []string, fileCh chan<- *file) {
|
2018-04-10 17:15:15 +00:00
|
|
|
for _, path := range pathList {
|
2017-12-01 16:04:31 +00:00
|
|
|
real, err := realpath.Realpath(path)
|
|
|
|
checkErr(err)
|
|
|
|
|
2019-02-25 14:11:51 +00:00
|
|
|
err = filepath.Walk(real, func(path string, f os.FileInfo, err error) error {
|
|
|
|
if !f.IsDir() && filepath.Ext(path) == ".php" {
|
|
|
|
wg.Add(1)
|
|
|
|
content, err := ioutil.ReadFile(path)
|
|
|
|
checkErr(err)
|
|
|
|
fileCh <- &file{path, content}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2018-01-09 22:03:53 +00:00
|
|
|
checkErr(err)
|
2017-12-01 14:04:53 +00:00
|
|
|
}
|
2017-12-01 16:04:31 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
func parserWorker(fileCh <-chan *file, r chan<- result) {
|
2018-04-10 17:15:15 +00:00
|
|
|
for {
|
2018-07-11 15:59:04 +00:00
|
|
|
f, ok := <-fileCh
|
2018-06-06 16:47:28 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-26 15:57:56 +00:00
|
|
|
parserWorker, err := parser.NewParser(f.content, phpVersion)
|
|
|
|
if err != nil {
|
|
|
|
panic(err.Error())
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 14:52:47 +00:00
|
|
|
if *withFreeFloating {
|
|
|
|
parserWorker.WithFreeFloating()
|
2018-06-29 21:51:11 +00:00
|
|
|
}
|
|
|
|
|
2018-04-10 20:59:57 +00:00
|
|
|
parserWorker.Parse()
|
2018-06-25 17:14:36 +00:00
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
r <- result{path: f.path, parser: parserWorker}
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
func printerWorker(r <-chan result) {
|
2018-06-21 22:00:02 +00:00
|
|
|
var counter int
|
|
|
|
|
2018-04-10 17:15:15 +00:00
|
|
|
for {
|
2019-03-10 21:37:01 +00:00
|
|
|
res, ok := <-r
|
2018-06-06 16:47:28 +00:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:00:02 +00:00
|
|
|
counter++
|
|
|
|
|
2020-03-09 10:44:10 +00:00
|
|
|
if *printPath {
|
|
|
|
fmt.Fprintf(os.Stdout, "==> [%d] %s\n", counter, res.path)
|
|
|
|
}
|
2018-04-10 17:15:15 +00:00
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
for _, e := range res.parser.GetErrors() {
|
2020-03-09 10:44:10 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "==> %s\n", e)
|
2018-04-10 17:15:15 +00:00
|
|
|
}
|
|
|
|
|
2018-07-29 08:44:38 +00:00
|
|
|
if *printBack {
|
|
|
|
o := bytes.NewBuffer([]byte{})
|
|
|
|
p := printer.NewPrinter(o)
|
2019-03-10 21:37:01 +00:00
|
|
|
p.Print(res.parser.GetRootNode())
|
2018-07-29 08:44:38 +00:00
|
|
|
|
2019-03-10 21:37:01 +00:00
|
|
|
err := ioutil.WriteFile(res.path, o.Bytes(), 0644)
|
2018-07-29 08:44:38 +00:00
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
2018-06-19 20:55:12 +00:00
|
|
|
var nsResolver *visitor.NamespaceResolver
|
|
|
|
if *showResolvedNs {
|
|
|
|
nsResolver = visitor.NewNamespaceResolver()
|
2019-03-10 21:37:01 +00:00
|
|
|
res.parser.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,
|
|
|
|
}
|
2019-03-10 21:37:01 +00:00
|
|
|
res.parser.GetRootNode().Walk(dumper)
|
2018-06-19 20:55:12 +00:00
|
|
|
case "json":
|
|
|
|
dumper := &visitor.JsonDumper{
|
|
|
|
Writer: os.Stdout,
|
|
|
|
NsResolver: nsResolver,
|
|
|
|
}
|
2019-03-10 21:37:01 +00:00
|
|
|
res.parser.GetRootNode().Walk(dumper)
|
2018-06-19 20:55:12 +00:00
|
|
|
case "pretty_json":
|
|
|
|
dumper := &visitor.PrettyJsonDumper{
|
|
|
|
Writer: os.Stdout,
|
2018-06-05 09:22:36 +00:00
|
|
|
NsResolver: nsResolver,
|
|
|
|
}
|
2019-03-10 21:37:01 +00:00
|
|
|
res.parser.GetRootNode().Walk(dumper)
|
2018-06-19 20:55:12 +00:00
|
|
|
case "go":
|
|
|
|
dumper := &visitor.GoDumper{Writer: os.Stdout}
|
2019-03-10 21:37:01 +00:00
|
|
|
res.parser.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
|
|
|
}
|
|
|
|
}
|