Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractSchemaGenerator.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Webapi\Model;
8 
14 
19 {
23  protected $cache;
24 
28  protected $typeProcessor;
29 
33  protected $serviceTypeList;
34 
38  protected $serviceMetadata;
39 
43  protected $authorization;
44 
50  private $serializer;
51 
62  public function __construct(
63  Webapi $cache,
64  \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
68  Json $serializer = null
69  ) {
70  $this->cache = $cache;
71  $this->typeProcessor = $typeProcessor;
72  $this->serviceTypeList = $serviceTypeList;
73  $this->serviceMetadata = $serviceMetadata;
74  $this->authorization = $authorization;
75  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
76  }
77 
83  public function getListOfServices()
84  {
85  return array_keys($this->serviceMetadata->getServicesConfig());
86  }
87 
97  public function generate($requestedServices, $requestScheme, $requestHost, $endPointUrl)
98  {
100  ksort($requestedServices);
101  $prefix = get_class($this) . $this->serializer->serialize($requestedServices);
102  $cacheId = $this->cache->generateCacheIdUsingContext($prefix);
103  $cachedSchemaContent = $this->cache->load($cacheId);
104  if ($cachedSchemaContent !== false) {
105  return $cachedSchemaContent;
106  }
107  $allowedServicesMetadata = $this->getAllowedServicesMetadata($requestedServices);
108  $this->collectCallInfo($allowedServicesMetadata);
109  $schemaContent = $this->generateSchema($allowedServicesMetadata, $requestScheme, $requestHost, $endPointUrl);
110  $this->cache->save($schemaContent, $cacheId, [Webapi::CACHE_TAG]);
111 
112  return $schemaContent;
113  }
114 
124  abstract protected function generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $requestUri);
125 
132  abstract protected function getServiceMetadata($serviceName);
133 
140  public function getElementComplexTypeName($messageName)
141  {
142  return ucfirst($messageName);
143  }
144 
153  protected function collectCallInfo($requestedServiceMetadata)
154  {
155  foreach ($requestedServiceMetadata as $serviceName => $serviceData) {
156  foreach ($serviceData['methods'] as $methodName => $methodData) {
157  $this->typeProcessor->processInterfaceCallInfo($methodData['interface'], $serviceName, $methodName);
158  }
159  }
160  }
161 
168  protected function getAllowedServicesMetadata($requestedServices)
169  {
170  $allowedServicesMetadata = [];
171  foreach ($requestedServices as $serviceName) {
172  $serviceMetadata = $this->getServiceMetadata($serviceName);
173  foreach ($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS] as $methodName => $methodData) {
174  if (!$this->authorization->isAllowed($methodData[ServiceMetadata::KEY_ACL_RESOURCES])) {
176  }
177  }
180  $allowedServicesMetadata[$serviceName] = $serviceMetadata;
181  }
182  }
183  return $allowedServicesMetadata;
184  }
185 
193  {
194  $allowedMethodNames = array_keys($serviceMetadata[ServiceMetadata::KEY_SERVICE_METHODS]);
197  foreach ($serviceMetadata[ServiceMetadata::KEY_ROUTES] as $path => &$routeGroup) {
198  foreach ($routeGroup as $httpMethod => &$route) {
199  if (!in_array($route[ServiceMetadata::KEY_ROUTE_METHOD], $allowedMethodNames)) {
200  unset($routeGroup[$httpMethod]);
201  }
202  }
203  if (empty($routeGroup)) {
205  }
206  }
207  }
208  }
209 }
generateSchema($requestedServiceMetadata, $requestScheme, $requestHost, $requestUri)
__construct(Webapi $cache, \Magento\Framework\Reflection\TypeProcessor $typeProcessor, ServiceTypeListInterface $serviceTypeList, ServiceMetadata $serviceMetadata, Authorization $authorization, Json $serializer=null)
$prefix
Definition: name.phtml:25
generate($requestedServices, $requestScheme, $requestHost, $endPointUrl)