Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Server.php
Go to the documentation of this file.
1 <?php
9 
11 
17 class Server
18 {
19  const SOAP_DEFAULT_ENCODING = 'UTF-8';
20 
24  const CONFIG_PATH_SOAP_CHARSET = 'webapi/soap/charset';
27  const REQUEST_PARAM_SERVICES = 'services';
28 
29  const REQUEST_PARAM_WSDL = 'wsdl';
30 
31  const REQUEST_PARAM_LIST_WSDL = 'wsdl_list';
32 
36  protected $_areaList;
37 
41  protected $_configScope;
42 
46  protected $_request;
47 
51  protected $_storeManager;
52 
57 
61  protected $_typeProcessor;
62 
66  protected $_scopeConfig;
67 
71  private $wsdlGenerator;
72 
86  public function __construct(
87  \Magento\Framework\App\AreaList $areaList,
88  \Magento\Framework\Config\ScopeInterface $configScope,
91  \Magento\Webapi\Model\Soap\ServerFactory $soapServerFactory,
92  \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
93  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
94  \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator
95  ) {
96  if (!extension_loaded('soap')) {
97  throw new \Magento\Framework\Webapi\Exception(
98  __('SOAP extension is not loaded.'),
99  0,
100  \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR
101  );
102  }
103  $this->_areaList = $areaList;
104  $this->_configScope = $configScope;
105  $this->_request = $request;
106  $this->_storeManager = $storeManager;
107  $this->_soapServerFactory = $soapServerFactory;
108  $this->_typeProcessor = $typeProcessor;
109  $this->_scopeConfig = $scopeConfig;
110  $this->wsdlGenerator = $wsdlGenerator;
111  }
112 
118  public function handle()
119  {
120  $rawRequestBody = file_get_contents('php://input');
121  $this->_checkRequest($rawRequestBody);
122  $options = ['encoding' => $this->getApiCharset(), 'soap_version' => SOAP_1_2];
123  $soapServer = $this->_soapServerFactory->create($this->getWsdlLocalUri(), $options);
124  $soapServer->handle($rawRequestBody);
125  }
126 
134  private function getWsdlLocalUri()
135  {
136  $wsdlBody = $this->wsdlGenerator->generate(
137  $this->_request->getRequestedServices(),
138  $this->_request->getScheme(),
139  $this->_request->getHttpHost(),
140  $this->generateUri()
141  );
142  return 'data://text/plain;base64,'.base64_encode($wsdlBody);
143  }
144 
150  public function getApiCharset()
151  {
152  $charset = $this->_scopeConfig->getValue(
153  self::CONFIG_PATH_SOAP_CHARSET,
154  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
155  );
156  return $charset ? $charset : self::SOAP_DEFAULT_ENCODING;
157  }
158 
165  public function generateUri($isWsdl = false)
166  {
167  $params = [self::REQUEST_PARAM_SERVICES => $this->_request->getParam(self::REQUEST_PARAM_SERVICES)];
168  if ($isWsdl) {
170  }
171  $query = http_build_query($params, '', '&');
172  return $this->getEndpointUri() . '?' . $query;
173  }
174 
180  public function getEndpointUri()
181  {
182  $storeCode = $this->_storeManager->getStore()->getCode() === \Magento\Store\Model\Store::ADMIN_CODE
184  : $this->_storeManager->getStore()->getCode();
185  return $this->_storeManager->getStore()->getBaseUrl()
186  . $this->_areaList->getFrontName($this->_configScope->getCurrentScope())
187  . '/' . $storeCode;
188  }
189 
197  protected function _checkRequest($soapRequest)
198  {
199  $dom = new \DOMDocument();
200  if (strlen($soapRequest) == 0 || !$dom->loadXML($soapRequest)) {
201  throw new \Magento\Framework\Webapi\Exception(
202  __('Invalid XML'),
203  0,
204  \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR
205  );
206  }
207  foreach ($dom->childNodes as $child) {
208  if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
209  throw new \Magento\Framework\Webapi\Exception(
210  __('Invalid XML: Detected use of illegal DOCTYPE'),
211  0,
212  \Magento\Framework\Webapi\Exception::HTTP_INTERNAL_ERROR
213  );
214  }
215  }
216  return $this;
217  }
218 }
$storeManager
__()
Definition: __.php:13
$storeCode
Definition: indexer.php:15
__construct(\Magento\Framework\App\AreaList $areaList, \Magento\Framework\Config\ScopeInterface $configScope, Request $request, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Webapi\Model\Soap\ServerFactory $soapServerFactory, \Magento\Framework\Reflection\TypeProcessor $typeProcessor, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Webapi\Model\Soap\Wsdl\Generator $wsdlGenerator)
Definition: Server.php:86
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18