PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/com/mikebevz/xsd2php/wsdl/Wsdl_1_1.php

http://github.com/moyarada/XSD-to-PHP
PHP | 362 lines | 188 code | 55 blank | 119 comment | 16 complexity | 7fe624e325d827216bc5ade28e1d6884 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. namespace com\mikebevz\xsd2php\wsdl;
  3. require_once 'AbstractWsdl.php';
  4. require_once 'IWsdl.php';
  5. class Wsdl_1_1 extends AbstractWsdl implements IWsdl {
  6. /**
  7. * WSDL 1.1 XML namespaces
  8. *
  9. * @var array
  10. */
  11. protected $namespaces = array (
  12. 'wsdl' => 'http://schemas.xmlsoap.org/wsdl/',
  13. 'soap' => 'http://schemas.xmlsoap.org/wsdl/soap/',
  14. 'http' => 'http://schemas.xmlsoap.org/wsdl/http/',
  15. 'mime' => 'http://schemas.xmlsoap.org/wsdl/mime/',
  16. 'soapenc' => 'http://schemas.xmlsoap.org/soap/encoding/',
  17. 'soapenv' => 'http://schemas.xmlsoap.org/soap/envelope/',
  18. 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  19. 'xsd' => 'http://www.w3.org/2001/XMLSchema',
  20. );
  21. /**
  22. * Prepare WSDL DOM model
  23. *
  24. * @return void
  25. */
  26. public function prepare() {
  27. $this->dom->createAttributeNS($this->namespaces['soap'], 'soap:definitions');
  28. $this->dom->createAttributeNS($this->namespaces[$this->xmlSchemaPreffix], $this->xmlSchemaPreffix.':definitions');
  29. //@todo this only required when we have arrays
  30. $this->dom->createAttributeNS($this->namespaces['soapenc'], 'soapenc:definitions');
  31. $this->wsTypes = $this->dom->createElement('wsdl:types');
  32. $this->wsDefinitions->appendChild($this->wsTypes);
  33. $wsXmlSchema = $this->dom->createElement($this->xmlSchemaPreffix.':schema');
  34. $wsXmlSchema->setAttribute('targetNamespace', $this->wsdlTargetNamespace);
  35. $this->wsXmlSchema = $this->wsTypes->appendChild($wsXmlSchema);
  36. $portType = $this->dom->createElement('wsdl:portType');
  37. $portType->setAttribute('name', $this->wsdlName.$this->portNameSuffix);
  38. $this->wsPortType = $this->wsDefinitions->appendChild($portType);
  39. $binding = $this->dom->createElement('wsdl:binding');
  40. $binding->setAttribute('name', $this->wsdlName.$this->bindingNameSuffix);
  41. $binding->setAttribute('type', $this->targetNsPrefix.":".$this->wsdlName.$this->portNameSuffix);
  42. $this->wsBinding = $this->wsDefinitions->appendChild($binding);
  43. $soapBinding = $this->dom->createElement('soap:binding');
  44. if ($this->soapBindingStyle === SOAP_DOCUMENT) {
  45. $soapBinding->setAttribute('style', "document");
  46. } elseif ($this->soapBindingStyle === SOAP_RPC) {
  47. $soapBinding->setAttribute('style', "rpc");
  48. }
  49. $soapBinding->setAttribute('transport', $this->soapBindingTransport);
  50. $this->wsBinding->appendChild($soapBinding);
  51. $wsService = $this->dom->createElement('wsdl:service');
  52. $wsService->setAttribute('name', $this->wsdlName.$this->serviceNameSuffix);
  53. $this->wsService = $this->wsDefinitions->appendChild($wsService);
  54. $port = $this->dom->createElement('wsdl:port');
  55. $port->setAttribute('name', $this->wsdlName.$this->portNameSuffix);
  56. $port->setAttribute('binding', $this->targetNsPrefix.":".$this->wsdlName.$this->bindingNameSuffix);
  57. $soapAddress = $this->dom->createElement('soap:address');
  58. $soapAddress->setAttribute('location', $this->getLocation());
  59. $port->appendChild($soapAddress);
  60. $this->wsService->appendChild($port);
  61. $this->extractTypes();
  62. }
  63. /**
  64. * Extract types from WSDL
  65. *
  66. * @return void
  67. */
  68. private function extractTypes() {
  69. $allEl = array();
  70. $allImp = array();
  71. $methods = array();
  72. foreach ($this->methodsMeta as $methodName => $data) {
  73. $elements = array();
  74. $input = false;
  75. $output = false;
  76. if (count($data['params']) == 1) {
  77. $type = $this->common->phpTypeToSoap($data['params'][0]['type']);
  78. $els = array();
  79. if ($type === false) {
  80. $type = $this->getTypeName($data['params'][0]['type']);
  81. $els [] = $this->createRefElement($type);
  82. } else {
  83. $els [] = $this->createSimpleElement(ucfirst($data['params'][0]['name']), $data['params'][0]['type']);
  84. }
  85. $elements[] = $this->createElementWithComplexType(ucfirst($methodName), $els);
  86. $this->addMessage(ucfirst($methodName), $this->targetNsPrefix.":".ucfirst($methodName));
  87. $input = true;
  88. } elseif($data['params'] > 1) {
  89. $els = array();
  90. foreach ($data['params'] as $input) {
  91. $type = $this->common->phpTypeToSoap($input['type']);
  92. if ($type === false) {
  93. $type = $this->getTypeName($input['type']);
  94. $els [] = $this->createRefElement($type);
  95. } else {
  96. $els [] = $this->createSimpleElement(ucfirst($input['name']), $input['type']);
  97. }
  98. }
  99. $elements[] = $this->createElementWithComplexType(ucfirst($methodName), $els);
  100. $this->addMessage(ucfirst($methodName), $this->targetNsPrefix.":".ucfirst($methodName));
  101. $input = true;
  102. }
  103. if (array_key_exists('return', $data)) {
  104. $type = $this->common->phpTypeToSoap($data['return']['type']);
  105. if ($type === false) {
  106. //@todo Create complex type
  107. } else {
  108. $elements [] = $this->createSimpleElement(ucfirst($methodName)."Response", $type);
  109. }
  110. //$messages[] = $this->targetNsPrefix.":".ucfirst($methodName)."Response";
  111. $this->addMessage(ucfirst($methodName)."Response", $this->targetNsPrefix.":".ucfirst($methodName));
  112. $output = true;
  113. }
  114. foreach ($this->xmlSchemaImports as $imp) {
  115. array_push($allImp, $imp);
  116. }
  117. foreach ($elements as $element) {
  118. array_push($allEl, $element);
  119. }
  120. $this->addPortOperations(ucfirst($methodName), $input, $output);
  121. $this->addBindingOperations(ucfirst($methodName), $input, $output);
  122. }
  123. foreach ($allImp as $import) {
  124. $this->wsXmlSchema->appendChild($import);
  125. }
  126. foreach ($allEl as $element) {
  127. $this->wsXmlSchema->appendChild($element);
  128. }
  129. // Add port operations
  130. //print_r($messages);
  131. }
  132. /**
  133. * Add operations to bindings
  134. *
  135. * <code>
  136. * <operation name="$operation">
  137. * <soap:operation
  138. * soapAction="targetNamespace/$operation" />
  139. * <input>
  140. * $input
  141. * </input>
  142. * <output>
  143. * $output
  144. * </output>
  145. * </operation>
  146. * </code>
  147. *
  148. * @param string $operation Operation name
  149. * @param array $input Array of inputs [optional]
  150. * @param array $output Array of outputs [optional]
  151. *
  152. * @return void
  153. */
  154. private function addBindingOperations($operation, $input = false, $output = false) {
  155. $el = $this->dom->createElement('wsdl:operation');
  156. $el->setAttribute('name', $operation);
  157. $soapOperation = $this->dom->createElement('soap:operation');
  158. $soapOperation->setAttribute('soapAction', $this->wsdlTargetNamespace."/".$operation);
  159. $el->appendChild($soapOperation);
  160. if ($input === true) {
  161. $inp = $this->dom->createElement('wsdl:input');
  162. $body = $this->dom->createElement('soap:body');
  163. $body->setAttribute('use', 'literal');
  164. $inp->appendChild($body);
  165. $el->appendChild($inp);
  166. }
  167. if ($output === true) {
  168. $out = $this->dom->createElement('wsdl:output');
  169. $body = $this->dom->createElement('soap:body');
  170. $body->setAttribute('use', 'literal');
  171. $out->appendChild($body);
  172. $el->appendChild($out);
  173. }
  174. $this->wsBinding->appendChild($el);
  175. }
  176. /**
  177. * Add operations to port
  178. *
  179. * <code>
  180. * <operation name="$name">
  181. * <input message="tns:messageName" />
  182. * <output message="tns:messageName" />
  183. * </operation>
  184. * </code>
  185. *
  186. * @param string $operation Name of the operation
  187. * @param array $input Array of inputs
  188. * @param array $output Array of outputs
  189. *
  190. * @return void
  191. */
  192. private function addPortOperations($operation, $input = false, $output = false) {
  193. $el = $this->dom->createElement("wsdl:operation");
  194. $el->setAttribute('name', $operation);
  195. if ($input === true) {
  196. $input = $this->dom->createElement('wsdl:input');
  197. $input->setAttribute('message', $this->targetNsPrefix.":".$operation);
  198. }
  199. if ($output === true) {
  200. $output = $this->dom->createElement('wsdl:output');
  201. $output->setAttribute('message', $this->targetNsPrefix.":". $operation."Response");
  202. }
  203. $el->appendChild($input);
  204. $el->appendChild($output);
  205. $this->wsPortType->appendChild($el);
  206. }
  207. /**
  208. * Add message
  209. *
  210. * <code>
  211. * <message name="$name">
  212. * <part name="$name" element="$type" />
  213. * </message>
  214. * </code>
  215. *
  216. * @param string $name Message name
  217. * @param string $type Message type
  218. *
  219. * @return void
  220. */
  221. private function addMessage($name, $type) {
  222. $el = $this->dom->createElement("wsdl:message");
  223. $el->setAttribute('name', $name);
  224. $parts = $this->dom->createElement("wsdl:part");
  225. $parts->setAttribute('name', $name);
  226. $parts->setAttribute('element', $type);
  227. $el->appendChild($parts);
  228. //$this->wsMessage->appendChild($el);
  229. //$this->dom->insertBefore($el, $this->wsPortType);
  230. //$this->wsDefinitions->replaceChild($el, $this->wsMessage);
  231. $xpath = new \DOMXPath($this->dom);
  232. $query = "//*[local-name()='definitions']/*";
  233. $types = $xpath->query($query);
  234. //print_r($types->item(0)->getNodePath());
  235. //$this->dom->insertBefore($el, $types->item(1));
  236. $this->wsDefinitions->insertBefore($el, $types->item(1));
  237. }
  238. /**
  239. * Create ref element
  240. *
  241. * <code>
  242. * <element ref="$ref" />
  243. * </code>
  244. *
  245. * @param string $ref
  246. *
  247. * @return DOMElement
  248. */
  249. private function createRefElement($ref) {
  250. $el = $this->dom->createElement($this->xmlSchemaPreffix.':element');
  251. $el->setAttribute('ref', $ref);
  252. return $el;
  253. }
  254. /**
  255. * Create an element with complex type
  256. *
  257. * <code>
  258. * <element name="$name">
  259. * <complexType>
  260. * <sequence>
  261. * $elements
  262. * </sequence>
  263. * </complexType>
  264. * </element>
  265. * </code>
  266. *
  267. * @param string $name Element name
  268. * @param array $elements Array of elements to include into sequence
  269. *
  270. * @return DOMElement
  271. */
  272. private function createElementWithComplexType($name, $elements) {
  273. //print_r('Create complex type '.$name."\n");
  274. $el = $this->dom->createElement($this->xmlSchemaPreffix.':element');
  275. $el->setAttribute('name', $name);
  276. $compType = $this->dom->createElement($this->xmlSchemaPreffix.':complexType');
  277. $seqType = $this->dom->createElement($this->xmlSchemaPreffix.':sequence');
  278. foreach ($elements as $element) {
  279. $seqType->appendChild($element);
  280. }
  281. $compType->appendChild($seqType);
  282. $el->appendChild($compType);
  283. return $el;
  284. }
  285. /**
  286. * Create simple element
  287. *
  288. * <code>
  289. * <element name="$name" type="$type" />
  290. * </code>
  291. *
  292. * @param string $name Element name
  293. * @param string $type Element type
  294. *
  295. * @return DOMElement
  296. */
  297. private function createSimpleElement($name, $type) {
  298. $el = $this->dom->createElement($this->xmlSchemaPreffix.':element');
  299. $el->setAttribute('name', $name);
  300. if (preg_match('/:/', $type)) {
  301. $el->setAttribute('type', $type);
  302. } else {
  303. $el->setAttribute('type', $this->xmlSchemaPreffix.':'.$type);
  304. }
  305. return $el;
  306. }
  307. }