Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Wsdl.php
Go to the documentation of this file.
1 <?php
25 #require_once "Zend/Soap/Wsdl/Strategy/Interface.php";
26 
30 #require_once "Zend/Soap/Wsdl/Strategy/Abstract.php";
31 
33 #require_once "Zend/Xml/Security.php";
34 
42 {
46  private $_dom;
47 
51  private $_wsdl;
52 
56  private $_uri;
57 
61  private $_schema = null;
62 
68  private $_includedTypes = array();
69 
73  protected $_strategy = null;
74 
75 
83  public function __construct($name, $uri, $strategy = true)
84  {
85  if ($uri instanceof Zend_Uri_Http) {
86  $uri = $uri->getUri();
87  }
88  $this->_uri = $uri;
89 
94  $wsdl = "<?xml version='1.0' ?>
95  <definitions name='$name' targetNamespace='$uri'
96  xmlns='http://schemas.xmlsoap.org/wsdl/'
97  xmlns:tns='$uri'
98  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/'
99  xmlns:xsd='http://www.w3.org/2001/XMLSchema'
100  xmlns:soap-enc='http://schemas.xmlsoap.org/soap/encoding/'
101  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/'></definitions>";
102  $this->_dom = new DOMDocument();
103  if (!$this->_dom = Zend_Xml_Security::scan($wsdl, $this->_dom)) {
104  #require_once 'Zend/Server/Exception.php';
105  throw new Zend_Server_Exception('Unable to create DomDocument');
106  }
107  $this->_wsdl = $this->_dom->documentElement;
108 
109  $this->setComplexTypeStrategy($strategy);
110  }
111 
118  public function setUri($uri)
119  {
120  if ($uri instanceof Zend_Uri_Http) {
121  $uri = $uri->getUri();
122  }
123  $oldUri = $this->_uri;
124  $this->_uri = $uri;
125 
126  if($this->_dom !== null) {
127  // @todo: This is the worst hack ever, but its needed due to design and non BC issues of WSDL generation
128  $xml = $this->_dom->saveXML();
129  $xml = str_replace($oldUri, $uri, $xml);
130  $this->_dom = new DOMDocument();
131  $this->_dom = Zend_Xml_Security::scan($xml, $this->_dom);
132  }
133 
134  return $this;
135  }
136 
144  public function setComplexTypeStrategy($strategy)
145  {
146  if($strategy === true) {
147  #require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
149  } else if($strategy === false) {
150  #require_once "Zend/Soap/Wsdl/Strategy/AnyType.php";
151  $strategy = new Zend_Soap_Wsdl_Strategy_AnyType();
152  } else if(is_string($strategy)) {
153  if(class_exists($strategy)) {
154  $strategy = new $strategy();
155  } else {
156  #require_once "Zend/Soap/Wsdl/Exception.php";
157  throw new Zend_Soap_Wsdl_Exception(
158  sprintf("Strategy with name '%s does not exist.", $strategy
159  ));
160  }
161  }
162 
163  if(!($strategy instanceof Zend_Soap_Wsdl_Strategy_Interface)) {
164  #require_once "Zend/Soap/Wsdl/Exception.php";
165  throw new Zend_Soap_Wsdl_Exception("Set a strategy that is not of type 'Zend_Soap_Wsdl_Strategy_Interface'");
166  }
167  $this->_strategy = $strategy;
168  return $this;
169  }
170 
176  public function getComplexTypeStrategy()
177  {
178  return $this->_strategy;
179  }
180 
191  public function addMessage($name, $parts)
192  {
193  $message = $this->_dom->createElement('message');
194 
195  $message->setAttribute('name', $name);
196 
197  if (sizeof($parts) > 0) {
198  foreach ($parts as $name => $type) {
199  $part = $this->_dom->createElement('part');
200  $part->setAttribute('name', $name);
201  if (is_array($type)) {
202  foreach ($type as $key => $value) {
203  $part->setAttribute($key, $value);
204  }
205  } else {
206  $part->setAttribute('type', $type);
207  }
208  $message->appendChild($part);
209  }
210  }
211 
212  $this->_wsdl->appendChild($message);
213 
214  return $message;
215  }
216 
223  public function addPortType($name)
224  {
225  $portType = $this->_dom->createElement('portType');
226  $portType->setAttribute('name', $name);
227  $this->_wsdl->appendChild($portType);
228 
229  return $portType;
230  }
231 
242  public function addPortOperation($portType, $name, $input = false, $output = false, $fault = false)
243  {
244  $operation = $this->_dom->createElement('operation');
245  $operation->setAttribute('name', $name);
246 
247  if (is_string($input) && (strlen(trim($input)) >= 1)) {
248  $node = $this->_dom->createElement('input');
249  $node->setAttribute('message', $input);
250  $operation->appendChild($node);
251  }
252  if (is_string($output) && (strlen(trim($output)) >= 1)) {
253  $node= $this->_dom->createElement('output');
254  $node->setAttribute('message', $output);
255  $operation->appendChild($node);
256  }
257  if (is_string($fault) && (strlen(trim($fault)) >= 1)) {
258  $node = $this->_dom->createElement('fault');
259  $node->setAttribute('message', $fault);
260  $operation->appendChild($node);
261  }
262 
263  $portType->appendChild($operation);
264 
265  return $operation;
266  }
267 
275  public function addBinding($name, $portType)
276  {
277  $binding = $this->_dom->createElement('binding');
278  $binding->setAttribute('name', $name);
279  $binding->setAttribute('type', $portType);
280 
281  $this->_wsdl->appendChild($binding);
282 
283  return $binding;
284  }
285 
295  public function addBindingOperation($binding, $name, $input = false, $output = false, $fault = false)
296  {
297  $operation = $this->_dom->createElement('operation');
298  $operation->setAttribute('name', $name);
299 
300  if (is_array($input)) {
301  $node = $this->_dom->createElement('input');
302  $soap_node = $this->_dom->createElement('soap:body');
303  foreach ($input as $name => $value) {
304  $soap_node->setAttribute($name, $value);
305  }
306  $node->appendChild($soap_node);
307  $operation->appendChild($node);
308  }
309 
310  if (is_array($output)) {
311  $node = $this->_dom->createElement('output');
312  $soap_node = $this->_dom->createElement('soap:body');
313  foreach ($output as $name => $value) {
314  $soap_node->setAttribute($name, $value);
315  }
316  $node->appendChild($soap_node);
317  $operation->appendChild($node);
318  }
319 
320  if (is_array($fault)) {
321  $node = $this->_dom->createElement('fault');
327  if (isset($fault['name'])) {
328  $node->setAttribute('name', $fault['name']);
329  }
330 
331  $soap_node = $this->_dom->createElement('soap:fault');
332  foreach ($fault as $name => $value) {
333  $soap_node->setAttribute($name, $value);
334  }
335  $node->appendChild($soap_node);
336  $operation->appendChild($node);
337  }
338 
339  $binding->appendChild($operation);
340 
341  return $operation;
342  }
343 
352  public function addSoapBinding($binding, $style = 'document', $transport = 'http://schemas.xmlsoap.org/soap/http')
353  {
354  $soap_binding = $this->_dom->createElement('soap:binding');
355  $soap_binding->setAttribute('style', $style);
356  $soap_binding->setAttribute('transport', $transport);
357 
358  $binding->appendChild($soap_binding);
359 
360  return $soap_binding;
361  }
362 
370  public function addSoapOperation($binding, $soap_action)
371  {
372  if ($soap_action instanceof Zend_Uri_Http) {
373  $soap_action = $soap_action->getUri();
374  }
375  $soap_operation = $this->_dom->createElement('soap:operation');
376  $soap_operation->setAttribute('soapAction', $soap_action);
377 
378  $binding->insertBefore($soap_operation, $binding->firstChild);
379 
380  return $soap_operation;
381  }
382 
392  public function addService($name, $port_name, $binding, $location)
393  {
394  if ($location instanceof Zend_Uri_Http) {
395  $location = $location->getUri();
396  }
397  $service = $this->_dom->createElement('service');
398  $service->setAttribute('name', $name);
399 
400  $port = $this->_dom->createElement('port');
401  $port->setAttribute('name', $port_name);
402  $port->setAttribute('binding', $binding);
403 
404  $soap_address = $this->_dom->createElement('soap:address');
405  $soap_address->setAttribute('location', $location);
406 
407  $port->appendChild($soap_address);
408  $service->appendChild($port);
409 
410  $this->_wsdl->appendChild($service);
411 
412  return $service;
413  }
414 
426  public function addDocumentation($input_node, $documentation)
427  {
428  if ($input_node === $this) {
429  $node = $this->_dom->documentElement;
430  } else {
431  $node = $input_node;
432  }
433 
434  $doc = $this->_dom->createElement('documentation');
435  $doc_cdata = $this->_dom->createTextNode(str_replace(array("\r\n", "\r"), "\n", $documentation));
436  $doc->appendChild($doc_cdata);
437 
438  if($node->hasChildNodes()) {
439  $node->insertBefore($doc, $node->firstChild);
440  } else {
441  $node->appendChild($doc);
442  }
443 
444  return $doc;
445  }
446 
452  public function addTypes($types)
453  {
454  if ($types instanceof DomDocument) {
455  $dom = $this->_dom->importNode($types->documentElement);
456  $this->_wsdl->appendChild($types->documentElement);
457  } elseif ($types instanceof DomNode || $types instanceof DomElement || $types instanceof DomDocumentFragment ) {
458  $dom = $this->_dom->importNode($types);
459  $this->_wsdl->appendChild($dom);
460  }
461  }
462 
469  public function addType($type)
470  {
471  if(!in_array($type, $this->_includedTypes)) {
472  $this->_includedTypes[] = $type;
473  }
474  return $this;
475  }
476 
482  public function getTypes()
483  {
484  return $this->_includedTypes;
485  }
486 
492  public function getSchema()
493  {
494  if($this->_schema == null) {
495  $this->addSchemaTypeSection();
496  }
497 
498  return $this->_schema;
499  }
500 
506  public function toXML()
507  {
508  return $this->_dom->saveXML();
509  }
510 
516  public function toDomDocument()
517  {
518  return $this->_dom;
519  }
520 
526  public function dump($filename = false)
527  {
528  if (!$filename) {
529  echo $this->toXML();
530  return true;
531  } else {
532  return file_put_contents($filename, $this->toXML());
533  }
534  }
535 
542  public function getType($type)
543  {
544  switch (strtolower($type)) {
545  case 'string':
546  case 'str':
547  return 'xsd:string';
548  case 'long':
549  return 'xsd:long';
550  case 'int':
551  case 'integer':
552  return 'xsd:int';
553  case 'float':
554  return 'xsd:float';
555  case 'double':
556  return 'xsd:double';
557  case 'boolean':
558  case 'bool':
559  return 'xsd:boolean';
560  case 'array':
561  return 'soap-enc:Array';
562  case 'object':
563  return 'xsd:struct';
564  case 'mixed':
565  return 'xsd:anyType';
566  case 'void':
567  return '';
568  default:
569  // delegate retrieval of complex type to current strategy
570  return $this->addComplexType($type);
571  }
572  }
573 
579  public function addSchemaTypeSection()
580  {
581  if ($this->_schema === null) {
582  $this->_schema = $this->_dom->createElement('xsd:schema');
583  $this->_schema->setAttribute('targetNamespace', $this->_uri);
584  $types = $this->_dom->createElement('types');
585  $types->appendChild($this->_schema);
586  $this->_wsdl->appendChild($types);
587  }
588  return $this;
589  }
590 
597  public function addComplexType($type)
598  {
599  if (in_array($type, $this->getTypes())) {
600  return "tns:$type";
601  }
602  $this->addSchemaTypeSection();
603 
604  $strategy = $this->getComplexTypeStrategy();
605  $strategy->setContext($this);
606  // delegates the detection of a complex type to the current strategy
607  return $strategy->addComplexType($type);
608  }
609 
616  private function _parseElement($element)
617  {
618  if (!is_array($element)) {
619  #require_once "Zend/Soap/Wsdl/Exception.php";
620  throw new Zend_Soap_Wsdl_Exception("The 'element' parameter needs to be an associative array.");
621  }
622 
623  $elementXml = $this->_dom->createElement('xsd:element');
624  foreach ($element as $key => $value) {
625  if (in_array($key, array('sequence', 'all', 'choice'))) {
626  if (is_array($value)) {
627  $complexType = $this->_dom->createElement('xsd:complexType');
628  if (count($value) > 0) {
629  $container = $this->_dom->createElement('xsd:' . $key);
630  foreach ($value as $subelement) {
631  $subelementXml = $this->_parseElement($subelement);
632  $container->appendChild($subelementXml);
633  }
634  $complexType->appendChild($container);
635  }
636  $elementXml->appendChild($complexType);
637  }
638  } else {
639  $elementXml->setAttribute($key, $value);
640  }
641  }
642  return $elementXml;
643  }
644 
663  public function addElement($element)
664  {
665  $schema = $this->getSchema();
666  $elementXml = $this->_parseElement($element);
667  $schema->appendChild($elementXml);
668  return 'tns:' . $element['name'];
669  }
670 }
addService($name, $port_name, $binding, $location)
Definition: Wsdl.php:392
addTypes($types)
Definition: Wsdl.php:452
addComplexType($type)
Definition: Wsdl.php:597
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
addPortType($name)
Definition: Wsdl.php:223
__construct($name, $uri, $strategy=true)
Definition: Wsdl.php:83
static scan($xml, DOMDocument $dom=null)
Definition: Security.php:71
addSoapOperation($binding, $soap_action)
Definition: Wsdl.php:370
addBindingOperation($binding, $name, $input=false, $output=false, $fault=false)
Definition: Wsdl.php:295
addSoapBinding($binding, $style='document', $transport='http://schemas.xmlsoap.org/soap/http')
Definition: Wsdl.php:352
$message
addElement($element)
Definition: Wsdl.php:663
$type
Definition: item.phtml:13
addType($type)
Definition: Wsdl.php:469
dump($filename=false)
Definition: Wsdl.php:526
addDocumentation($input_node, $documentation)
Definition: Wsdl.php:426
$value
Definition: gender.phtml:16
addPortOperation($portType, $name, $input=false, $output=false, $fault=false)
Definition: Wsdl.php:242
addSchemaTypeSection()
Definition: Wsdl.php:579
addBinding($name, $portType)
Definition: Wsdl.php:275
setComplexTypeStrategy($strategy)
Definition: Wsdl.php:144
toDomDocument()
Definition: Wsdl.php:516
getType($type)
Definition: Wsdl.php:542
getComplexTypeStrategy()
Definition: Wsdl.php:176
setUri($uri)
Definition: Wsdl.php:118
addMessage($name, $parts)
Definition: Wsdl.php:191
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12