Ejemplo n.º 1
0
 /**
  * Create a Zend soap_server object
  * 
  * @param PhpWsdl $server The PhpWsdl object
  * @param array $data Hook data (default: NULL)
  * @return Zend_Soap_Server The Zend server object
  */
 public static function CreateServer($server, $data = null)
 {
     if (!is_null(self::$Server)) {
         return self::$Server;
     }
     self::$Server = new Zend_Soap_Server(!is_null($server->WsdlUri) ? $server->WsdlUri : $server->EndPoint . '?WSDL', array_merge(self::$Options, array('actor' => $server->EndPoint, 'wsdl' => $server->CreateWsdl(), 'uri' => $server->NameSpace)));
     if (!is_null($data)) {
         if (PhpWsdl::CallHook('ZendConfigHook', $data)) {
             if (is_object($data['class'])) {
                 $this->Server->setObject($data['class']);
             } else {
                 $this->Server->setClass($data['class']);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Output the JavaScript JSON client source for this webservice
  * 
  * @param boolean $withHeaders Send JavaScript headers? (default: TRUE)
  * @param boolean $echo Print source (default: TRUE)
  * @param boolean $cache Cache the result (default: TRUE);
  * @return string JavaScript source
  */
 public function OutputJs($withHeaders = true, $echo = true, $cache = true)
 {
     PhpWsdl::Debug('Output JavaScript');
     if (sizeof($this->Server->Methods) < 1) {
         $this->Server->CreateWsdl();
     }
     if ($withHeaders) {
         $this->JavaScriptHeaders();
     }
     if (is_null($this->JsonJs)) {
         $res = array();
         $data = array('server' => $this, 'res' => &$data, 'withheaders' => &$withHeaders, 'echo' => &$echo, 'cache' => &$cache);
         if (PhpWsdl::CallHook('ServersBeginOutputJsHook', $data)) {
             $res[] = 'var ' . $this->Server->Name . 'JsonClient=function(){';
             $res[] = '	this._EndPoint="' . $this->Server->EndPoint . '";';
             $res[] = '	this._Call=function(method,param,cb,cbData){';
             $res[] = '		var server=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");';
             $res[] = '		server.open("POST",this._EndPoint,cb!=null);';
             $res[] = '		server.setRequestHeader("Content-Type","application/x-www-form-urlencoded");';
             $res[] = '		var req="json="+encodeURIComponent(JSON.stringify({call:method,param:(typeof(param)!="undefined")?param:[]}));';
             $res[] = '		if(cb){';
             $res[] = '			server.onreadystatechange=this._EndCall;';
             $res[] = '			server.cb=cb;';
             $res[] = '			server.cbData=(typeof(cbData)!="undefined")?cbData:null;';
             $res[] = '			server.send(req);';
             $res[] = '			return server;';
             $res[] = '		}else{';
             $res[] = '			server.send(req);';
             $res[] = '			var res=JSON.parse(server.responseText);';
             $res[] = '			delete(server);';
             $res[] = '			return res;';
             $res[] = '		}';
             $res[] = '	};';
             $res[] = '	this._EndCall=function(e){';
             $res[] = '		var server=e.currentTarget;';
             $res[] = '		if(server.readyState!=4)';
             $res[] = '			return;';
             $res[] = '		if(server.status!=200)';
             $res[] = '			throw(new Exception("AJAX error "+server.status+": "+server.statusText));';
             $res[] = '		if(server.cb)';
             $res[] = '			server.cb(JSON.parse(server.responseText),server.cbData);';
             $res[] = '		server.cb=null;';
             $res[] = '		server.cbData=null;';
             $res[] = '		delete(server);';
             $res[] = '	};';
         }
         $i = -1;
         $len = sizeof($this->Server->Methods);
         while (++$i < $len) {
             $m = $this->Server->Methods[$i];
             if (PhpWsdl::CallHook('ServersOutputMethodJsHook', array_merge($data, array('method' => &$m)))) {
                 $temp = array();
                 $j = -1;
                 $pLen = sizeof($m->Param);
                 while (++$j < $pLen) {
                     $temp[] = $m->Param[$j]->Name;
                 }
                 $temp = implode(',', $temp);
                 $res[] = '	this.' . $m->Name . '=function(' . $temp . ($temp != '' ? ',' : '') . '_cb,_cbData){';
                 $res[] = '		return this._Call("' . $m->Name . '",[' . $temp . '],(typeof(_cb)!="undefined")?_cb:null,(typeof(_cbData)!="undefined")?_cbData:null);';
                 $res[] = '	};';
             }
         }
         $i = -1;
         $len = sizeof($this->Server->Types);
         while (++$i < $len) {
             $t = $this->Server->Types[$i];
             if ($t->IsArray) {
                 continue;
             }
             if (PhpWsdl::CallHook('ServersOutputTypeJsHook', array_merge($data, array('type' => &$t)))) {
                 $eLen = sizeof($t->Elements);
                 $j = -1;
                 $class = get_class($t);
                 switch ($class) {
                     case 'PhpWsdlComplex':
                         $temp = array();
                         if (!is_null($t->Inherit)) {
                             $it = $this->Server->GetType($t->Inherit);
                             $j = -1;
                             $tLen = sizeof($it->Elements);
                             while (++$j < $tLen) {
                                 $temp[] = $it->Elements[$j]->Name;
                             }
                         } else {
                             $tLen = 0;
                         }
                         $j = -1;
                         while (++$j < $eLen) {
                             $temp[] = $t->Elements[$j]->Name;
                         }
                         $tLen += $eLen;
                         $res[] = '	this.' . $t->Name . '=function(' . implode(',', $temp) . '){';
                         $j = -1;
                         while (++$j < $tLen) {
                             $res[] = '		this.' . $temp[$j] . '=(typeof(' . $temp[$j] . ')!="undefined")?' . $temp[$j] . ':null;';
                         }
                         $res[] = '	};';
                         break;
                     case 'PhpWsdlEnum':
                         $res[] = '	this.' . $t->Name . '={';
                         while (++$j < $eLen) {
                             $temp = explode('=', $t->Elements[$j], 2);
                             if (sizeof($temp) == 1) {
                                 $temp[] = $temp[0];
                             }
                             $res[] = '		"' . $temp[0] . '":"' . addslashes($temp[1]) . '"' . ($j < $eLen - 1 ? ',' : '');
                         }
                         $res[] = '	};';
                         break;
                     default:
                         PhpWsdl::Debug('WARNING: Could not create JavaScript code for unknown type "' . $class . '"!');
                         break;
                 }
             }
         }
         PhpWsdl::CallHook('ServersEndOutputJsHook', $data);
         $res[] = '};';
         $res = utf8_encode(implode("\n", $res));
         $this->JsonJs = $res;
     }
     $min = $this->ForceJsonJsMin || self::HasParam('min') && self::IsJsPackerAvailable() && !$this->ForceNotJsonJsMin;
     if ($min) {
         if (is_null($this->JsonJsMin)) {
             $this->JsonJsMin = $this->PackJavaScript($this->JsonJs);
         }
         $res = $this->JsonJsMin;
     } else {
         if (!$min) {
             $res = $this->JsonJs;
         }
     }
     if ($cache) {
         $this->Server->WriteWsdlToCache(null, null, null, true);
     }
     if ($echo) {
         echo $res;
     }
     return $res;
 }