Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rest.php
Go to the documentation of this file.
1 <?php
8 
24 
32 {
38  const SCHEMA_PATH = '/schema';
39 
44  protected $_router;
45 
50  protected $_route;
51 
55  protected $_request;
56 
60  protected $_response;
61 
65  protected $_objectManager;
66 
70  protected $_appState;
71 
76  protected $authorization;
77 
83 
87  protected $_errorProcessor;
88 
92  protected $_pathProcessor;
93 
97  protected $areaList;
98 
102  protected $session;
103 
108  protected $paramsOverrider;
109 
114 
119  private $storeManager;
120 
141  public function __construct(
144  Router $router,
145  \Magento\Framework\ObjectManagerInterface $objectManager,
146  \Magento\Framework\App\State $appState,
149  ErrorProcessor $errorProcessor,
150  PathProcessor $pathProcessor,
151  \Magento\Framework\App\AreaList $areaList,
153  StoreManagerInterface $storeManager,
155  ) {
156  $this->_router = $router;
157  $this->_request = $request;
158  $this->_response = $response;
159  $this->_objectManager = $objectManager;
160  $this->_appState = $appState;
161  $this->authorization = $authorization;
162  $this->serviceInputProcessor = $serviceInputProcessor;
163  $this->_errorProcessor = $errorProcessor;
164  $this->_pathProcessor = $pathProcessor;
165  $this->areaList = $areaList;
166  $this->paramsOverrider = $paramsOverrider;
167  $this->storeManager = $storeManager;
168  $this->requestProcessorPool = $requestProcessorPool;
169  }
170 
180  public function dispatch(\Magento\Framework\App\RequestInterface $request)
181  {
182  $path = $this->_pathProcessor->process($request->getPathInfo());
183  $this->_request->setPathInfo($path);
184  $this->areaList->getArea($this->_appState->getAreaCode())
185  ->load(\Magento\Framework\App\Area::PART_TRANSLATE);
186  try {
187  $processor = $this->requestProcessorPool->getProcessor($this->_request);
188  $processor->process($this->_request);
189  } catch (\Exception $e) {
190  $maskedException = $this->_errorProcessor->maskException($e);
191  $this->_response->setException($maskedException);
192  }
193 
194  return $this->_response;
195  }
196 
202  protected function isSchemaRequest()
203  {
204  return $this->_request->getPathInfo() === self::SCHEMA_PATH;
205  }
206 
214  protected function getCurrentRoute()
215  {
216  if (!$this->_route) {
217  $this->_route = $this->_router->match($this->_request);
218  }
219 
220  return $this->_route;
221  }
222 
231  protected function checkPermissions()
232  {
233  $route = $this->getCurrentRoute();
234  if (!$this->authorization->isAllowed($route->getAclResources())) {
235  $params = ['resources' => implode(', ', $route->getAclResources())];
236  throw new AuthorizationException(
237  __("The consumer isn't authorized to access %resources.", $params)
238  );
239  }
240  }
241 
251  protected function validateRequest()
252  {
253  $this->checkPermissions();
254  if ($this->getCurrentRoute()->isSecure() && !$this->_request->isSecure()) {
255  throw new \Magento\Framework\Webapi\Exception(__('Operation allowed only in HTTPS'));
256  }
257  if ($this->storeManager->getStore()->getCode() === Store::ADMIN_CODE
258  && strtoupper($this->_request->getMethod()) === RestRequest::HTTP_METHOD_GET
259  ) {
260  throw new \Magento\Framework\Webapi\Exception(__('Cannot perform GET operation with store code \'all\''));
261  }
262  }
263 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
$processor
Definition: 404.php:10
$storeManager
__()
Definition: __.php:13
__construct(RestRequest $request, RestResponse $response, Router $router, \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Framework\App\State $appState, Authorization $authorization, ServiceInputProcessor $serviceInputProcessor, ErrorProcessor $errorProcessor, PathProcessor $pathProcessor, \Magento\Framework\App\AreaList $areaList, ParamsOverrider $paramsOverrider, StoreManagerInterface $storeManager, RequestProcessorPool $requestProcessorPool)
Definition: Rest.php:141
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
dispatch(\Magento\Framework\App\RequestInterface $request)
Definition: Rest.php:180