Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Generator.php
Go to the documentation of this file.
1 <?php
9 
17 
22 {
24  const WSDL_NAME = 'MagentoWSDL';
25 
31  protected $_wsdlFactory;
32 
43  public function __construct(
44  \Magento\Webapi\Model\Cache\Type\Webapi $cache,
45  \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
46  \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface $serviceTypeList,
49  WsdlFactory $wsdlFactory
50  ) {
51  $this->_wsdlFactory = $wsdlFactory;
52  parent::__construct(
53  $cache,
58  );
59  }
60 
64  protected function generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $endPointUrl)
65  {
66  $wsdl = $this->_wsdlFactory->create(self::WSDL_NAME, $endPointUrl);
67  $wsdl->addSchemaTypeSection();
68  $faultMessageName = $this->_addGenericFaultComplexTypeNodes($wsdl);
69  $wsdl = $this->addCustomAttributeTypes($wsdl);
70 
71  foreach ($requestedServiceMetadata as $serviceClass => &$serviceData) {
72  $portTypeName = $this->getPortTypeName($serviceClass);
73  $bindingName = $this->getBindingName($serviceClass);
74  $portType = $wsdl->addPortType($portTypeName);
75  $binding = $wsdl->addBinding($bindingName, Wsdl::TYPES_NS . ':' . $portTypeName);
76  $wsdl->addSoapBinding($binding, 'document', 'http://schemas.xmlsoap.org/soap/http', SOAP_1_2);
77  $portName = $this->getPortName($serviceClass);
78  $serviceName = $this->getServiceName($serviceClass);
79  $wsdl->addService($serviceName, $portName, Wsdl::TYPES_NS . ':' . $bindingName, $endPointUrl, SOAP_1_2);
80 
81  foreach ($serviceData[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
82  $operationName = $this->typeProcessor->getOperationName($serviceClass, $methodName);
83  $bindingDataPrototype = ['use' => 'literal'];
84  $inputBinding = $bindingDataPrototype;
85  $inputMessageName = $this->_createOperationInput($wsdl, $operationName, $methodData);
86 
87  $outputMessageName = false;
88  $outputBinding = false;
89  if (isset($methodData['interface']['out']['parameters'])) {
90  $outputBinding = $bindingDataPrototype;
91  $outputMessageName = $this->_createOperationOutput($wsdl, $operationName, $methodData);
92  }
93  $faultBinding = ['name' => Fault::NODE_DETAIL_WRAPPER];
94 
95  $wsdl->addPortOperation(
96  $portType,
97  $operationName,
98  $inputMessageName,
99  $outputMessageName,
100  ['message' => $faultMessageName, 'name' => Fault::NODE_DETAIL_WRAPPER]
101  );
102  $bindingOperation = $wsdl->addBindingOperation(
103  $binding,
104  $operationName,
105  $inputBinding,
106  $outputBinding,
107  $faultBinding,
108  SOAP_1_2
109  );
110  $wsdl->addSoapOperation($bindingOperation, $operationName, SOAP_1_2);
111  }
112  }
113  return $wsdl->toXML();
114  }
115 
122  protected function addCustomAttributeTypes($wsdl)
123  {
124  foreach ($this->serviceTypeList->getDataTypes() as $customAttributeClass) {
125  $typeName = $this->typeProcessor->register($customAttributeClass);
126  $wsdl->addComplexType($this->typeProcessor->getArrayItemType($typeName));
127  }
128  return $wsdl;
129  }
130 
139  protected function _createOperationInput(Wsdl $wsdl, $operationName, $methodData)
140  {
141  $inputMessageName = $this->getInputMessageName($operationName);
142  $complexTypeName = $this->getElementComplexTypeName($inputMessageName);
143  $inputParameters = [];
144  $elementData = [
145  'name' => $inputMessageName,
146  'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
147  ];
148  if (isset($methodData['interface']['in']['parameters'])) {
149  $inputParameters = $methodData['interface']['in']['parameters'];
150  } else {
151  $elementData['nillable'] = 'true';
152  }
153  $wsdl->addElement($elementData);
154  $callInfo = [];
155  $callInfo['requiredInput']['yes']['calls'] = [$operationName];
156  $typeData = [
157  'documentation' => $methodData['documentation'],
158  'parameters' => $inputParameters,
159  'callInfo' => $callInfo,
160  ];
161  $this->typeProcessor->setTypeData($complexTypeName, $typeData);
162  $wsdl->addComplexType($complexTypeName);
163  $wsdl->addMessage(
164  $inputMessageName,
165  [
166  'messageParameters' => [
167  'element' => Wsdl::TYPES_NS . ':' . $inputMessageName,
168  ]
169  ]
170  );
171  return Wsdl::TYPES_NS . ':' . $inputMessageName;
172  }
173 
182  protected function _createOperationOutput(Wsdl $wsdl, $operationName, $methodData)
183  {
184  $outputMessageName = $this->getOutputMessageName($operationName);
185  $complexTypeName = $this->getElementComplexTypeName($outputMessageName);
186  $wsdl->addElement(
187  [
188  'name' => $outputMessageName,
189  'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
190  ]
191  );
192  $callInfo = [];
193  $callInfo['returned']['always']['calls'] = [$operationName];
194  $typeData = [
195  'documentation' => sprintf('Response container for the %s call.', $operationName),
196  'parameters' => $methodData['interface']['out']['parameters'],
197  'callInfo' => $callInfo,
198  ];
199  $this->typeProcessor->setTypeData($complexTypeName, $typeData);
200  $wsdl->addComplexType($complexTypeName);
201  $wsdl->addMessage(
202  $outputMessageName,
203  [
204  'messageParameters' => [
205  'element' => Wsdl::TYPES_NS . ':' . $outputMessageName,
206  ]
207  ]
208  );
209  return Wsdl::TYPES_NS . ':' . $outputMessageName;
210  }
211 
218  public function getPortTypeName($serviceName)
219  {
220  return $serviceName . 'PortType';
221  }
222 
229  public function getBindingName($serviceName)
230  {
231  return $serviceName . 'Binding';
232  }
233 
240  public function getPortName($serviceName)
241  {
242  return $serviceName . 'Port';
243  }
244 
251  public function getServiceName($serviceName)
252  {
253  return $serviceName . 'Service';
254  }
255 
262  public function getInputMessageName($operationName)
263  {
264  return $operationName . 'Request';
265  }
266 
273  public function getOutputMessageName($operationName)
274  {
275  return $operationName . 'Response';
276  }
277 
284  protected function _addGenericFaultComplexTypeNodes($wsdl)
285  {
286  $faultMessageName = Fault::NODE_DETAIL_WRAPPER;
287  $complexTypeName = $this->getElementComplexTypeName($faultMessageName);
288  $wsdl->addElement(
289  [
290  'name' => $faultMessageName,
291  'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
292  ]
293  );
294  $faultParamsComplexType = Fault::NODE_DETAIL_PARAMETER;
295  $faultParamsData = [
296  'parameters' => [
298  'type' => 'string',
299  'required' => true,
300  'documentation' => '',
301  ],
303  'type' => 'string',
304  'required' => true,
305  'documentation' => '',
306  ],
307  ],
308  ];
309  $wrappedErrorComplexType = Fault::NODE_DETAIL_WRAPPED_ERROR;
310  $wrappedErrorData = [
311  'parameters' => [
313  'type' => 'string',
314  'required' => true,
315  'documentation' => '',
316  ],
318  'type' => "{$faultParamsComplexType}[]",
319  'required' => false,
320  'documentation' => 'Message parameters.',
321  ],
322  ],
323  ];
324  $genericFaultTypeData = [
325  'parameters' => [
327  'type' => 'string',
328  'required' => false,
329  'documentation' => 'Exception calls stack trace.',
330  ],
332  'type' => "{$faultParamsComplexType}[]",
333  'required' => false,
334  'documentation' => 'Additional exception parameters.',
335  ],
337  'type' => "{$wrappedErrorComplexType}[]",
338  'required' => false,
339  'documentation' => 'Additional wrapped errors.',
340  ],
341  ],
342  ];
343  $this->typeProcessor->setTypeData($faultParamsComplexType, $faultParamsData);
344  $this->typeProcessor->setTypeData($wrappedErrorComplexType, $wrappedErrorData);
345  $this->typeProcessor->setTypeData($complexTypeName, $genericFaultTypeData);
346  $wsdl->addComplexType($complexTypeName);
347  $wsdl->addMessage(
348  $faultMessageName,
349  [
350  'messageParameters' => [
351  'element' => Wsdl::TYPES_NS . ':' . $faultMessageName,
352  ]
353  ]
354  );
355 
356  return Wsdl::TYPES_NS . ':' . $faultMessageName;
357  }
358 
365  protected function getServiceMetadata($serviceName)
366  {
367  return $this->serviceMetadata->getServiceMetadata($serviceName);
368  }
369 
373  protected function getAllowedServicesMetadata($requestedServices)
374  {
375  $allowedServicesMetadata = parent::getAllowedServicesMetadata($requestedServices);
376  if (!$allowedServicesMetadata) {
377  throw new AuthorizationException(
378  __(
379  "The consumer isn't authorized to access %resources.",
380  ['resources' => implode(', ', $requestedServices)]
381  )
382  );
383  }
384  return $allowedServicesMetadata;
385  }
386 }
generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $endPointUrl)
Definition: Generator.php:64
getAllowedServicesMetadata($requestedServices)
Definition: Generator.php:373
__()
Definition: __.php:13
const NODE_DETAIL_WRAPPED_ERROR_PARAMETERS
Definition: Fault.php:34
__construct(\Magento\Webapi\Model\Cache\Type\Webapi $cache, \Magento\Framework\Reflection\TypeProcessor $typeProcessor, \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface $serviceTypeList, \Magento\Webapi\Model\ServiceMetadata $serviceMetadata, Authorization $authorization, WsdlFactory $wsdlFactory)
Definition: Generator.php:43
_createOperationInput(Wsdl $wsdl, $operationName, $methodData)
Definition: Generator.php:139
_createOperationOutput(Wsdl $wsdl, $operationName, $methodData)
Definition: Generator.php:182