Generator.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Webapi\Model\Soap\Wsdl;
  8. use Magento\Webapi\Model\AbstractSchemaGenerator;
  9. use Magento\Webapi\Model\Soap\Fault;
  10. use Magento\Webapi\Model\Soap\Wsdl;
  11. use Magento\Webapi\Model\Soap\WsdlFactory;
  12. use Magento\Framework\Webapi\Authorization;
  13. use Magento\Webapi\Model\ServiceMetadata;
  14. use Magento\Framework\Exception\AuthorizationException;
  15. /**
  16. * WSDL generator.
  17. */
  18. class Generator extends AbstractSchemaGenerator
  19. {
  20. /** WSDL name */
  21. const WSDL_NAME = 'MagentoWSDL';
  22. /**
  23. * WSDL factory instance.
  24. *
  25. * @var WsdlFactory
  26. */
  27. protected $_wsdlFactory;
  28. /**
  29. * Initialize dependencies.
  30. *
  31. * @param \Magento\Webapi\Model\Cache\Type\Webapi $cache
  32. * @param \Magento\Framework\Reflection\TypeProcessor $typeProcessor
  33. * @param \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface $serviceTypeList
  34. * @param \Magento\Webapi\Model\ServiceMetadata $serviceMetadata
  35. * @param Authorization $authorization
  36. * @param WsdlFactory $wsdlFactory
  37. */
  38. public function __construct(
  39. \Magento\Webapi\Model\Cache\Type\Webapi $cache,
  40. \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
  41. \Magento\Framework\Webapi\CustomAttribute\ServiceTypeListInterface $serviceTypeList,
  42. \Magento\Webapi\Model\ServiceMetadata $serviceMetadata,
  43. Authorization $authorization,
  44. WsdlFactory $wsdlFactory
  45. ) {
  46. $this->_wsdlFactory = $wsdlFactory;
  47. parent::__construct(
  48. $cache,
  49. $typeProcessor,
  50. $serviceTypeList,
  51. $serviceMetadata,
  52. $authorization
  53. );
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. protected function generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $endPointUrl)
  59. {
  60. $wsdl = $this->_wsdlFactory->create(self::WSDL_NAME, $endPointUrl);
  61. $wsdl->addSchemaTypeSection();
  62. $faultMessageName = $this->_addGenericFaultComplexTypeNodes($wsdl);
  63. $wsdl = $this->addCustomAttributeTypes($wsdl);
  64. foreach ($requestedServiceMetadata as $serviceClass => &$serviceData) {
  65. $portTypeName = $this->getPortTypeName($serviceClass);
  66. $bindingName = $this->getBindingName($serviceClass);
  67. $portType = $wsdl->addPortType($portTypeName);
  68. $binding = $wsdl->addBinding($bindingName, Wsdl::TYPES_NS . ':' . $portTypeName);
  69. $wsdl->addSoapBinding($binding, 'document', 'http://schemas.xmlsoap.org/soap/http', SOAP_1_2);
  70. $portName = $this->getPortName($serviceClass);
  71. $serviceName = $this->getServiceName($serviceClass);
  72. $wsdl->addService($serviceName, $portName, Wsdl::TYPES_NS . ':' . $bindingName, $endPointUrl, SOAP_1_2);
  73. foreach ($serviceData[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
  74. $operationName = $this->typeProcessor->getOperationName($serviceClass, $methodName);
  75. $bindingDataPrototype = ['use' => 'literal'];
  76. $inputBinding = $bindingDataPrototype;
  77. $inputMessageName = $this->_createOperationInput($wsdl, $operationName, $methodData);
  78. $outputMessageName = false;
  79. $outputBinding = false;
  80. if (isset($methodData['interface']['out']['parameters'])) {
  81. $outputBinding = $bindingDataPrototype;
  82. $outputMessageName = $this->_createOperationOutput($wsdl, $operationName, $methodData);
  83. }
  84. $faultBinding = ['name' => Fault::NODE_DETAIL_WRAPPER];
  85. $wsdl->addPortOperation(
  86. $portType,
  87. $operationName,
  88. $inputMessageName,
  89. $outputMessageName,
  90. ['message' => $faultMessageName, 'name' => Fault::NODE_DETAIL_WRAPPER]
  91. );
  92. $bindingOperation = $wsdl->addBindingOperation(
  93. $binding,
  94. $operationName,
  95. $inputBinding,
  96. $outputBinding,
  97. $faultBinding,
  98. SOAP_1_2
  99. );
  100. $wsdl->addSoapOperation($bindingOperation, $operationName, SOAP_1_2);
  101. }
  102. }
  103. return $wsdl->toXML();
  104. }
  105. /**
  106. * Create and add WSDL Types for complex custom attribute classes
  107. *
  108. * @param \Magento\Webapi\Model\Soap\Wsdl $wsdl
  109. * @return \Magento\Webapi\Model\Soap\Wsdl
  110. */
  111. protected function addCustomAttributeTypes($wsdl)
  112. {
  113. foreach ($this->serviceTypeList->getDataTypes() as $customAttributeClass) {
  114. $typeName = $this->typeProcessor->register($customAttributeClass);
  115. $wsdl->addComplexType($this->typeProcessor->getArrayItemType($typeName));
  116. }
  117. return $wsdl;
  118. }
  119. /**
  120. * Create input message and corresponding element and complex types in WSDL.
  121. *
  122. * @param Wsdl $wsdl
  123. * @param string $operationName
  124. * @param array $methodData
  125. * @return string input message name
  126. */
  127. protected function _createOperationInput(Wsdl $wsdl, $operationName, $methodData)
  128. {
  129. $inputMessageName = $this->getInputMessageName($operationName);
  130. $complexTypeName = $this->getElementComplexTypeName($inputMessageName);
  131. $inputParameters = [];
  132. $elementData = [
  133. 'name' => $inputMessageName,
  134. 'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
  135. ];
  136. if (isset($methodData['interface']['in']['parameters'])) {
  137. $inputParameters = $methodData['interface']['in']['parameters'];
  138. } else {
  139. $elementData['nillable'] = 'true';
  140. }
  141. $wsdl->addElement($elementData);
  142. $callInfo = [];
  143. $callInfo['requiredInput']['yes']['calls'] = [$operationName];
  144. $typeData = [
  145. 'documentation' => $methodData['documentation'],
  146. 'parameters' => $inputParameters,
  147. 'callInfo' => $callInfo,
  148. ];
  149. $this->typeProcessor->setTypeData($complexTypeName, $typeData);
  150. $wsdl->addComplexType($complexTypeName);
  151. $wsdl->addMessage(
  152. $inputMessageName,
  153. [
  154. 'messageParameters' => [
  155. 'element' => Wsdl::TYPES_NS . ':' . $inputMessageName,
  156. ]
  157. ]
  158. );
  159. return Wsdl::TYPES_NS . ':' . $inputMessageName;
  160. }
  161. /**
  162. * Create output message and corresponding element and complex types in WSDL.
  163. *
  164. * @param Wsdl $wsdl
  165. * @param string $operationName
  166. * @param array $methodData
  167. * @return string output message name
  168. */
  169. protected function _createOperationOutput(Wsdl $wsdl, $operationName, $methodData)
  170. {
  171. $outputMessageName = $this->getOutputMessageName($operationName);
  172. $complexTypeName = $this->getElementComplexTypeName($outputMessageName);
  173. $wsdl->addElement(
  174. [
  175. 'name' => $outputMessageName,
  176. 'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
  177. ]
  178. );
  179. $callInfo = [];
  180. $callInfo['returned']['always']['calls'] = [$operationName];
  181. $typeData = [
  182. 'documentation' => sprintf('Response container for the %s call.', $operationName),
  183. 'parameters' => $methodData['interface']['out']['parameters'],
  184. 'callInfo' => $callInfo,
  185. ];
  186. $this->typeProcessor->setTypeData($complexTypeName, $typeData);
  187. $wsdl->addComplexType($complexTypeName);
  188. $wsdl->addMessage(
  189. $outputMessageName,
  190. [
  191. 'messageParameters' => [
  192. 'element' => Wsdl::TYPES_NS . ':' . $outputMessageName,
  193. ]
  194. ]
  195. );
  196. return Wsdl::TYPES_NS . ':' . $outputMessageName;
  197. }
  198. /**
  199. * Get name for service portType node.
  200. *
  201. * @param string $serviceName
  202. * @return string
  203. */
  204. public function getPortTypeName($serviceName)
  205. {
  206. return $serviceName . 'PortType';
  207. }
  208. /**
  209. * Get name for service binding node.
  210. *
  211. * @param string $serviceName
  212. * @return string
  213. */
  214. public function getBindingName($serviceName)
  215. {
  216. return $serviceName . 'Binding';
  217. }
  218. /**
  219. * Get name for service port node.
  220. *
  221. * @param string $serviceName
  222. * @return string
  223. */
  224. public function getPortName($serviceName)
  225. {
  226. return $serviceName . 'Port';
  227. }
  228. /**
  229. * Get name for service service.
  230. *
  231. * @param string $serviceName
  232. * @return string
  233. */
  234. public function getServiceName($serviceName)
  235. {
  236. return $serviceName . 'Service';
  237. }
  238. /**
  239. * Get input message node name for operation.
  240. *
  241. * @param string $operationName
  242. * @return string
  243. */
  244. public function getInputMessageName($operationName)
  245. {
  246. return $operationName . 'Request';
  247. }
  248. /**
  249. * Get output message node name for operation.
  250. *
  251. * @param string $operationName
  252. * @return string
  253. */
  254. public function getOutputMessageName($operationName)
  255. {
  256. return $operationName . 'Response';
  257. }
  258. /**
  259. * Add WSDL elements related to generic SOAP fault, which are common for all operations: element, type and message.
  260. *
  261. * @param Wsdl $wsdl
  262. * @return string Default fault message name
  263. */
  264. protected function _addGenericFaultComplexTypeNodes($wsdl)
  265. {
  266. $faultMessageName = Fault::NODE_DETAIL_WRAPPER;
  267. $complexTypeName = $this->getElementComplexTypeName($faultMessageName);
  268. $wsdl->addElement(
  269. [
  270. 'name' => $faultMessageName,
  271. 'type' => Wsdl::TYPES_NS . ':' . $complexTypeName,
  272. ]
  273. );
  274. $faultParamsComplexType = Fault::NODE_DETAIL_PARAMETER;
  275. $faultParamsData = [
  276. 'parameters' => [
  277. Fault::NODE_DETAIL_PARAMETER_KEY => [
  278. 'type' => 'string',
  279. 'required' => true,
  280. 'documentation' => '',
  281. ],
  282. Fault::NODE_DETAIL_PARAMETER_VALUE => [
  283. 'type' => 'string',
  284. 'required' => true,
  285. 'documentation' => '',
  286. ],
  287. ],
  288. ];
  289. $wrappedErrorComplexType = Fault::NODE_DETAIL_WRAPPED_ERROR;
  290. $wrappedErrorData = [
  291. 'parameters' => [
  292. Fault::NODE_DETAIL_WRAPPED_ERROR_MESSAGE => [
  293. 'type' => 'string',
  294. 'required' => true,
  295. 'documentation' => '',
  296. ],
  297. Fault::NODE_DETAIL_WRAPPED_ERROR_PARAMETERS => [
  298. 'type' => "{$faultParamsComplexType}[]",
  299. 'required' => false,
  300. 'documentation' => 'Message parameters.',
  301. ],
  302. ],
  303. ];
  304. $genericFaultTypeData = [
  305. 'parameters' => [
  306. Fault::NODE_DETAIL_TRACE => [
  307. 'type' => 'string',
  308. 'required' => false,
  309. 'documentation' => 'Exception calls stack trace.',
  310. ],
  311. Fault::NODE_DETAIL_PARAMETERS => [
  312. 'type' => "{$faultParamsComplexType}[]",
  313. 'required' => false,
  314. 'documentation' => 'Additional exception parameters.',
  315. ],
  316. Fault::NODE_DETAIL_WRAPPED_ERRORS => [
  317. 'type' => "{$wrappedErrorComplexType}[]",
  318. 'required' => false,
  319. 'documentation' => 'Additional wrapped errors.',
  320. ],
  321. ],
  322. ];
  323. $this->typeProcessor->setTypeData($faultParamsComplexType, $faultParamsData);
  324. $this->typeProcessor->setTypeData($wrappedErrorComplexType, $wrappedErrorData);
  325. $this->typeProcessor->setTypeData($complexTypeName, $genericFaultTypeData);
  326. $wsdl->addComplexType($complexTypeName);
  327. $wsdl->addMessage(
  328. $faultMessageName,
  329. [
  330. 'messageParameters' => [
  331. 'element' => Wsdl::TYPES_NS . ':' . $faultMessageName,
  332. ]
  333. ]
  334. );
  335. return Wsdl::TYPES_NS . ':' . $faultMessageName;
  336. }
  337. /**
  338. * Get service metadata
  339. *
  340. * @param string $serviceName
  341. * @return array
  342. */
  343. protected function getServiceMetadata($serviceName)
  344. {
  345. return $this->serviceMetadata->getServiceMetadata($serviceName);
  346. }
  347. /**
  348. * {@inheritdoc}
  349. */
  350. protected function getAllowedServicesMetadata($requestedServices)
  351. {
  352. $allowedServicesMetadata = parent::getAllowedServicesMetadata($requestedServices);
  353. if (!$allowedServicesMetadata) {
  354. throw new AuthorizationException(
  355. __(
  356. "The consumer isn't authorized to access %resources.",
  357. ['resources' => implode(', ', $requestedServices)]
  358. )
  359. );
  360. }
  361. return $allowedServicesMetadata;
  362. }
  363. }