Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AutoDiscover.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Server/Interface.php';
27 
30 #require_once 'Zend/Soap/Wsdl.php';
31 
34 #require_once 'Zend/Server/Reflection.php';
35 
38 #require_once 'Zend/Server/Abstract.php';
39 
42 #require_once 'Zend/Uri.php';
43 
52 {
56  protected $_wsdl = null;
57 
61  protected $_reflection = null;
62 
66  protected $_functions = array();
67 
71  protected $_strategy;
72 
78  protected $_uri;
79 
85  protected $_operationBodyStyle = array('use' => 'encoded', 'encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/");
86 
92  protected $_bindingStyle = array('style' => 'rpc', 'transport' => 'http://schemas.xmlsoap.org/soap/http');
93 
99  protected $_wsdlClass = 'Zend_Soap_Wsdl';
100 
108  public function __construct($strategy = true, $uri=null, $wsdlClass=null)
109  {
110  $this->_reflection = new Zend_Server_Reflection();
111  $this->setComplexTypeStrategy($strategy);
112 
113  if($uri !== null) {
114  $this->setUri($uri);
115  }
116 
117  if($wsdlClass !== null) {
118  $this->setWsdlClass($wsdlClass);
119  }
120  }
121 
130  public function setUri($uri)
131  {
132  if (!is_string($uri) && !($uri instanceof Zend_Uri)) {
133  #require_once "Zend/Soap/AutoDiscover/Exception.php";
134  throw new Zend_Soap_AutoDiscover_Exception("No uri given to Zend_Soap_AutoDiscover::setUri as string or Zend_Uri instance.");
135  }
136  $this->_uri = $uri;
137 
138  // change uri in WSDL file also if existant
139  if ($this->_wsdl instanceof Zend_Soap_Wsdl) {
140  $this->_wsdl->setUri($uri);
141  }
142 
143  return $this;
144  }
145 
151  public function getUri()
152  {
153  if($this->_uri !== null) {
154  $uri = $this->_uri;
155  } else {
156  $schema = $this->getSchema();
157  $host = $this->getHostName();
158  $scriptName = $this->getRequestUriWithoutParameters();
159  $uri = Zend_Uri::factory($schema . '://' . $host . $scriptName);
160  $this->setUri($uri);
161  }
162  return $uri;
163  }
164 
174  public function setWsdlClass($wsdlClass)
175  {
176  if (!is_string($wsdlClass) && !is_subclass_of($wsdlClass, 'Zend_Soap_Wsdl')) {
177  #require_once "Zend/Soap/AutoDiscover/Exception.php";
178  throw new Zend_Soap_AutoDiscover_Exception("No Zend_Soap_Wsdl subclass given to Zend_Soap_AutoDiscover::setWsdlClass as string.");
179  }
180  $this->_wsdlClass = $wsdlClass;
181 
182  return $this;
183  }
184 
190  public function getWsdlClass()
191  {
192  return $this->_wsdlClass;
193  }
194 
206  public function setOperationBodyStyle(array $operationStyle=array())
207  {
208  if(!isset($operationStyle['use'])) {
209  #require_once "Zend/Soap/AutoDiscover/Exception.php";
210  throw new Zend_Soap_AutoDiscover_Exception("Key 'use' is required in Operation soap:body style.");
211  }
212  $this->_operationBodyStyle = $operationStyle;
213  return $this;
214  }
215 
224  public function setBindingStyle(array $bindingStyle=array())
225  {
226  if(isset($bindingStyle['style'])) {
227  $this->_bindingStyle['style'] = $bindingStyle['style'];
228  }
229  if(isset($bindingStyle['transport'])) {
230  $this->_bindingStyle['transport'] = $bindingStyle['transport'];
231  }
232  return $this;
233  }
234 
240  protected function getSchema()
241  {
242  $schema = "http";
243  if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
244  $schema = 'https';
245  }
246  return $schema;
247  }
248 
254  protected function getHostName()
255  {
256  if(isset($_SERVER['HTTP_HOST'])) {
257  $host = $_SERVER['HTTP_HOST'];
258  } else {
259  $host = $_SERVER['SERVER_NAME'];
260  }
261  return $host;
262  }
263 
269  protected function getRequestUriWithoutParameters()
270  {
271  if (isset($_SERVER['HTTP_X_ORIGINAL_URL'])) { // IIS with Microsoft Rewrite Module
272  $requestUri = $_SERVER['HTTP_X_ORIGINAL_URL'];
273  } elseif (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // check this first so IIS will catch
274  $requestUri = $_SERVER['HTTP_X_REWRITE_URL'];
275  } elseif (isset($_SERVER['REQUEST_URI'])) {
276  $requestUri = $_SERVER['REQUEST_URI'];
277  } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
278  $requestUri = $_SERVER['ORIG_PATH_INFO'];
279  } else {
280  $requestUri = $_SERVER['SCRIPT_NAME'];
281  }
282  if( ($pos = strpos($requestUri, "?")) !== false) {
283  $requestUri = substr($requestUri, 0, $pos);
284  }
285 
286  return $requestUri;
287  }
288 
295  public function setComplexTypeStrategy($strategy)
296  {
297  $this->_strategy = $strategy;
298  if($this->_wsdl instanceof Zend_Soap_Wsdl) {
299  $this->_wsdl->setComplexTypeStrategy($strategy);
300  }
301 
302  return $this;
303  }
304 
313  public function setClass($class, $namespace = '', $argv = null)
314  {
315  $uri = $this->getUri();
316 
317  $wsdl = new $this->_wsdlClass($class, $uri, $this->_strategy);
318 
319  // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
320  $wsdl->addSchemaTypeSection();
321 
322  $port = $wsdl->addPortType($class . 'Port');
323  $binding = $wsdl->addBinding($class . 'Binding', 'tns:' .$class. 'Port');
324 
325  $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
326  $wsdl->addService($class . 'Service', $class . 'Port', 'tns:' . $class . 'Binding', $uri);
327  foreach ($this->_reflection->reflectClass($class)->getMethods() as $method) {
328  $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
329  }
330  $this->_wsdl = $wsdl;
331 
332  return $this;
333  }
334 
342  public function addFunction($function, $namespace = '')
343  {
344  static $port;
345  static $operation;
346  static $binding;
347 
348  if (!is_array($function)) {
349  $function = (array) $function;
350  }
351 
352  $uri = $this->getUri();
353 
354  if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
355  $parts = explode('.', basename($_SERVER['SCRIPT_NAME']));
356  $name = $parts[0];
357  $wsdl = new Zend_Soap_Wsdl($name, $uri, $this->_strategy);
358 
359  // The wsdl:types element must precede all other elements (WS-I Basic Profile 1.1 R2023)
360  $wsdl->addSchemaTypeSection();
361 
362  $port = $wsdl->addPortType($name . 'Port');
363  $binding = $wsdl->addBinding($name . 'Binding', 'tns:' .$name. 'Port');
364 
365  $wsdl->addSoapBinding($binding, $this->_bindingStyle['style'], $this->_bindingStyle['transport']);
366  $wsdl->addService($name . 'Service', $name . 'Port', 'tns:' . $name . 'Binding', $uri);
367  } else {
368  $wsdl = $this->_wsdl;
369  }
370 
371  foreach ($function as $func) {
372  $method = $this->_reflection->reflectFunction($func);
373  $this->_addFunctionToWsdl($method, $wsdl, $port, $binding);
374  }
375  $this->_wsdl = $wsdl;
376 
377  return $this;
378  }
379 
389  protected function _addFunctionToWsdl($function, $wsdl, $port, $binding)
390  {
391  $uri = $this->getUri();
392 
393  // We only support one prototype: the one with the maximum number of arguments
394  $prototype = null;
395  $maxNumArgumentsOfPrototype = -1;
396  foreach ($function->getPrototypes() as $tmpPrototype) {
397  $numParams = count($tmpPrototype->getParameters());
398  if ($numParams > $maxNumArgumentsOfPrototype) {
399  $maxNumArgumentsOfPrototype = $numParams;
400  $prototype = $tmpPrototype;
401  }
402  }
403  if ($prototype === null) {
404  #require_once "Zend/Soap/AutoDiscover/Exception.php";
405  throw new Zend_Soap_AutoDiscover_Exception("No prototypes could be found for the '" . $function->getName() . "' function");
406  }
407 
408  // Add the input message (parameters)
409  $args = array();
410  if ($this->_bindingStyle['style'] == 'document') {
411  // Document style: wrap all parameters in a sequence element
412  $sequence = array();
413  foreach ($prototype->getParameters() as $param) {
414  $sequenceElement = array(
415  'name' => $param->getName(),
416  'type' => $wsdl->getType($param->getType())
417  );
418  if ($param->isOptional()) {
419  $sequenceElement['nillable'] = 'true';
420  }
421  $sequence[] = $sequenceElement;
422  }
423  $element = array(
424  'name' => $function->getName(),
425  'sequence' => $sequence
426  );
427  // Add the wrapper element part, which must be named 'parameters'
428  $args['parameters'] = array('element' => $wsdl->addElement($element));
429  } else {
430  // RPC style: add each parameter as a typed part
431  foreach ($prototype->getParameters() as $param) {
432  $args[$param->getName()] = array('type' => $wsdl->getType($param->getType()));
433  }
434  }
435  $wsdl->addMessage($function->getName() . 'In', $args);
436 
437  $isOneWayMessage = false;
438  if($prototype->getReturnType() == "void") {
439  $isOneWayMessage = true;
440  }
441 
442  if($isOneWayMessage == false) {
443  // Add the output message (return value)
444  $args = array();
445  if ($this->_bindingStyle['style'] == 'document') {
446  // Document style: wrap the return value in a sequence element
447  $sequence = array();
448  if ($prototype->getReturnType() != "void") {
449  $sequence[] = array(
450  'name' => $function->getName() . 'Result',
451  'type' => $wsdl->getType($prototype->getReturnType())
452  );
453  }
454  $element = array(
455  'name' => $function->getName() . 'Response',
456  'sequence' => $sequence
457  );
458  // Add the wrapper element part, which must be named 'parameters'
459  $args['parameters'] = array('element' => $wsdl->addElement($element));
460  } else if ($prototype->getReturnType() != "void") {
461  // RPC style: add the return value as a typed part
462  $args['return'] = array('type' => $wsdl->getType($prototype->getReturnType()));
463  }
464  $wsdl->addMessage($function->getName() . 'Out', $args);
465  }
466 
467  // Add the portType operation
468  if($isOneWayMessage == false) {
469  $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', 'tns:' . $function->getName() . 'Out');
470  } else {
471  $portOperation = $wsdl->addPortOperation($port, $function->getName(), 'tns:' . $function->getName() . 'In', false);
472  }
473  $desc = $function->getDescription();
474  if (strlen($desc) > 0) {
475  $wsdl->addDocumentation($portOperation, $desc);
476  }
477 
478  // When using the RPC style, make sure the operation style includes a 'namespace' attribute (WS-I Basic Profile 1.1 R2717)
479  if ($this->_bindingStyle['style'] == 'rpc' && !isset($this->_operationBodyStyle['namespace'])) {
480  $this->_operationBodyStyle['namespace'] = ''.$uri;
481  }
482 
483  // Add the binding operation
484  if($isOneWayMessage == false) {
485  $operation = $wsdl->addBindingOperation($binding, $function->getName(), $this->_operationBodyStyle, $this->_operationBodyStyle);
486  } else {
487  $operation = $wsdl->addBindingOperation($binding, $function->getName(), $this->_operationBodyStyle);
488  }
489  $wsdl->addSoapOperation($operation, $uri . '#' .$function->getName());
490 
491  // Add the function name to the list
492  $this->_functions[] = $function->getName();
493  }
494 
502  public function fault($fault = null, $code = null)
503  {
504  #require_once "Zend/Soap/AutoDiscover/Exception.php";
505  throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
506  }
507 
513  public function handle($request = false)
514  {
515  if (!headers_sent()) {
516  header('Content-Type: text/xml');
517  }
518  $this->_wsdl->dump();
519  }
520 
528  public function dump($filename)
529  {
530  if($this->_wsdl !== null) {
531  return $this->_wsdl->dump($filename);
532  } else {
536  #require_once "Zend/Soap/AutoDiscover/Exception.php";
537  throw new Zend_Soap_AutoDiscover_Exception("Cannot dump autodiscovered contents, WSDL file has not been generated yet.");
538  }
539  }
540 
547  public function toXml()
548  {
549  if($this->_wsdl !== null) {
550  return $this->_wsdl->toXml();
551  } else {
555  #require_once "Zend/Soap/AutoDiscover/Exception.php";
556  throw new Zend_Soap_AutoDiscover_Exception("Cannot return autodiscovered contents, WSDL file has not been generated yet.");
557  }
558  }
559 
565  public function getFunctions()
566  {
567  return $this->_functions;
568  }
569 
576  public function loadFunctions($definition)
577  {
578  #require_once "Zend/Soap/AutoDiscover/Exception.php";
579  throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
580  }
581 
588  public function setPersistence($mode)
589  {
590  #require_once "Zend/Soap/AutoDiscover/Exception.php";
591  throw new Zend_Soap_AutoDiscover_Exception("Function has no use in AutoDiscover.");
592  }
593 
600  public function getType($type)
601  {
602  if (!($this->_wsdl instanceof Zend_Soap_Wsdl)) {
605  // WSDL is not defined yet, so we can't recognize type in context of current service
606  return '';
607  } else {
608  return $this->_wsdl->getType($type);
609  }
610  }
611 }
loadFunctions($definition)
is_subclass_of($obj, $className)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
static factory($uri='http', $className=null)
Definition: Uri.php:96
setBindingStyle(array $bindingStyle=array())
setOperationBodyStyle(array $operationStyle=array())
handle($request=false)
_addFunctionToWsdl($function, $wsdl, $port, $binding)
$type
Definition: item.phtml:13
__construct($strategy=true, $uri=null, $wsdlClass=null)
addFunction($function, $namespace='')
$_option $_optionId $class
Definition: date.phtml:13
$pos
Definition: list.phtml:42
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$method
Definition: info.phtml:13
setClass($class, $namespace='', $argv=null)
setComplexTypeStrategy($strategy)
fault($fault=null, $code=null)
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14
$element
Definition: element.phtml:12