Wsdl.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Soap
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. require_once "Zend/Soap/Wsdl/Strategy/Interface.php";
  22. require_once "Zend/Soap/Wsdl/Strategy/Abstract.php";
  23. /**
  24. * Zend_Soap_Wsdl
  25. *
  26. * @category Zend
  27. * @package Zend_Soap
  28. */
  29. class Zend_Soap_Wsdl
  30. {
  31. /**
  32. * @var object DomDocument Instance
  33. */
  34. private $_dom;
  35. /**
  36. * @var object WSDL Root XML_Tree_Node
  37. */
  38. private $_wsdl;
  39. /**
  40. * @var string URI where the WSDL will be available
  41. */
  42. private $_uri;
  43. /**
  44. * @var DOMElement
  45. */
  46. private $_schema = null;
  47. /**
  48. * Types defined on schema
  49. *
  50. * @var array
  51. */
  52. private $_includedTypes = array();
  53. /**
  54. * Strategy for detection of complex types
  55. */
  56. protected $_strategy = null;
  57. /**
  58. * Constructor
  59. *
  60. * @param string $name Name of the Web Service being Described
  61. * @param string $uri URI where the WSDL will be available
  62. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  63. */
  64. public function __construct($name, $uri, $strategy = true)
  65. {
  66. if ($uri instanceof Zend_Uri_Http) {
  67. $uri = $uri->getUri();
  68. }
  69. $this->_uri = $uri;
  70. /**
  71. * @todo change DomDocument object creation from cparsing to construxting using API
  72. * It also should authomatically escape $name and $uri values if necessary
  73. */
  74. $wsdl = "<?xml version='1.0' ?>
  75. <definitions name='$name' targetNamespace='$uri'
  76. xmlns='http://schemas.xmlsoap.org/wsdl/'
  77. xmlns:tns='$uri'
  78. xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
  79. xmlns:xsd='http://www.w3.org/2001/XMLSchema'
  80. xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/'
  81. xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'></definitions>";
  82. $this->_dom = new DOMDocument();
  83. if (!$this->_dom->loadXML($wsdl)) {
  84. require_once 'Zend/Server/Exception.php';
  85. throw new Zend_Server_Exception('Unable to create DomDocument');
  86. } else {
  87. $this->_wsdl = $this->_dom->documentElement;
  88. }
  89. $this->setComplexTypeStrategy($strategy);
  90. }
  91. /**
  92. * Set a new uri for this WSDL
  93. *
  94. * @param string|Zend_Uri_Http $uri
  95. * @return Zend_Server_Wsdl
  96. */
  97. public function setUri($uri)
  98. {
  99. if ($uri instanceof Zend_Uri_Http) {
  100. $uri = $uri->getUri();
  101. }
  102. $oldUri = $this->_uri;
  103. $this->_uri = $uri;
  104. if($this->_dom !== null) {
  105. // @todo: This is the worst hack ever, but its needed due to design and non BC issues of WSDL generation
  106. $xml = $this->_dom->saveXML();
  107. $xml = str_replace($oldUri, $uri, $xml);
  108. $this->_dom = new DOMDocument();
  109. $this->_dom->loadXML($xml);
  110. }
  111. return $this;
  112. }
  113. /**
  114. * Set a strategy for complex type detection and handling
  115. *
  116. * @todo Boolean is for backwards compability with extractComplexType object var. Remove it in later versions.
  117. * @param boolean|string|Zend_Soap_Wsdl_Strategy_Interface $strategy
  118. * @return Zend_Soap_Wsdl
  119. */
  120. public function setComplexTypeStrategy($strategy)
  121. {
  122. if($strategy === true) {
  123. require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
  124. $strategy = new Zend_Soap_Wsdl_Strategy_DefaultComplexType();
  125. } else if($strategy === false) {
  126. require_once "Zend/Soap/Wsdl/Strategy/AnyType.php";
  127. $strategy = new Zend_Soap_Wsdl_Strategy_AnyType();
  128. } else if(is_string($strategy)) {
  129. if(class_exists($strategy)) {
  130. $strategy = new $strategy();
  131. } else {
  132. require_once "Zend/Soap/Wsdl/Exception.php";
  133. throw new Zend_Soap_Wsdl_Exception(
  134. sprintf("Strategy with name '%s does not exist.", $strategy
  135. ));
  136. }
  137. }
  138. if(!($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface)) {
  139. require_once "Zend/Soap/Wsdl/Exception.php";
  140. throw new Zend_Soap_Wsdl_Exception("Set a strategy that is not of type 'Zend_Soap_Wsdl_Strategy_Interface'");
  141. }
  142. $this->_strategy = $strategy;
  143. return $this;
  144. }
  145. /**
  146. * Get the current complex type strategy
  147. *
  148. * @return Zend_Soap_Wsdl_Strategy_Interface
  149. */
  150. public function getComplexTypeStrategy()
  151. {
  152. return $this->_strategy;
  153. }
  154. /**
  155. * Add a {@link http://www.w3.org/TR/wsdl#_messages message} element to the WSDL
  156. *
  157. * @param string $name Name for the {@link http://www.w3.org/TR/wsdl#_messages message}
  158. * @param array $parts An array of {@link http://www.w3.org/TR/wsdl#_message parts}
  159. * The array is constructed like: 'name of part' => 'part xml schema data type'
  160. * @return object The new message's XML_Tree_Node for use in {@link function addDocumentation}
  161. */
  162. public function addMessage($name, $parts)
  163. {
  164. $message = $this->_dom->createElement('message');
  165. $message->setAttribute('name', $name);
  166. if (sizeof($parts) > 0) {
  167. foreach ($parts as $name => $type) {
  168. $part = $this->_dom->createElement('part');
  169. $part->setAttribute('name', $name);
  170. $part->setAttribute('type', $type);
  171. $message->appendChild($part);
  172. }
  173. }
  174. $this->_wsdl->appendChild($message);
  175. return $message;
  176. }
  177. /**
  178. * Add a {@link http://www.w3.org/TR/wsdl#_porttypes portType} element to the WSDL
  179. *
  180. * @param string $name portType element's name
  181. * @return object The new portType's XML_Tree_Node for use in {@link function addPortOperation} and {@link function addDocumentation}
  182. */
  183. public function addPortType($name)
  184. {
  185. $portType = $this->_dom->createElement('portType');
  186. $portType->setAttribute('name', $name);
  187. $this->_wsdl->appendChild($portType);
  188. return $portType;
  189. }
  190. /**
  191. * Add an {@link http://www.w3.org/TR/wsdl#_request-response operation} element to a portType element
  192. *
  193. * @param object $portType a portType XML_Tree_Node, from {@link function addPortType}
  194. * @param string $name Operation name
  195. * @param string $input Input Message
  196. * @param string $output Output Message
  197. * @param string $fault Fault Message
  198. * @return object The new operation's XML_Tree_Node for use in {@link function addDocumentation}
  199. */
  200. public function addPortOperation($portType, $name, $input = false, $output = false, $fault = false)
  201. {
  202. $operation = $this->_dom->createElement('operation');
  203. $operation->setAttribute('name', $name);
  204. if (is_string($input) && (strlen(trim($input)) >= 1)) {
  205. $node = $this->_dom->createElement('input');
  206. $node->setAttribute('message', $input);
  207. $operation->appendChild($node);
  208. }
  209. if (is_string($output) && (strlen(trim($output)) >= 1)) {
  210. $node= $this->_dom->createElement('output');
  211. $node->setAttribute('message', $output);
  212. $operation->appendChild($node);
  213. }
  214. if (is_string($fault) && (strlen(trim($fault)) >= 1)) {
  215. $node = $this->_dom->createElement('fault');
  216. $node->setAttribute('message', $fault);
  217. $operation->appendChild($node);
  218. }
  219. $portType->appendChild($operation);
  220. return $operation;
  221. }
  222. /**
  223. * Add a {@link http://www.w3.org/TR/wsdl#_bindings binding} element to WSDL
  224. *
  225. * @param string $name Name of the Binding
  226. * @param string $type name of the portType to bind
  227. * @return object The new binding's XML_Tree_Node for use with {@link function addBindingOperation} and {@link function addDocumentation}
  228. */
  229. public function addBinding($name, $portType)
  230. {
  231. $binding = $this->_dom->createElement('binding');
  232. $binding->setAttribute('name', $name);
  233. $binding->setAttribute('type', $portType);
  234. $this->_wsdl->appendChild($binding);
  235. return $binding;
  236. }
  237. /**
  238. * Add an operation to a binding element
  239. *
  240. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  241. * @param array $input An array of attributes for the input element, allowed keys are: 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  242. * @param array $output An array of attributes for the output element, allowed keys are: 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  243. * @param array $fault An array of attributes for the fault element, allowed keys are: 'name', 'use', 'namespace', 'encodingStyle'. {@link http://www.w3.org/TR/wsdl#_soap:body More Information}
  244. * @return object The new Operation's XML_Tree_Node for use with {@link function addSoapOperation} and {@link function addDocumentation}
  245. */
  246. public function addBindingOperation($binding, $name, $input = false, $output = false, $fault = false)
  247. {
  248. $operation = $this->_dom->createElement('operation');
  249. $operation->setAttribute('name', $name);
  250. if (is_array($input)) {
  251. $node = $this->_dom->createElement('input');
  252. $soap_node = $this->_dom->createElement('soap:body');
  253. foreach ($input as $name => $value) {
  254. $soap_node->setAttribute($name, $value);
  255. }
  256. $node->appendChild($soap_node);
  257. $operation->appendChild($node);
  258. }
  259. if (is_array($output)) {
  260. $node = $this->_dom->createElement('output');
  261. $soap_node = $this->_dom->createElement('soap:body');
  262. foreach ($output as $name => $value) {
  263. $soap_node->setAttribute($name, $value);
  264. }
  265. $node->appendChild($soap_node);
  266. $operation->appendChild($node);
  267. }
  268. if (is_array($fault)) {
  269. $node = $this->_dom->createElement('fault');
  270. if (isset($fault['name'])) {
  271. $node->setAttribute('name', $fault['name']);
  272. }
  273. $soap_node = $this->_dom->createElement('soap:body');
  274. foreach ($output as $name => $value) {
  275. $soap_node->setAttribute($name, $value);
  276. }
  277. $node->appendChild($soap_node);
  278. $operation->appendChild($node);
  279. }
  280. $binding->appendChild($operation);
  281. return $operation;
  282. }
  283. /**
  284. * Add a {@link http://www.w3.org/TR/wsdl#_soap:binding SOAP binding} element to a Binding element
  285. *
  286. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  287. * @param string $style binding style, possible values are "rpc" (the default) and "document"
  288. * @param string $transport Transport method (defaults to HTTP)
  289. * @return boolean
  290. */
  291. public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
  292. {
  293. $soap_binding = $this->_dom->createElement('soap:binding');
  294. $soap_binding->setAttribute('style', $style);
  295. $soap_binding->setAttribute('transport', $transport);
  296. $binding->appendChild($soap_binding);
  297. return $soap_binding;
  298. }
  299. /**
  300. * Add a {@link http://www.w3.org/TR/wsdl#_soap:operation SOAP operation} to an operation element
  301. *
  302. * @param object $operation An operation XML_Tree_Node returned by {@link function addBindingOperation}
  303. * @param string $soap_action SOAP Action
  304. * @return boolean
  305. */
  306. public function addSoapOperation($binding, $soap_action)
  307. {
  308. if ($soap_action instanceof Zend_Uri_Http) {
  309. $soap_action = $soap_action->getUri();
  310. }
  311. $soap_operation = $this->_dom->createElement('soap:operation');
  312. $soap_operation->setAttribute('soapAction', $soap_action);
  313. $binding->insertBefore($soap_operation, $binding->firstChild);
  314. return $soap_operation;
  315. }
  316. /**
  317. * Add a {@link http://www.w3.org/TR/wsdl#_services service} element to the WSDL
  318. *
  319. * @param string $name Service Name
  320. * @param string $port_name Name of the port for the service
  321. * @param string $binding Binding for the port
  322. * @param string $location SOAP Address for the service
  323. * @return object The new service's XML_Tree_Node for use with {@link function addDocumentation}
  324. */
  325. public function addService($name, $port_name, $binding, $location)
  326. {
  327. if ($location instanceof Zend_Uri_Http) {
  328. $location = $location->getUri();
  329. }
  330. $service = $this->_dom->createElement('service');
  331. $service->setAttribute('name', $name);
  332. $port = $this->_dom->createElement('port');
  333. $port->setAttribute('name', $port_name);
  334. $port->setAttribute('binding', $binding);
  335. $soap_address = $this->_dom->createElement('soap:address');
  336. $soap_address->setAttribute('location', $location);
  337. $port->appendChild($soap_address);
  338. $service->appendChild($port);
  339. $this->_wsdl->appendChild($service);
  340. return $service;
  341. }
  342. /**
  343. * Add a {@link http://www.w3.org/TR/wsdl#_documentation document} element to any element in the WSDL
  344. *
  345. * @param object $input_node An XML_Tree_Node returned by another method to add the document to
  346. * @param string $document Human readable documentation for the node
  347. * @return boolean
  348. */
  349. public function addDocumentation($input_node, $documentation)
  350. {
  351. if ($input_node === $this) {
  352. $node = $this->_dom->documentElement;
  353. } else {
  354. $node = $input_node;
  355. }
  356. /** @todo Check if 'documentation' is a correct name for the element (WSDL spec uses 'document') */
  357. $doc = $this->_dom->createElement('documentation');
  358. $doc_cdata = $this->_dom->createTextNode($documentation);
  359. $doc->appendChild($doc_cdata);
  360. $node->appendChild($doc);
  361. return $doc;
  362. }
  363. /**
  364. * Add WSDL Types element
  365. *
  366. * @param object $types A DomDocument|DomNode|DomElement|DomDocumentFragment with all the XML Schema types defined in it
  367. */
  368. public function addTypes($types)
  369. {
  370. if ($types instanceof DomDocument) {
  371. $dom = $this->_dom->importNode($types->documentElement);
  372. $this->_wsdl->appendChild($types->documentElement);
  373. } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
  374. $dom = $this->_dom->importNode($types);
  375. $this->_wsdl->appendChild($dom);
  376. }
  377. }
  378. /**
  379. * Add a complex type name that is part of this WSDL and can be used in signatures.
  380. *
  381. * @param string $type
  382. * @return Zend_Soap_Wsdl
  383. */
  384. public function addType($type)
  385. {
  386. if(!in_array($type, $this->_includedTypes)) {
  387. $this->_includedTypes[] = $type;
  388. }
  389. return $this;
  390. }
  391. /**
  392. * Return an array of all currently included complex types
  393. *
  394. * @return array
  395. */
  396. public function getTypes()
  397. {
  398. return $this->_includedTypes;
  399. }
  400. /**
  401. * Return the Schema node of the WSDL
  402. *
  403. * @return DOMElement
  404. */
  405. public function getSchema()
  406. {
  407. if($this->_schema == null) {
  408. $this->addSchemaTypeSection();
  409. }
  410. return $this->_schema;
  411. }
  412. /**
  413. * Return the WSDL as XML
  414. *
  415. * @return string WSDL as XML
  416. */
  417. public function toXML()
  418. {
  419. return $this->_dom->saveXML();
  420. }
  421. /**
  422. * Return DOM Document
  423. *
  424. * @return object DomDocum ent
  425. */
  426. public function toDomDocument()
  427. {
  428. return $this->_dom;
  429. }
  430. /**
  431. * Echo the WSDL as XML
  432. *
  433. * @return boolean
  434. */
  435. public function dump($filename = false)
  436. {
  437. if (!$filename) {
  438. echo $this->toXML();
  439. return true;
  440. } else {
  441. return file_put_contents($filename, $this->toXML());
  442. }
  443. }
  444. /**
  445. * Returns an XSD Type for the given PHP type
  446. *
  447. * @param string $type PHP Type to get the XSD type for
  448. * @return string
  449. */
  450. public function getType($type)
  451. {
  452. switch (strtolower($type)) {
  453. case 'string':
  454. case 'str':
  455. return 'xsd:string';
  456. break;
  457. case 'int':
  458. case 'integer':
  459. return 'xsd:int';
  460. break;
  461. case 'float':
  462. case 'double':
  463. return 'xsd:float';
  464. break;
  465. case 'boolean':
  466. case 'bool':
  467. return 'xsd:boolean';
  468. break;
  469. case 'array':
  470. return 'soap-enc:Array';
  471. break;
  472. case 'object':
  473. return 'xsd:struct';
  474. break;
  475. case 'mixed':
  476. return 'xsd:anyType';
  477. break;
  478. case 'void':
  479. return '';
  480. default:
  481. // delegate retrieval of complex type to current strategy
  482. return $this->addComplexType($type);
  483. }
  484. }
  485. /**
  486. * This function makes sure a complex types section and schema additions are set.
  487. *
  488. * @return Zend_Soap_Wsdl
  489. */
  490. public function addSchemaTypeSection()
  491. {
  492. if ($this->_schema === null) {
  493. $this->_schema = $this->_dom->createElement('xsd:schema');
  494. $this->_schema->setAttribute('targetNamespace', $this->_uri);
  495. $types = $this->_dom->createElement('types');
  496. $types->appendChild($this->_schema);
  497. $this->_wsdl->appendChild($types);
  498. }
  499. return $this;
  500. }
  501. /**
  502. * Add a {@link http://www.w3.org/TR/wsdl#_types types} data type definition
  503. *
  504. * @param string $type Name of the class to be specified
  505. * @return string XSD Type for the given PHP type
  506. */
  507. public function addComplexType($type)
  508. {
  509. if (in_array($type, $this->getTypes())) {
  510. return "tns:$type";
  511. }
  512. $this->addSchemaTypeSection();
  513. $strategy = $this->getComplexTypeStrategy();
  514. $strategy->setContext($this);
  515. // delegates the detection of a complex type to the current strategy
  516. return $strategy->addComplexType($type);
  517. }
  518. }