WSHelper.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Class that generates a WSDL file and creates documentation
  4. * for your webservices.
  5. *
  6. * Patch by Shawn Cook (Shawn@itbytez.com) for the useWSDLCache option
  7. *
  8. * @author David Kingma
  9. * @version 1.5
  10. */
  11. class WSHelper {
  12. private $uri;
  13. private $class = null; //IPReflectionClass object
  14. private $name; //class name
  15. private $persistence = SOAP_PERSISTENCE_SESSION;
  16. private $wsdlfile; //wsdl file name
  17. private $server; //soap server object
  18. public $actor;
  19. public $structureMap = array();
  20. public $classNameArr = array();
  21. public $wsdlFolder; //WSDL cache folder
  22. public $useWSDLCache = true;
  23. public $type = SOAP_RPC;
  24. public $use = SOAP_LITERAL;
  25. /**
  26. * Constructor
  27. * @param string The Uri name
  28. * @return void
  29. */
  30. public function __construct($uri, $class=null){
  31. $this->uri = $uri;
  32. //$this->setWSDLCacheFolder($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['PHP_SELF'])."/wsdl/");
  33. $this->setWSDLCacheFolder("/tmp/wsdl/");
  34. if($class) $this->setClass($class);
  35. }
  36. /**
  37. * Adds the given class name to the list of classes
  38. * to be included in the documentation/WSDL/Request handlers
  39. * @param string
  40. * @return void
  41. */
  42. public function setClass($name){
  43. $this->name = $name;
  44. $this->wsdlfile = $this->wsdlFolder.$this->name.".wsdl";
  45. }
  46. public function setWSDLCacheFolder($folder) {
  47. $this->wsdlFolder = $folder;
  48. //reset wsdlfile
  49. $this->wsdlfile = $this->wsdlFolder.$this->name.".wsdl";
  50. }
  51. /**
  52. * Sets the persistence level for the soap class
  53. */
  54. public function setPersistence($persistence) {
  55. $this->persistence = $persistence;
  56. }
  57. /**
  58. * Handles everything. Makes sure the webservice is handled,
  59. * documentations is generated, or the wsdl is generated,
  60. * according to the page request
  61. * @return void
  62. */
  63. public function handle(){
  64. if(substr($_SERVER['QUERY_STRING'], -4) == 'wsdl'){
  65. $this->showWSDL();
  66. }elseif(isset($GLOBALS['HTTP_RAW_POST_DATA']) && strlen($GLOBALS['HTTP_RAW_POST_DATA'])>0){
  67. $this->handleRequest();
  68. }else{
  69. $this->createDocumentation();
  70. }
  71. }
  72. /**
  73. * Checks if the current WSDL is up-to-date, regenerates if necessary and outputs the WSDL
  74. * @return void
  75. */
  76. public function showWSDL(){
  77. //check if it's a legal webservice class
  78. if(!in_array($this->name, $this->classNameArr))
  79. throw new Exception("No valid webservice class.");
  80. //@TODO: nog een mooie oplossing voor het cachen zoeken
  81. header("Content-type: text/xml");
  82. if($this->useWSDLCache && file_exists($this->wsdlfile)){
  83. readfile($this->wsdlfile);
  84. }else{
  85. //make sure to refresh PHP WSDL cache system
  86. ini_set("soap.wsdl_cache_enabled",0);
  87. echo $this->createWSDL();
  88. }
  89. }
  90. private function createWSDL(){
  91. $this->class = new IPReflectionClass($this->name);
  92. $wsdl = new WSDLStruct($this->uri, "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']."?class=".$this->name, $this->type, $this->use);
  93. $wsdl->setService($this->class);
  94. try {
  95. $gendoc = $wsdl->generateDocument();
  96. } catch (WSDLException $exception) {
  97. $exception->Display();
  98. exit();
  99. }
  100. $fh = fopen($this->wsdlfile, "w+");
  101. fwrite($fh, $gendoc);
  102. fclose($fh);
  103. return $gendoc;
  104. }
  105. /**
  106. * Lets the native PHP5 soap implementation handle the request
  107. * after registrating the class
  108. * @return void
  109. */
  110. private function handleRequest(){
  111. //check if it's a legal webservice class
  112. if(!in_array($this->name, $this->classNameArr))
  113. throw new Exception("No valid webservice class.");
  114. //check cache
  115. if(!file_exists($this->wsdlfile))
  116. $this->createWSDL();
  117. $options = Array('actor' => $this->actor, 'classmap' => $this->structureMap);
  118. header("Content-type: text/xml");
  119. $this->server = new SoapServer($this->wsdlfile, $options);
  120. $this->server->setClass($this->name);
  121. $this->server->setPersistence($this->persistence);
  122. use_soap_error_handler(true);
  123. $this->server->handle();
  124. }
  125. /**
  126. * @param string code
  127. * @param string string
  128. * @param string actor
  129. * @param mixed details
  130. * @param string name
  131. * @return void
  132. */
  133. public function fault($code, $string, $actor, $details, $name='') {
  134. if($this->server)
  135. return $this->server->fault($code, $string, $actor, $details, $name);
  136. else
  137. die($details);
  138. }
  139. /**
  140. * Generates the documentations for the webservice usage.
  141. * @TODO: "int", "boolean", "double", "float", "string", "void"
  142. * @param string Template filename
  143. * @return void
  144. */
  145. public function createDocumentation($template="wshelper/templates/docclass.xsl") {
  146. if(!is_file($template))
  147. throw new WSException("Could not find the template file: '$template'");
  148. $this->class = new IPReflectionClass($this->name);
  149. $xtpl = new IPXSLTemplate($template);
  150. $documentation = Array();
  151. $documentation['menu'] = Array();
  152. //loop menu items
  153. sort($this->classNameArr);//ff sorteren
  154. foreach($this->classNameArr as $className) {
  155. $documentation['menu'][] = new IPReflectionClass($className);
  156. }
  157. if($this->class){
  158. $this->class->properties = $this->class->getProperties(false, false);
  159. $this->class->methods = $this->class->getMethods(false, false);
  160. foreach((array)$this->class->methods as $method) {
  161. $method->params = $method->getParameters();
  162. }
  163. $documentation['class'] = $this->class;
  164. }
  165. echo $xtpl->execute($documentation);
  166. }
  167. }
  168. ?>