2
0

Wsdl.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  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. * or 'name of part' => array('type' => 'part xml schema type')
  161. * or 'name of part' => array('element' => 'part xml element name')
  162. * @return object The new message's XML_Tree_Node for use in {@link function addDocumentation}
  163. */
  164. public function addMessage($name, $parts)
  165. {
  166. $message = $this->_dom->createElement('message');
  167. $message->setAttribute('name', $name);
  168. if (sizeof($parts) > 0) {
  169. foreach ($parts as $name => $type) {
  170. $part = $this->_dom->createElement('part');
  171. $part->setAttribute('name', $name);
  172. if (is_array($type)) {
  173. foreach ($type as $key => $value) {
  174. $part->setAttribute($key, $value);
  175. }
  176. } else {
  177. $part->setAttribute('type', $type);
  178. }
  179. $message->appendChild($part);
  180. }
  181. }
  182. $this->_wsdl->appendChild($message);
  183. return $message;
  184. }
  185. /**
  186. * Add a {@link http://www.w3.org/TR/wsdl#_porttypes portType} element to the WSDL
  187. *
  188. * @param string $name portType element's name
  189. * @return object The new portType's XML_Tree_Node for use in {@link function addPortOperation} and {@link function addDocumentation}
  190. */
  191. public function addPortType($name)
  192. {
  193. $portType = $this->_dom->createElement('portType');
  194. $portType->setAttribute('name', $name);
  195. $this->_wsdl->appendChild($portType);
  196. return $portType;
  197. }
  198. /**
  199. * Add an {@link http://www.w3.org/TR/wsdl#_request-response operation} element to a portType element
  200. *
  201. * @param object $portType a portType XML_Tree_Node, from {@link function addPortType}
  202. * @param string $name Operation name
  203. * @param string $input Input Message
  204. * @param string $output Output Message
  205. * @param string $fault Fault Message
  206. * @return object The new operation's XML_Tree_Node for use in {@link function addDocumentation}
  207. */
  208. public function addPortOperation($portType, $name, $input = false, $output = false, $fault = false)
  209. {
  210. $operation = $this->_dom->createElement('operation');
  211. $operation->setAttribute('name', $name);
  212. if (is_string($input) && (strlen(trim($input)) >= 1)) {
  213. $node = $this->_dom->createElement('input');
  214. $node->setAttribute('message', $input);
  215. $operation->appendChild($node);
  216. }
  217. if (is_string($output) && (strlen(trim($output)) >= 1)) {
  218. $node= $this->_dom->createElement('output');
  219. $node->setAttribute('message', $output);
  220. $operation->appendChild($node);
  221. }
  222. if (is_string($fault) && (strlen(trim($fault)) >= 1)) {
  223. $node = $this->_dom->createElement('fault');
  224. $node->setAttribute('message', $fault);
  225. $operation->appendChild($node);
  226. }
  227. $portType->appendChild($operation);
  228. return $operation;
  229. }
  230. /**
  231. * Add a {@link http://www.w3.org/TR/wsdl#_bindings binding} element to WSDL
  232. *
  233. * @param string $name Name of the Binding
  234. * @param string $type name of the portType to bind
  235. * @return object The new binding's XML_Tree_Node for use with {@link function addBindingOperation} and {@link function addDocumentation}
  236. */
  237. public function addBinding($name, $portType)
  238. {
  239. $binding = $this->_dom->createElement('binding');
  240. $binding->setAttribute('name', $name);
  241. $binding->setAttribute('type', $portType);
  242. $this->_wsdl->appendChild($binding);
  243. return $binding;
  244. }
  245. /**
  246. * Add an operation to a binding element
  247. *
  248. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  249. * @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}
  250. * @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}
  251. * @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}
  252. * @return object The new Operation's XML_Tree_Node for use with {@link function addSoapOperation} and {@link function addDocumentation}
  253. */
  254. public function addBindingOperation($binding, $name, $input = false, $output = false, $fault = false)
  255. {
  256. $operation = $this->_dom->createElement('operation');
  257. $operation->setAttribute('name', $name);
  258. if (is_array($input)) {
  259. $node = $this->_dom->createElement('input');
  260. $soap_node = $this->_dom->createElement('soap:body');
  261. foreach ($input as $name => $value) {
  262. $soap_node->setAttribute($name, $value);
  263. }
  264. $node->appendChild($soap_node);
  265. $operation->appendChild($node);
  266. }
  267. if (is_array($output)) {
  268. $node = $this->_dom->createElement('output');
  269. $soap_node = $this->_dom->createElement('soap:body');
  270. foreach ($output as $name => $value) {
  271. $soap_node->setAttribute($name, $value);
  272. }
  273. $node->appendChild($soap_node);
  274. $operation->appendChild($node);
  275. }
  276. if (is_array($fault)) {
  277. $node = $this->_dom->createElement('fault');
  278. if (isset($fault['name'])) {
  279. $node->setAttribute('name', $fault['name']);
  280. }
  281. $soap_node = $this->_dom->createElement('soap:body');
  282. foreach ($output as $name => $value) {
  283. $soap_node->setAttribute($name, $value);
  284. }
  285. $node->appendChild($soap_node);
  286. $operation->appendChild($node);
  287. }
  288. $binding->appendChild($operation);
  289. return $operation;
  290. }
  291. /**
  292. * Add a {@link http://www.w3.org/TR/wsdl#_soap:binding SOAP binding} element to a Binding element
  293. *
  294. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  295. * @param string $style binding style, possible values are "rpc" (the default) and "document"
  296. * @param string $transport Transport method (defaults to HTTP)
  297. * @return boolean
  298. */
  299. public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
  300. {
  301. $soap_binding = $this->_dom->createElement('soap:binding');
  302. $soap_binding->setAttribute('style', $style);
  303. $soap_binding->setAttribute('transport', $transport);
  304. $binding->appendChild($soap_binding);
  305. return $soap_binding;
  306. }
  307. /**
  308. * Add a {@link http://www.w3.org/TR/wsdl#_soap:operation SOAP operation} to an operation element
  309. *
  310. * @param object $operation An operation XML_Tree_Node returned by {@link function addBindingOperation}
  311. * @param string $soap_action SOAP Action
  312. * @return boolean
  313. */
  314. public function addSoapOperation($binding, $soap_action)
  315. {
  316. if ($soap_action instanceof Zend_Uri_Http) {
  317. $soap_action = $soap_action->getUri();
  318. }
  319. $soap_operation = $this->_dom->createElement('soap:operation');
  320. $soap_operation->setAttribute('soapAction', $soap_action);
  321. $binding->insertBefore($soap_operation, $binding->firstChild);
  322. return $soap_operation;
  323. }
  324. /**
  325. * Add a {@link http://www.w3.org/TR/wsdl#_services service} element to the WSDL
  326. *
  327. * @param string $name Service Name
  328. * @param string $port_name Name of the port for the service
  329. * @param string $binding Binding for the port
  330. * @param string $location SOAP Address for the service
  331. * @return object The new service's XML_Tree_Node for use with {@link function addDocumentation}
  332. */
  333. public function addService($name, $port_name, $binding, $location)
  334. {
  335. if ($location instanceof Zend_Uri_Http) {
  336. $location = $location->getUri();
  337. }
  338. $service = $this->_dom->createElement('service');
  339. $service->setAttribute('name', $name);
  340. $port = $this->_dom->createElement('port');
  341. $port->setAttribute('name', $port_name);
  342. $port->setAttribute('binding', $binding);
  343. $soap_address = $this->_dom->createElement('soap:address');
  344. $soap_address->setAttribute('location', $location);
  345. $port->appendChild($soap_address);
  346. $service->appendChild($port);
  347. $this->_wsdl->appendChild($service);
  348. return $service;
  349. }
  350. /**
  351. * Add a documentation element to any element in the WSDL.
  352. *
  353. * Note that the WSDL {@link http://www.w3.org/TR/wsdl#_documentation specification} uses 'document',
  354. * but the WSDL {@link http://schemas.xmlsoap.org/wsdl/ schema} uses 'documentation' instead.
  355. * The {@link http://www.ws-i.org/Profiles/BasicProfile-1.1-2004-08-24.html#WSDL_documentation_Element WS-I Basic Profile 1.1} recommends using 'documentation'.
  356. *
  357. * @param object $input_node An XML_Tree_Node returned by another method to add the documentation to
  358. * @param string $documentation Human readable documentation for the node
  359. * @return DOMElement The documentation element
  360. */
  361. public function addDocumentation($input_node, $documentation)
  362. {
  363. if ($input_node === $this) {
  364. $node = $this->_dom->documentElement;
  365. } else {
  366. $node = $input_node;
  367. }
  368. $doc = $this->_dom->createElement('documentation');
  369. $doc_cdata = $this->_dom->createTextNode($documentation);
  370. $doc->appendChild($doc_cdata);
  371. if($node->hasChildNodes()) {
  372. $node->insertBefore($doc, $node->firstChild);
  373. } else {
  374. $node->appendChild($doc);
  375. }
  376. return $doc;
  377. }
  378. /**
  379. * Add WSDL Types element
  380. *
  381. * @param object $types A DomDocument|DomNode|DomElement|DomDocumentFragment with all the XML Schema types defined in it
  382. */
  383. public function addTypes($types)
  384. {
  385. if ($types instanceof DomDocument) {
  386. $dom = $this->_dom->importNode($types->documentElement);
  387. $this->_wsdl->appendChild($types->documentElement);
  388. } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
  389. $dom = $this->_dom->importNode($types);
  390. $this->_wsdl->appendChild($dom);
  391. }
  392. }
  393. /**
  394. * Add a complex type name that is part of this WSDL and can be used in signatures.
  395. *
  396. * @param string $type
  397. * @return Zend_Soap_Wsdl
  398. */
  399. public function addType($type)
  400. {
  401. if(!in_array($type, $this->_includedTypes)) {
  402. $this->_includedTypes[] = $type;
  403. }
  404. return $this;
  405. }
  406. /**
  407. * Return an array of all currently included complex types
  408. *
  409. * @return array
  410. */
  411. public function getTypes()
  412. {
  413. return $this->_includedTypes;
  414. }
  415. /**
  416. * Return the Schema node of the WSDL
  417. *
  418. * @return DOMElement
  419. */
  420. public function getSchema()
  421. {
  422. if($this->_schema == null) {
  423. $this->addSchemaTypeSection();
  424. }
  425. return $this->_schema;
  426. }
  427. /**
  428. * Return the WSDL as XML
  429. *
  430. * @return string WSDL as XML
  431. */
  432. public function toXML()
  433. {
  434. return $this->_dom->saveXML();
  435. }
  436. /**
  437. * Return DOM Document
  438. *
  439. * @return object DomDocum ent
  440. */
  441. public function toDomDocument()
  442. {
  443. return $this->_dom;
  444. }
  445. /**
  446. * Echo the WSDL as XML
  447. *
  448. * @return boolean
  449. */
  450. public function dump($filename = false)
  451. {
  452. if (!$filename) {
  453. echo $this->toXML();
  454. return true;
  455. } else {
  456. return file_put_contents($filename, $this->toXML());
  457. }
  458. }
  459. /**
  460. * Returns an XSD Type for the given PHP type
  461. *
  462. * @param string $type PHP Type to get the XSD type for
  463. * @return string
  464. */
  465. public function getType($type)
  466. {
  467. switch (strtolower($type)) {
  468. case 'string':
  469. case 'str':
  470. return 'xsd:string';
  471. break;
  472. case 'int':
  473. case 'integer':
  474. return 'xsd:int';
  475. break;
  476. case 'float':
  477. case 'double':
  478. return 'xsd:float';
  479. break;
  480. case 'boolean':
  481. case 'bool':
  482. return 'xsd:boolean';
  483. break;
  484. case 'array':
  485. return 'soap-enc:Array';
  486. break;
  487. case 'object':
  488. return 'xsd:struct';
  489. break;
  490. case 'mixed':
  491. return 'xsd:anyType';
  492. break;
  493. case 'void':
  494. return '';
  495. default:
  496. // delegate retrieval of complex type to current strategy
  497. return $this->addComplexType($type);
  498. }
  499. }
  500. /**
  501. * This function makes sure a complex types section and schema additions are set.
  502. *
  503. * @return Zend_Soap_Wsdl
  504. */
  505. public function addSchemaTypeSection()
  506. {
  507. if ($this->_schema === null) {
  508. $this->_schema = $this->_dom->createElement('xsd:schema');
  509. $this->_schema->setAttribute('targetNamespace', $this->_uri);
  510. $types = $this->_dom->createElement('types');
  511. $types->appendChild($this->_schema);
  512. $this->_wsdl->appendChild($types);
  513. }
  514. return $this;
  515. }
  516. /**
  517. * Add a {@link http://www.w3.org/TR/wsdl#_types types} data type definition
  518. *
  519. * @param string $type Name of the class to be specified
  520. * @return string XSD Type for the given PHP type
  521. */
  522. public function addComplexType($type)
  523. {
  524. if (in_array($type, $this->getTypes())) {
  525. return "tns:$type";
  526. }
  527. $this->addSchemaTypeSection();
  528. $strategy = $this->getComplexTypeStrategy();
  529. $strategy->setContext($this);
  530. // delegates the detection of a complex type to the current strategy
  531. return $strategy->addComplexType($type);
  532. }
  533. /**
  534. * Parse an xsd:element represented as an array into a DOMElement.
  535. *
  536. * @param array $element an xsd:element represented as an array
  537. * @return DOMElement parsed element
  538. */
  539. private function _parseElement($element)
  540. {
  541. if (!is_array($element)) {
  542. require_once "Zend/Soap/Wsdl/Exception.php";
  543. throw new Zend_Soap_Wsdl_Exception("The 'element' parameter needs to be an associative array.");
  544. }
  545. $elementXml = $this->_dom->createElement('xsd:element');
  546. foreach ($element as $key => $value) {
  547. if (in_array($key, array('sequence', 'all', 'choice'))) {
  548. if (is_array($value)) {
  549. $complexType = $this->_dom->createElement('xsd:complexType');
  550. if (count($value) > 0) {
  551. $container = $this->_dom->createElement('xsd:' . $key);
  552. foreach ($value as $subelement) {
  553. $subelementXml = $this->_parseElement($subelement);
  554. $container->appendChild($subelementXml);
  555. }
  556. $complexType->appendChild($container);
  557. }
  558. $elementXml->appendChild($complexType);
  559. }
  560. } else {
  561. $elementXml->setAttribute($key, $value);
  562. }
  563. }
  564. return $elementXml;
  565. }
  566. /**
  567. * Add an xsd:element represented as an array to the schema.
  568. *
  569. * Array keys represent attribute names and values their respective value.
  570. * The 'sequence', 'all' and 'choice' keys must have an array of elements as their value,
  571. * to add them to a nested complexType.
  572. *
  573. * Example: array( 'name' => 'MyElement',
  574. * 'sequence' => array( array('name' => 'myString', 'type' => 'string'),
  575. * array('name' => 'myInteger', 'type' => 'int') ) );
  576. * Resulting XML: <xsd:element name="MyElement"><xsd:complexType><xsd:sequence>
  577. * <xsd:element name="myString" type="string"/>
  578. * <xsd:element name="myInteger" type="int"/>
  579. * </xsd:sequence></xsd:complexType></xsd:element>
  580. *
  581. * @param array $element an xsd:element represented as an array
  582. * @return string xsd:element for the given element array
  583. */
  584. public function addElement($element)
  585. {
  586. $schema = $this->getSchema();
  587. $elementXml = $this->_parseElement($element);
  588. $schema->appendChild($elementXml);
  589. return 'tns:' . $element['name'];
  590. }
  591. }