Skip to content
This repository has been archived by the owner on May 20, 2019. It is now read-only.

Change Webapi extension to use Inject Pool logic #1

Merged
merged 19 commits into from
Mar 21, 2018
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 17 additions & 138 deletions app/code/Magento/Webapi/Controller/Rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Webapi\Controller;

use Magento\Framework\App\DeploymentConfig;
Expand All @@ -13,15 +14,13 @@
use Magento\Framework\Webapi\Request;
use Magento\Framework\Webapi\Rest\Request as RestRequest;
use Magento\Framework\Webapi\Rest\Response as RestResponse;
use Magento\Framework\Webapi\Rest\Response\FieldsFilter;
use Magento\Framework\Webapi\ServiceInputProcessor;
use Magento\Framework\Webapi\ServiceOutputProcessor;
use Magento\Store\Model\Store;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Webapi\Controller\Rest\ParamsOverrider;
use Magento\Webapi\Controller\Rest\Router;
use Magento\Webapi\Controller\Rest\Router\Route;
use Magento\Webapi\Model\Rest\Swagger\Generator;
use Magento\Webapi\Controller\Rest\RequestProcessorPool;

/**
* Front controller for WebAPI REST area.
Expand All @@ -31,7 +30,11 @@
*/
class Rest implements \Magento\Framework\App\FrontControllerInterface
{
/** Path for accessing REST API schema */
/**
* Path for accessing REST API schema
*
* @deprecated 100.3.0
*/
const SCHEMA_PATH = '/schema';

/**
Expand Down Expand Up @@ -93,11 +96,6 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
*/
protected $areaList;

/**
* @var \Magento\Framework\Webapi\Rest\Response\FieldsFilter
*/
protected $fieldsFilter;

/**
* @var \Magento\Framework\Session\Generic
*/
Expand All @@ -110,31 +108,16 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
protected $paramsOverrider;

/**
* @var \Magento\Framework\Webapi\ServiceOutputProcessor
*/
protected $serviceOutputProcessor;

/**
* @var \Magento\Webapi\Model\Rest\Swagger\Generator
* @var RequestProcessorPool
*/
protected $swaggerGenerator;
protected $requestProcessorPool;

/**
* @var StoreManagerInterface
* @deprecated 100.1.0
*/
private $storeManager;

/**
* @var DeploymentConfig
*/
private $deploymentConfig;

/**
* @var Rest\InputParamsResolver
*/
private $inputParamsResolver;

/**
* Initialize dependencies
*
Expand All @@ -148,11 +131,9 @@ class Rest implements \Magento\Framework\App\FrontControllerInterface
* @param ErrorProcessor $errorProcessor
* @param PathProcessor $pathProcessor
* @param \Magento\Framework\App\AreaList $areaList
* @param FieldsFilter $fieldsFilter
* @param ParamsOverrider $paramsOverrider
* @param ServiceOutputProcessor $serviceOutputProcessor
* @param Generator $swaggerGenerator ,
* @param StoreManagerInterface $storeManager
* @param RequestProcessorPool $requestProcessorPool
*
* TODO: Consider removal of warning suppression
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
Expand All @@ -168,11 +149,9 @@ public function __construct(
ErrorProcessor $errorProcessor,
PathProcessor $pathProcessor,
\Magento\Framework\App\AreaList $areaList,
FieldsFilter $fieldsFilter,
ParamsOverrider $paramsOverrider,
ServiceOutputProcessor $serviceOutputProcessor,
Generator $swaggerGenerator,
StoreManagerInterface $storeManager
StoreManagerInterface $storeManager,
RequestProcessorPool $requestProcessorPool
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to follow Backward Compatibility
ObjectManager::getInstance()->create(...::class)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to operate with RequestProcessorInterface
Pool it will be one of implementation of RequestProcessorInterface

Underhood of RequestProcessorInterface we can resolve proper processor by request and call process method

) {
$this->_router = $router;
$this->_request = $request;
Expand All @@ -184,37 +163,9 @@ public function __construct(
$this->_errorProcessor = $errorProcessor;
$this->_pathProcessor = $pathProcessor;
$this->areaList = $areaList;
$this->fieldsFilter = $fieldsFilter;
$this->paramsOverrider = $paramsOverrider;
$this->serviceOutputProcessor = $serviceOutputProcessor;
$this->swaggerGenerator = $swaggerGenerator;
$this->storeManager = $storeManager;
}

/**
* Get deployment config
*
* @return DeploymentConfig
*/
private function getDeploymentConfig()
{
if (!$this->deploymentConfig instanceof \Magento\Framework\App\DeploymentConfig) {
$this->deploymentConfig = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\App\DeploymentConfig::class);
}
return $this->deploymentConfig;
}

/**
* Set deployment config
*
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
* @return void
* @deprecated 100.1.0
*/
public function setDeploymentConfig(\Magento\Framework\App\DeploymentConfig $deploymentConfig)
{
$this->deploymentConfig = $deploymentConfig;
$this->requestProcessorPool = $requestProcessorPool;
}

/**
Expand All @@ -231,17 +182,14 @@ public function dispatch(\Magento\Framework\App\RequestInterface $request)
$path = $this->_pathProcessor->process($request->getPathInfo());
$this->_request->setPathInfo($path);
$this->areaList->getArea($this->_appState->getAreaCode())
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
->load(\Magento\Framework\App\Area::PART_TRANSLATE);
try {
if ($this->isSchemaRequest()) {
$this->processSchemaRequest();
} else {
$this->processApiRequest();
}
$this->requestProcessorPool->process($this->_request);
} catch (\Exception $e) {
$maskedException = $this->_errorProcessor->maskException($e);
$this->_response->setException($maskedException);
}

return $this->_response;
}

Expand All @@ -267,6 +215,7 @@ protected function getCurrentRoute()
if (!$this->_route) {
$this->_route = $this->_router->match($this->_request);
}

return $this->_route;
}

Expand All @@ -289,60 +238,6 @@ protected function checkPermissions()
}
}

/**
* Execute schema request
*
* @return void
*/
protected function processSchemaRequest()
{
$requestedServices = $this->_request->getRequestedServices('all');
$requestedServices = $requestedServices == Request::ALL_SERVICES
? $this->swaggerGenerator->getListOfServices()
: $requestedServices;
$responseBody = $this->swaggerGenerator->generate(
$requestedServices,
$this->_request->getScheme(),
$this->_request->getHttpHost(false),
$this->_request->getRequestUri()
);
$this->_response->setBody($responseBody)->setHeader('Content-Type', 'application/json');
}

/**
* Execute API request
*
* @return void
* @throws AuthorizationException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Webapi\Exception
*/
protected function processApiRequest()
{
$inputParams = $this->getInputParamsResolver()->resolve();

$route = $this->getInputParamsResolver()->getRoute();
$serviceMethodName = $route->getServiceMethod();
$serviceClassName = $route->getServiceClass();

$service = $this->_objectManager->get($serviceClassName);
/** @var \Magento\Framework\Api\AbstractExtensibleObject $outputData */
$outputData = call_user_func_array([$service, $serviceMethodName], $inputParams);
$outputData = $this->serviceOutputProcessor->process(
$outputData,
$serviceClassName,
$serviceMethodName
);
if ($this->_request->getParam(FieldsFilter::FILTER_PARAMETER) && is_array($outputData)) {
$outputData = $this->fieldsFilter->filter($outputData);
}
$header = $this->getDeploymentConfig()->get(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT);
if ($header) {
$this->_response->setHeader('X-Frame-Options', $header);
}
$this->_response->prepareResponse($outputData);
}

/**
* Validate request
*
Expand All @@ -364,20 +259,4 @@ protected function validateRequest()
throw new \Magento\Framework\Webapi\Exception(__('Cannot perform GET operation with store code \'all\''));
}
}

/**
* The getter function to get InputParamsResolver object
*
* @return \Magento\Webapi\Controller\Rest\InputParamsResolver
*
* @deprecated 100.1.0
*/
private function getInputParamsResolver()
{
if ($this->inputParamsResolver === null) {
$this->inputParamsResolver = \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Webapi\Controller\Rest\InputParamsResolver::class);
}
return $this->inputParamsResolver;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Webapi\Controller\Rest;

/**
* Request processor interface
*/
interface RequestProcessorInterface
{

/**
* @param \Magento\Framework\Webapi\Rest\Request $request
* @return void
* @throws \Magento\Framework\Exception\AuthorizationException
* @throws \Magento\Framework\Exception\InputException
* @throws \Magento\Framework\Webapi\Exception
*/
public function process(\Magento\Framework\Webapi\Rest\Request $request);

/**
* @return string
*/
public function getProcessorPath();
}
75 changes: 75 additions & 0 deletions app/code/Magento/Webapi/Controller/Rest/RequestProcessorPool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Webapi\Controller\Rest;

/**
* Request Processor Pool
*/
class RequestProcessorPool implements RequestProcessorInterface
{

/**
* @var array
*/
private $requestProcessors;

/**
* Initial dependencies
*
* @param array $requestProcessors
*/
public function __construct($requestProcessors = [])
{
$this->requestProcessors = $requestProcessors;
}

/**
* {@inheritdoc}
*
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function process(\Magento\Framework\Webapi\Rest\Request $request)
{
$processed = false;

/**
* @var RequestProcessorInterface $processor
*/
foreach ($this->requestProcessors as $processor) {
if (strpos(ltrim($request->getPathInfo(), '/'), $processor->getProcessorPath()) === 0) {
$processor->process($request);
$processed = true;
break;
}
}
if (!$processed) {
throw new \Magento\Framework\Exception\LocalizedException(
__('Specified request cannot be processed.'),
null,
400
);
}
}

/**
* Get array of rest processors from di.xml
*
* @return array
*/
public function getProcessors()
{
return $this->requestProcessors;
}

/**
* {@inheritdoc}
*/
public function getProcessorPath()
{
return null;
}
}
Loading