|
| 1 | +package gotask |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "github.com/pkg/errors" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "text/template" |
| 10 | + "unicode" |
| 11 | +) |
| 12 | + |
| 13 | +const phpBody = `<?php |
| 14 | +
|
| 15 | +declare(strict_types=1); |
| 16 | +{{ $ns := .Namespace -}} |
| 17 | +{{if $ns}} |
| 18 | +namespace {{ $ns }}; |
| 19 | +{{end}} |
| 20 | +use Reasno\GoTask\GoTask; |
| 21 | +use Reasno\GoTask\GoTaskProxy; |
| 22 | +{{ $class := .Class.Name }} |
| 23 | +class {{ $class }} extends GoTaskProxy |
| 24 | +{ |
| 25 | +{{- range $m := .Class.Functions}} |
| 26 | + /** |
| 27 | + * @param {{ $m.ParamModifier }} $payload |
| 28 | + * @return {{ $m.ResultModifier | returnAnnotation }} |
| 29 | + */ |
| 30 | + public function {{ $m.Name | lcfirst }}({{ $m.ParamModifier | postSpace }}$payload){{ $m.ResultModifier | returnTypeHint }} |
| 31 | + { |
| 32 | + return parent::call({{ methodName $class $m.Name }}, $payload, {{ $m.Raw | flag}}); |
| 33 | + } |
| 34 | +{{end -}} |
| 35 | +}` |
| 36 | + |
| 37 | +var tpl *template.Template |
| 38 | + |
| 39 | +func init() { |
| 40 | + tpl = template.Must(template.New("phpBody").Funcs(template.FuncMap{ |
| 41 | + "flag": func(raw *bool) string { |
| 42 | + if *raw { |
| 43 | + return "GoTask::PAYLOAD_RAW" |
| 44 | + } |
| 45 | + return "0" |
| 46 | + }, |
| 47 | + "returnAnnotation": func(modifier *string) string { |
| 48 | + if *modifier != "" { |
| 49 | + return *modifier |
| 50 | + } |
| 51 | + return "mixed" |
| 52 | + }, |
| 53 | + "returnTypeHint": func(modifier *string) string { |
| 54 | + if *modifier != "" { |
| 55 | + return " : " + *modifier |
| 56 | + } |
| 57 | + return "" |
| 58 | + }, |
| 59 | + "postSpace": func(name *string) string { |
| 60 | + if *name == "" { |
| 61 | + return "" |
| 62 | + } |
| 63 | + return *name + " " |
| 64 | + }, |
| 65 | + "lcfirst": func(name *string) string { |
| 66 | + for _, v := range *name { |
| 67 | + u := string(unicode.ToLower(v)) |
| 68 | + return u + (*name)[len(u):] |
| 69 | + } |
| 70 | + return "" |
| 71 | + }, |
| 72 | + "methodName": func(class string, method string) string { |
| 73 | + return "\"" + class + "." + method + "\"" |
| 74 | + }, |
| 75 | + }).Parse(phpBody)) |
| 76 | +} |
| 77 | + |
| 78 | +// generate php file body |
| 79 | +func body(namespace *string, class *Class) string { |
| 80 | + out := bytes.NewBuffer(nil) |
| 81 | + |
| 82 | + data := struct { |
| 83 | + Namespace string |
| 84 | + Class Class |
| 85 | + }{ |
| 86 | + Namespace: *namespace, |
| 87 | + Class: *class, |
| 88 | + } |
| 89 | + |
| 90 | + err := tpl.Execute(out, data) |
| 91 | + if err != nil { |
| 92 | + panic(err) |
| 93 | + } |
| 94 | + |
| 95 | + return out.String() |
| 96 | +} |
| 97 | + |
| 98 | +func generatePHP(receiver interface{}) error { |
| 99 | + namespace := property(receiver, "Namespace", "App\\GoTask") |
| 100 | + class := reflectStruct(receiver) |
| 101 | + dirPath := property(receiver, "Path", "./../app/GoTask") |
| 102 | + err := os.MkdirAll(dirPath, os.FileMode(0755)) |
| 103 | + if err != nil { |
| 104 | + return errors.Wrap(err, "cannot create dir for php files") |
| 105 | + } |
| 106 | + fullPath, err := filepath.Abs(filepath.Clean(dirPath) + "/" + class.Name + ".php") |
| 107 | + if err != nil { |
| 108 | + return errors.Wrap(err, "invalid file path") |
| 109 | + } |
| 110 | + out := body(&namespace, class) |
| 111 | + err = ioutil.WriteFile(fullPath, []byte(out), os.FileMode(0755)) |
| 112 | + if err != nil { |
| 113 | + return errors.Wrap(err, "failed to generate PHP file") |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
0 commit comments