Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Soap.php
Go to the documentation of this file.
1 <?php
9 
13 
21 {
25  const CONTENT_TYPE_SOAP_CALL = 'application/soap+xml';
26 
27  const CONTENT_TYPE_WSDL_REQUEST = 'text/xml';
28 
32  protected $_soapServer;
33 
37  protected $_wsdlGenerator;
38 
42  protected $_request;
43 
47  protected $_response;
48 
52  protected $_errorProcessor;
53 
57  protected $_appState;
58 
62  protected $_localeResolver;
63 
67  protected $_pathProcessor;
68 
72  protected $areaList;
73 
77  protected $rendererFactory;
78 
92  public function __construct(
94  \Magento\Framework\Webapi\Response $response,
95  \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator,
96  \Magento\Webapi\Model\Soap\Server $soapServer,
97  ErrorProcessor $errorProcessor,
98  \Magento\Framework\App\State $appState,
99  \Magento\Framework\Locale\ResolverInterface $localeResolver,
100  PathProcessor $pathProcessor,
101  \Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory,
102  \Magento\Framework\App\AreaList $areaList
103  ) {
104  $this->_request = $request;
105  $this->_response = $response;
106  $this->_wsdlGenerator = $wsdlGenerator;
107  $this->_soapServer = $soapServer;
108  $this->_errorProcessor = $errorProcessor;
109  $this->_appState = $appState;
110  $this->_localeResolver = $localeResolver;
111  $this->_pathProcessor = $pathProcessor;
112  $this->areaList = $areaList;
113  $this->rendererFactory = $rendererFactory;
114  }
115 
122  public function dispatch(\Magento\Framework\App\RequestInterface $request)
123  {
124  $path = $this->_pathProcessor->process($request->getPathInfo());
125  $this->_request->setPathInfo($path);
126  $this->areaList->getArea($this->_appState->getAreaCode())->load(\Magento\Framework\App\Area::PART_TRANSLATE);
127  try {
128  if ($this->_isWsdlRequest()) {
129  $this->validateWsdlRequest();
130  $responseBody = $this->_wsdlGenerator->generate(
131  $this->_request->getRequestedServices(),
132  $this->_request->getScheme(),
133  $this->_request->getHttpHost(),
134  $this->_soapServer->generateUri()
135  );
136  $this->_setResponseContentType(self::CONTENT_TYPE_WSDL_REQUEST);
137  $this->_setResponseBody($responseBody);
138  } elseif ($this->_isWsdlListRequest()) {
139  $servicesList = [];
140  foreach ($this->_wsdlGenerator->getListOfServices() as $serviceName) {
141  $servicesList[$serviceName]['wsdl_endpoint'] = $this->_soapServer->getEndpointUri()
142  . '?' . \Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL . '&services=' . $serviceName;
143  }
144  $renderer = $this->rendererFactory->get();
145  $this->_setResponseContentType($renderer->getMimeType());
146  $this->_setResponseBody($renderer->render($servicesList));
147  } else {
148  $this->_soapServer->handle();
149  }
150  } catch (\Exception $e) {
151  $this->_prepareErrorResponse($e);
152  }
153  return $this->_response;
154  }
155 
161  protected function _isWsdlRequest()
162  {
163  return $this->_request->getParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_WSDL) !== null;
164  }
165 
171  protected function _isWsdlListRequest()
172  {
173  return $this->_request->getParam(\Magento\Webapi\Model\Soap\Server::REQUEST_PARAM_LIST_WSDL) !== null;
174  }
175 
182  protected function _prepareErrorResponse($exception)
183  {
184  $maskedException = $this->_errorProcessor->maskException($exception);
185  if ($this->_isWsdlRequest()) {
186  $httpCode = $maskedException->getHttpCode();
187  $contentType = self::CONTENT_TYPE_WSDL_REQUEST;
188  } else {
189  $httpCode = Response::HTTP_OK;
190  $contentType = self::CONTENT_TYPE_SOAP_CALL;
191  }
192  $this->_setResponseContentType($contentType);
193  $this->_response->setHttpResponseCode($httpCode);
194  $soapFault = new \Magento\Webapi\Model\Soap\Fault(
195  $this->_request,
196  $this->_soapServer,
197  $maskedException,
198  $this->_localeResolver,
199  $this->_appState
200  );
201  $this->_setResponseBody($soapFault->toXml());
202  }
203 
210  protected function _setResponseContentType($contentType = 'text/xml')
211  {
212  $this->_response->clearHeaders()->setHeader(
213  'Content-Type',
214  "{$contentType}; charset={$this->_soapServer->getApiCharset()}"
215  );
216  return $this;
217  }
218 
225  protected function _setResponseBody($responseBody)
226  {
227  $this->_response->setBody(
228  preg_replace(
229  '/<\?xml version="([^\"]+)"([^>]+)>/i',
230  '<?xml version="$1" encoding="' . $this->_soapServer->getApiCharset() . '"?>',
231  $responseBody
232  )
233  );
234  return $this;
235  }
236 
243  protected function validateWsdlRequest()
244  {
246  $servicesParam = Request::REQUEST_PARAM_SERVICES;
247  $requestParams = array_keys($this->_request->getParams());
248  $allowedParams = [$wsdlParam, $servicesParam];
249  $notAllowedParameters = array_diff($requestParams, $allowedParams);
250  if (count($notAllowedParameters)) {
251  $notAllowed = implode(', ', $notAllowedParameters);
252  $message = __(
253  'Not allowed parameters: %1. Please use only %2 and %3.',
254  $notAllowed,
255  $wsdlParam,
256  $servicesParam
257  );
258  throw new \Magento\Framework\Webapi\Exception($message);
259  }
260  }
261 }
$response
Definition: 404.php:11
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
_setResponseContentType($contentType='text/xml')
Definition: Soap.php:210
$message
dispatch(\Magento\Framework\App\RequestInterface $request)
Definition: Soap.php:122
_setResponseBody($responseBody)
Definition: Soap.php:225
_prepareErrorResponse($exception)
Definition: Soap.php:182
__construct(Request $request, \Magento\Framework\Webapi\Response $response, \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator, \Magento\Webapi\Model\Soap\Server $soapServer, ErrorProcessor $errorProcessor, \Magento\Framework\App\State $appState, \Magento\Framework\Locale\ResolverInterface $localeResolver, PathProcessor $pathProcessor, \Magento\Framework\Webapi\Rest\Response\RendererFactory $rendererFactory, \Magento\Framework\App\AreaList $areaList)
Definition: Soap.php:92