PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Soap/Wsdl.php

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