package gotsrpc import ( "github.com/foomo/gotsrpc/config" "strings" ) func renderPHPRPCServiceClients(service *Service, namespce string, g *code) error { g.l(` ['method' => 'POST', 'timeout' => 5, 'header' => 'Content-type: application/json']];`) g.nl() g.l(`private $options;`) g.l(`private $endpoint;`) g.nl() // Constructor g.l(`public function __constructor($endpoint, $options=null)`) g.l(`{`) g.ind(1) g.l(`$this->endpoint = $endpoint;`) g.l(`$this->options = (is_null($options)) ? self::$defaultOptions : $options;`) g.ind(-1) g.l(`}`) g.nl() // Service methods for _, method := range service.Methods { args := []string{} params := []string{} for _, a := range method.Args { args = append(args, "$"+a.Name) params = append(params, "$"+a.Name) } //rets := []string{} //returns := []string{} //for i, r := range method.Return { // name := r.Name // if len(name) == 0 { // name = fmt.Sprintf("ret%s_%d", method.Name, i) // } // rets = append(rets, "&"+name) // returns = append(returns, name+" "+r.Value.goType(aliases, fullPackageName)) //} //returns = append(returns, "clientErr error") //g.l(`func (c *` + clientName + `) ` + method.Name + `(` + strings.Join(params, ", ") + `) (` + strings.Join(returns, ", ") + `) {`) //g.l(`args := []interface{}{` + strings.Join(args, ", ") + `}`) //g.l(`reply := []interface{}{` + strings.Join(rets, ", ") + `}`) //g.l(`clientErr = gotsrpc.CallClient(c.URL, c.EndPoint, "` + method.Name + `", args, reply)`) //g.l(`return`) //g.l(`}`) //g.nl() g.l(`public function ` + lcfirst(method.Name) + `(` + strings.Join(params, ", ") + `)`) g.l(`{`) g.ind(1) g.l(`return $this->call('` + method.Name + `', [` + strings.Join(params, ", ") + `]);`) g.ind(-1) g.l(`}`) g.nl() } // Protected methods g.l(`protected function call($method, array $request)`) g.l(`{`) g.ind(1) g.l(`$options = $this->options;`) g.l(`$options['http']['content'] = json_encode($request);`) g.l(`$options['http']['content'] = json_encode($request);`) g.l(`return json_decode(file_get_contents($this->endpoint . '/' . $method, false, stream_context_create($options)));`) g.ind(-1) g.l(`}`) g.nl() g.ind(-1) g.l(`}`) g.nl() return nil } func RenderPHPRPCClients(services map[string]*Service, config *config.Target) (code map[string]string, err error) { code = map[string]string{} for _, service := range services { // Check if we should render this service as ts rcp // Note: remove once there's a separate gorcp generator if !config.IsPHPRPC(service.Name) { continue } target := config.GetPHPTarget(service.Name) g := newCode(" ") if err = renderPHPRPCServiceClients(service, target.Namespace, g); err != nil { return } code[target.Out+"/"+service.Name+"Client.php"] = g.string() } return }