PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/website/library/Zend/Soap/Wsdl.php

https://bitbucket.org/efdac/e-forest_platform
PHP | 666 lines | 378 code | 66 blank | 222 comment | 48 complexity | 3452fff5864f825e0e41fc60557bd210 MD5 | raw file
  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-2010 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 20096 2010-01-06 02:05:09Z bkarwin $
  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. if (isset($fault['name'])) {
  285. $node->setAttribute('name', $fault['name']);
  286. }
  287. $soap_node = $this->_dom->createElement('soap:body');
  288. foreach ($output as $name => $value) {
  289. $soap_node->setAttribute($name, $value);
  290. }
  291. $node->appendChild($soap_node);
  292. $operation->appendChild($node);
  293. }
  294. $binding->appendChild($operation);
  295. return $operation;
  296. }
  297. /**
  298. * Add a {@link http://www.w3.org/TR/wsdl#_soap:binding SOAP binding} element to a Binding element
  299. *
  300. * @param object $binding A binding XML_Tree_Node returned by {@link function addBinding}
  301. * @param string $style binding style, possible values are "rpc" (the default) and "document"
  302. * @param string $transport Transport method (defaults to HTTP)
  303. * @return boolean
  304. */
  305. public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
  306. {
  307. $soap_binding = $this->_dom->createElement('soap:binding');
  308. $soap_binding->setAttribute('style', $style);
  309. $soap_binding->setAttribute('transport', $transport);
  310. $binding->appendChild($soap_binding);
  311. return $soap_binding;
  312. }
  313. /**
  314. * Add a {@link http://www.w3.org/TR/wsdl#_soap:operation SOAP operation} to an operation element
  315. *
  316. * @param object $operation An operation XML_Tree_Node returned by {@link function addBindingOperation}
  317. * @param string $soap_action SOAP Action
  318. * @return boolean
  319. */
  320. public function addSoapOperation($binding, $soap_action)
  321. {
  322. if ($soap_action instanceof Zend_Uri_Http) {
  323. $soap_action = $soap_action->getUri();
  324. }
  325. $soap_operation = $this->_dom->createElement('soap:operation');
  326. $soap_operation->setAttribute('soapAction', $soap_action);
  327. $binding->insertBefore($soap_operation, $binding->firstChild);
  328. return $soap_operation;
  329. }
  330. /**
  331. * Add a {@link http://www.w3.org/TR/wsdl#_services service} element to the WSDL
  332. *
  333. * @param string $name Service Name
  334. * @param string $port_name Name of the port for the service
  335. * @param string $binding Binding for the port
  336. * @param string $location SOAP Address for the service
  337. * @return object The new service's XML_Tree_Node for use with {@link function addDocumentation}
  338. */
  339. public function addService($name, $port_name, $binding, $location)
  340. {
  341. if ($location instanceof Zend_Uri_Http) {
  342. $location = $location->getUri();
  343. }
  344. $service = $this->_dom->createElement('service');
  345. $service->setAttribute('name', $name);
  346. $port = $this->_dom->createElement('port');
  347. $port->setAttribute('name', $port_name);
  348. $port->setAttribute('binding', $binding);
  349. $soap_address = $this->_dom->createElement('soap:address');
  350. $soap_address->setAttribute('location', $location);
  351. $port->appendChild($soap_address);
  352. $service->appendChild($port);
  353. $this->_wsdl->appendChild($service);
  354. return $service;
  355. }
  356. /**
  357. * Add a documentation element to any element in the WSDL.
  358. *
  359. * Note that the WSDL {@link http://www.w3.org/TR/wsdl#_documentation specification} uses 'document',
  360. * but the WSDL {@link http://schemas.xmlsoap.org/wsdl/ schema} uses 'documentation' instead.
  361. * 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'.
  362. *
  363. * @param object $input_node An XML_Tree_Node returned by another method to add the documentation to
  364. * @param string $documentation Human readable documentation for the node
  365. * @return DOMElement The documentation element
  366. */
  367. public function addDocumentation($input_node, $documentation)
  368. {
  369. if ($input_node === $this) {
  370. $node = $this->_dom->documentElement;
  371. } else {
  372. $node = $input_node;
  373. }
  374. $doc = $this->_dom->createElement('documentation');
  375. $doc_cdata = $this->_dom->createTextNode($documentation);
  376. $doc->appendChild($doc_cdata);
  377. if($node->hasChildNodes()) {
  378. $node->insertBefore($doc, $node->firstChild);
  379. } else {
  380. $node->appendChild($doc);
  381. }
  382. return $doc;
  383. }
  384. /**
  385. * Add WSDL Types element
  386. *
  387. * @param object $types A DomDocument|DomNode|DomElement|DomDocumentFragment with all the XML Schema types defined in it
  388. */
  389. public function addTypes($types)
  390. {
  391. if ($types instanceof DomDocument) {
  392. $dom = $this->_dom->importNode($types->documentElement);
  393. $this->_wsdl->appendChild($types->documentElement);
  394. } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
  395. $dom = $this->_dom->importNode($types);
  396. $this->_wsdl->appendChild($dom);
  397. }
  398. }
  399. /**
  400. * Add a complex type name that is part of this WSDL and can be used in signatures.
  401. *
  402. * @param string $type
  403. * @return Zend_Soap_Wsdl
  404. */
  405. public function addType($type)
  406. {
  407. if(!in_array($type, $this->_includedTypes)) {
  408. $this->_includedTypes[] = $type;
  409. }
  410. return $this;
  411. }
  412. /**
  413. * Return an array of all currently included complex types
  414. *
  415. * @return array
  416. */
  417. public function getTypes()
  418. {
  419. return $this->_includedTypes;
  420. }
  421. /**
  422. * Return the Schema node of the WSDL
  423. *
  424. * @return DOMElement
  425. */
  426. public function getSchema()
  427. {
  428. if($this->_schema == null) {
  429. $this->addSchemaTypeSection();
  430. }
  431. return $this->_schema;
  432. }
  433. /**
  434. * Return the WSDL as XML
  435. *
  436. * @return string WSDL as XML
  437. */
  438. public function toXML()
  439. {
  440. return $this->_dom->saveXML();
  441. }
  442. /**
  443. * Return DOM Document
  444. *
  445. * @return object DomDocum ent
  446. */
  447. public function toDomDocument()
  448. {
  449. return $this->_dom;
  450. }
  451. /**
  452. * Echo the WSDL as XML
  453. *
  454. * @return boolean
  455. */
  456. public function dump($filename = false)
  457. {
  458. if (!$filename) {
  459. echo $this->toXML();
  460. return true;
  461. } else {
  462. return file_put_contents($filename, $this->toXML());
  463. }
  464. }
  465. /**
  466. * Returns an XSD Type for the given PHP type
  467. *
  468. * @param string $type PHP Type to get the XSD type for
  469. * @return string
  470. */
  471. public function getType($type)
  472. {
  473. switch (strtolower($type)) {
  474. case 'string':
  475. case 'str':
  476. return 'xsd:string';
  477. break;
  478. case 'int':
  479. case 'integer':
  480. return 'xsd:int';
  481. break;
  482. case 'float':
  483. case 'double':
  484. return 'xsd:float';
  485. break;
  486. case 'boolean':
  487. case 'bool':
  488. return 'xsd:boolean';
  489. break;
  490. case 'array':
  491. return 'soap-enc:Array';
  492. break;
  493. case 'object':
  494. return 'xsd:struct';
  495. break;
  496. case 'mixed':
  497. return 'xsd:anyType';
  498. break;
  499. case 'void':
  500. return '';
  501. default:
  502. // delegate retrieval of complex type to current strategy
  503. return $this->addComplexType($type);
  504. }
  505. }
  506. /**
  507. * This function makes sure a complex types section and schema additions are set.
  508. *
  509. * @return Zend_Soap_Wsdl
  510. */
  511. public function addSchemaTypeSection()
  512. {
  513. if ($this->_schema === null) {
  514. $this->_schema = $this->_dom->createElement('xsd:schema');
  515. $this->_schema->setAttribute('targetNamespace', $this->_uri);
  516. $types = $this->_dom->createElement('types');
  517. $types->appendChild($this->_schema);
  518. $this->_wsdl->appendChild($types);
  519. }
  520. return $this;
  521. }
  522. /**
  523. * Add a {@link http://www.w3.org/TR/wsdl#_types types} data type definition
  524. *
  525. * @param string $type Name of the class to be specified
  526. * @return string XSD Type for the given PHP type
  527. */
  528. public function addComplexType($type)
  529. {
  530. if (in_array($type, $this->getTypes())) {
  531. return "tns:$type";
  532. }
  533. $this->addSchemaTypeSection();
  534. $strategy = $this->getComplexTypeStrategy();
  535. $strategy->setContext($this);
  536. // delegates the detection of a complex type to the current strategy
  537. return $strategy->addComplexType($type);
  538. }
  539. /**
  540. * Parse an xsd:element represented as an array into a DOMElement.
  541. *
  542. * @param array $element an xsd:element represented as an array
  543. * @return DOMElement parsed element
  544. */
  545. private function _parseElement($element)
  546. {
  547. if (!is_array($element)) {
  548. require_once "Zend/Soap/Wsdl/Exception.php";
  549. throw new Zend_Soap_Wsdl_Exception("The 'element' parameter needs to be an associative array.");
  550. }
  551. $elementXml = $this->_dom->createElement('xsd:element');
  552. foreach ($element as $key => $value) {
  553. if (in_array($key, array('sequence', 'all', 'choice'))) {
  554. if (is_array($value)) {
  555. $complexType = $this->_dom->createElement('xsd:complexType');
  556. if (count($value) > 0) {
  557. $container = $this->_dom->createElement('xsd:' . $key);
  558. foreach ($value as $subelement) {
  559. $subelementXml = $this->_parseElement($subelement);
  560. $container->appendChild($subelementXml);
  561. }
  562. $complexType->appendChild($container);
  563. }
  564. $elementXml->appendChild($complexType);
  565. }
  566. } else {
  567. $elementXml->setAttribute($key, $value);
  568. }
  569. }
  570. return $elementXml;
  571. }
  572. /**
  573. * Add an xsd:element represented as an array to the schema.
  574. *
  575. * Array keys represent attribute names and values their respective value.
  576. * The 'sequence', 'all' and 'choice' keys must have an array of elements as their value,
  577. * to add them to a nested complexType.
  578. *
  579. * Example: array( 'name' => 'MyElement',
  580. * 'sequence' => array( array('name' => 'myString', 'type' => 'string'),
  581. * array('name' => 'myInteger', 'type' => 'int') ) );
  582. * Resulting XML: <xsd:element name="MyElement"><xsd:complexType><xsd:sequence>
  583. * <xsd:element name="myString" type="string"/>
  584. * <xsd:element name="myInteger" type="int"/>
  585. * </xsd:sequence></xsd:complexType></xsd:element>
  586. *
  587. * @param array $element an xsd:element represented as an array
  588. * @return string xsd:element for the given element array
  589. */
  590. public function addElement($element)
  591. {
  592. $schema = $this->getSchema();
  593. $elementXml = $this->_parseElement($element);
  594. $schema->appendChild($elementXml);
  595. return 'tns:' . $element['name'];
  596. }
  597. }