Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 5d5d48d

Browse files
committed
2 parents a86fd3f + aa45d49 commit 5d5d48d

File tree

10 files changed

+507
-220
lines changed

10 files changed

+507
-220
lines changed

app/code/Magento/Webapi/Controller/Rest.php

Lines changed: 19 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Webapi\Controller;
78

89
use Magento\Framework\App\DeploymentConfig;
@@ -13,15 +14,13 @@
1314
use Magento\Framework\Webapi\Request;
1415
use Magento\Framework\Webapi\Rest\Request as RestRequest;
1516
use Magento\Framework\Webapi\Rest\Response as RestResponse;
16-
use Magento\Framework\Webapi\Rest\Response\FieldsFilter;
1717
use Magento\Framework\Webapi\ServiceInputProcessor;
18-
use Magento\Framework\Webapi\ServiceOutputProcessor;
1918
use Magento\Store\Model\Store;
2019
use Magento\Store\Model\StoreManagerInterface;
2120
use Magento\Webapi\Controller\Rest\ParamsOverrider;
2221
use Magento\Webapi\Controller\Rest\Router;
2322
use Magento\Webapi\Controller\Rest\Router\Route;
24-
use Magento\Webapi\Model\Rest\Swagger\Generator;
23+
use Magento\Webapi\Controller\Rest\RequestProcessorPool;
2524

2625
/**
2726
* Front controller for WebAPI REST area.
@@ -31,7 +30,11 @@
3130
*/
3231
class Rest implements \Magento\Framework\App\FrontControllerInterface
3332
{
34-
/** Path for accessing REST API schema */
33+
/**
34+
* Path for accessing REST API schema
35+
*
36+
* @deprecated 100.3.0
37+
*/
3538
const SCHEMA_PATH = '/schema';
3639

3740
/**
@@ -93,11 +96,6 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
9396
*/
9497
protected $areaList;
9598

96-
/**
97-
* @var \Magento\Framework\Webapi\Rest\Response\FieldsFilter
98-
*/
99-
protected $fieldsFilter;
100-
10199
/**
102100
* @var \Magento\Framework\Session\Generic
103101
*/
@@ -110,31 +108,16 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
110108
protected $paramsOverrider;
111109

112110
/**
113-
* @var \Magento\Framework\Webapi\ServiceOutputProcessor
114-
*/
115-
protected $serviceOutputProcessor;
116-
117-
/**
118-
* @var \Magento\Webapi\Model\Rest\Swagger\Generator
111+
* @var RequestProcessorPool
119112
*/
120-
protected $swaggerGenerator;
113+
protected $requestProcessorPool;
121114

122115
/**
123116
* @var StoreManagerInterface
124117
* @deprecated 100.1.0
125118
*/
126119
private $storeManager;
127120

128-
/**
129-
* @var DeploymentConfig
130-
*/
131-
private $deploymentConfig;
132-
133-
/**
134-
* @var Rest\InputParamsResolver
135-
*/
136-
private $inputParamsResolver;
137-
138121
/**
139122
* Initialize dependencies
140123
*
@@ -148,11 +131,9 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
148131
* @param ErrorProcessor $errorProcessor
149132
* @param PathProcessor $pathProcessor
150133
* @param \Magento\Framework\App\AreaList $areaList
151-
* @param FieldsFilter $fieldsFilter
152134
* @param ParamsOverrider $paramsOverrider
153-
* @param ServiceOutputProcessor $serviceOutputProcessor
154-
* @param Generator $swaggerGenerator ,
155135
* @param StoreManagerInterface $storeManager
136+
* @param RequestProcessorPool $requestProcessorPool
156137
*
157138
* TODO: Consider removal of warning suppression
158139
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -168,11 +149,9 @@ public function __construct(
168149
ErrorProcessor $errorProcessor,
169150
PathProcessor $pathProcessor,
170151
\Magento\Framework\App\AreaList $areaList,
171-
FieldsFilter $fieldsFilter,
172152
ParamsOverrider $paramsOverrider,
173-
ServiceOutputProcessor $serviceOutputProcessor,
174-
Generator $swaggerGenerator,
175-
StoreManagerInterface $storeManager
153+
StoreManagerInterface $storeManager,
154+
RequestProcessorPool $requestProcessorPool
176155
) {
177156
$this->_router = $router;
178157
$this->_request = $request;
@@ -184,37 +163,9 @@ public function __construct(
184163
$this->_errorProcessor = $errorProcessor;
185164
$this->_pathProcessor = $pathProcessor;
186165
$this->areaList = $areaList;
187-
$this->fieldsFilter = $fieldsFilter;
188166
$this->paramsOverrider = $paramsOverrider;
189-
$this->serviceOutputProcessor = $serviceOutputProcessor;
190-
$this->swaggerGenerator = $swaggerGenerator;
191167
$this->storeManager = $storeManager;
192-
}
193-
194-
/**
195-
* Get deployment config
196-
*
197-
* @return DeploymentConfig
198-
*/
199-
private function getDeploymentConfig()
200-
{
201-
if (!$this->deploymentConfig instanceof \Magento\Framework\App\DeploymentConfig) {
202-
$this->deploymentConfig = \Magento\Framework\App\ObjectManager::getInstance()
203-
->get(\Magento\Framework\App\DeploymentConfig::class);
204-
}
205-
return $this->deploymentConfig;
206-
}
207-
208-
/**
209-
* Set deployment config
210-
*
211-
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
212-
* @return void
213-
* @deprecated 100.1.0
214-
*/
215-
public function setDeploymentConfig(\Magento\Framework\App\DeploymentConfig $deploymentConfig)
216-
{
217-
$this->deploymentConfig = $deploymentConfig;
168+
$this->requestProcessorPool = $requestProcessorPool;
218169
}
219170

220171
/**
@@ -231,17 +182,14 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
231182
$path = $this->_pathProcessor->process($request->getPathInfo());
232183
$this->_request->setPathInfo($path);
233184
$this->areaList->getArea($this->_appState->getAreaCode())
234-
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
185+
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
235186
try {
236-
if ($this->isSchemaRequest()) {
237-
$this->processSchemaRequest();
238-
} else {
239-
$this->processApiRequest();
240-
}
187+
$this->requestProcessorPool->process($this->_request);
241188
} catch (\Exception $e) {
242189
$maskedException = $this->_errorProcessor->maskException($e);
243190
$this->_response->setException($maskedException);
244191
}
192+
245193
return $this->_response;
246194
}
247195

@@ -260,13 +208,14 @@ protected function isSchemaRequest()
260208
*
261209
* @return Route
262210
* @deprecated 100.1.0
263-
* @see \Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
211+
* @see Magento\Webapi\Controller\Rest\InputParamsResolver::getRoute
264212
*/
265213
protected function getCurrentRoute()
266214
{
267215
if (!$this->_route) {
268216
$this->_route = $this->_router->match($this->_request);
269217
}
218+
270219
return $this->_route;
271220
}
272221

@@ -289,68 +238,14 @@ protected function checkPermissions()
289238
}
290239
}
291240

292-
/**
293-
* Execute schema request
294-
*
295-
* @return void
296-
*/
297-
protected function processSchemaRequest()
298-
{
299-
$requestedServices = $this->_request->getRequestedServices('all');
300-
$requestedServices = $requestedServices == Request::ALL_SERVICES
301-
? $this->swaggerGenerator->getListOfServices()
302-
: $requestedServices;
303-
$responseBody = $this->swaggerGenerator->generate(
304-
$requestedServices,
305-
$this->_request->getScheme(),
306-
$this->_request->getHttpHost(false),
307-
$this->_request->getRequestUri()
308-
);
309-
$this->_response->setBody($responseBody)->setHeader('Content-Type', 'application/json');
310-
}
311-
312-
/**
313-
* Execute API request
314-
*
315-
* @return void
316-
* @throws AuthorizationException
317-
* @throws \Magento\Framework\Exception\InputException
318-
* @throws \Magento\Framework\Webapi\Exception
319-
*/
320-
protected function processApiRequest()
321-
{
322-
$inputParams = $this->getInputParamsResolver()->resolve();
323-
324-
$route = $this->getInputParamsResolver()->getRoute();
325-
$serviceMethodName = $route->getServiceMethod();
326-
$serviceClassName = $route->getServiceClass();
327-
328-
$service = $this->_objectManager->get($serviceClassName);
329-
/** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */
330-
$outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
331-
$outputData = $this->serviceOutputProcessor->process(
332-
$outputData,
333-
$serviceClassName,
334-
$serviceMethodName
335-
);
336-
if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) {
337-
$outputData = $this->fieldsFilter->filter($outputData);
338-
}
339-
$header = $this->getDeploymentConfig()->get(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT);
340-
if ($header) {
341-
$this->_response->setHeader('X-Frame-Options', $header);
342-
}
343-
$this->_response->prepareResponse($outputData);
344-
}
345-
346241
/**
347242
* Validate request
348243
*
349244
* @throws AuthorizationException
350245
* @throws \Magento\Framework\Webapi\Exception
351246
* @return void
352247
* @deprecated 100.1.0
353-
* @see \Magento\Webapi\Controller\Rest\RequestValidator::validate
248+
* @see Magento\Webapi\Controller\Rest\RequestValidator::validate
354249
*/
355250
protected function validateRequest()
356251
{
@@ -364,20 +259,4 @@ protected function validateRequest()
364259
throw new \Magento\Framework\Webapi\Exception(__('Cannot perform GET operation with store code \'all\''));
365260
}
366261
}
367-
368-
/**
369-
* The getter function to get InputParamsResolver object
370-
*
371-
* @return \Magento\Webapi\Controller\Rest\InputParamsResolver
372-
*
373-
* @deprecated 100.1.0
374-
*/
375-
private function getInputParamsResolver()
376-
{
377-
if ($this->inputParamsResolver === null) {
378-
$this->inputParamsResolver = \Magento\Framework\App\ObjectManager::getInstance()
379-
->get(\Magento\Webapi\Controller\Rest\InputParamsResolver::class);
380-
}
381-
return $this->inputParamsResolver;
382-
}
383262
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Controller\Rest;
8+
9+
/**
10+
* Request processor interface
11+
*/
12+
interface RequestProcessorInterface
13+
{
14+
15+
/**
16+
* @param \Magento\Framework\Webapi\Rest\Request $request
17+
* @return void
18+
* @throws \Magento\Framework\Exception\AuthorizationException
19+
* @throws \Magento\Framework\Exception\InputException
20+
* @throws \Magento\Framework\Webapi\Exception
21+
*/
22+
public function process(\Magento\Framework\Webapi\Rest\Request $request);
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getProcessorPath();
28+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Webapi\Controller\Rest;
8+
9+
/**
10+
* Request Processor Pool
11+
*/
12+
class RequestProcessorPool implements RequestProcessorInterface
13+
{
14+
15+
/**
16+
* @var array
17+
*/
18+
private $requestProcessors;
19+
20+
/**
21+
* Initial dependencies
22+
*
23+
* @param array $requestProcessors
24+
*/
25+
public function __construct($requestProcessors = [])
26+
{
27+
$this->requestProcessors = $requestProcessors;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*
33+
* @throws \Magento\Framework\Exception\LocalizedException
34+
*/
35+
public function process(\Magento\Framework\Webapi\Rest\Request $request)
36+
{
37+
$processed = false;
38+
39+
/**
40+
* @var RequestProcessorInterface $processor
41+
*/
42+
foreach ($this->requestProcessors as $processor) {
43+
if (strpos(ltrim($request->getPathInfo(), '/'), $processor->getProcessorPath()) === 0) {
44+
$processor->process($request);
45+
$processed = true;
46+
break;
47+
}
48+
}
49+
if (!$processed) {
50+
throw new \Magento\Framework\Exception\LocalizedException(
51+
__('Specified request cannot be processed.'),
52+
null,
53+
400
54+
);
55+
}
56+
}
57+
58+
/**
59+
* Get array of rest processors from di.xml
60+
*
61+
* @return array
62+
*/
63+
public function getProcessors()
64+
{
65+
return $this->requestProcessors;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function getProcessorPath()
72+
{
73+
return null;
74+
}
75+
}

app/code/Magento/Webapi/Controller/Rest/Router/Route.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,4 +241,14 @@ public function getParameters()
241241
{
242242
return $this->parameters;
243243
}
244+
245+
/**
246+
* Get route path.
247+
*
248+
* @return string
249+
*/
250+
public function getRoutePath()
251+
{
252+
return $this->route;
253+
}
244254
}

0 commit comments

Comments
 (0)