Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
ServiceMetadata Class Reference

Public Member Functions

 __construct (\Magento\Webapi\Model\Config $config, WebApiCache $cache, \Magento\Webapi\Model\Config\ClassReflector $classReflector, \Magento\Framework\Reflection\TypeProcessor $typeProcessor, SerializerInterface $serializer=null)
 
 getServicesConfig ()
 
 getServiceMetadata ($serviceName)
 
 getServiceName ($interfaceName, $version, $preserveVersion=true)
 
 getRouteMetadata ($serviceName)
 
 getRoutesConfig ()
 

Data Fields

const KEY_CLASS = 'class'
 
const KEY_IS_SECURE = 'isSecure'
 
const KEY_SERVICE_METHODS = 'methods'
 
const KEY_METHOD = 'method'
 
const KEY_IS_REQUIRED = 'inputRequired'
 
const KEY_ACL_RESOURCES = 'resources'
 
const KEY_ROUTES = 'routes'
 
const KEY_ROUTE_METHOD = 'method'
 
const KEY_ROUTE_PARAMS = 'parameters'
 
const SERVICES_CONFIG_CACHE_ID = 'services-services-config'
 
const ROUTES_CONFIG_CACHE_ID = 'routes-services-config'
 
const REFLECTED_TYPES_CACHE_ID = 'soap-reflected-types'
 

Protected Member Functions

 initServicesMetadata ()
 
 initRoutesMetadata ()
 

Protected Attributes

 $services
 
 $routes
 
 $cache
 
 $config
 
 $classReflector
 
 $typeProcessor
 

Detailed Description

Service Metadata Model

Definition at line 16 of file ServiceMetadata.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Webapi\Model\Config  $config,
WebApiCache  $cache,
\Magento\Webapi\Model\Config\ClassReflector  $classReflector,
\Magento\Framework\Reflection\TypeProcessor  $typeProcessor,
SerializerInterface  $serializer = null 
)

Initialize dependencies.

Parameters
\Magento\Webapi\Model\Config$config
WebApiCache$cache
\Magento\Webapi\Model\Config\ClassReflector$classReflector
\Magento\Framework\Reflection\TypeProcessor$typeProcessor
SerializerInterface | null$serializer

Definition at line 91 of file ServiceMetadata.php.

97  {
98  $this->config = $config;
99  $this->cache = $cache;
100  $this->classReflector = $classReflector;
101  $this->typeProcessor = $typeProcessor;
102  $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
103  }

Member Function Documentation

◆ getRouteMetadata()

getRouteMetadata (   $serviceName)

Retrieve specific service interface data with route.

Parameters
string$serviceName
Returns
array
Exceptions

Definition at line 253 of file ServiceMetadata.php.

254  {
255  $routesConfig = $this->getRoutesConfig();
256  if (!isset($routesConfig[$serviceName]) || !is_array($routesConfig[$serviceName])) {
257  throw new \RuntimeException(__('Requested service is not available: "%1"', $serviceName));
258  }
259  return $routesConfig[$serviceName];
260  }
__()
Definition: __.php:13

◆ getRoutesConfig()

getRoutesConfig ( )

Return routes loaded from cache if enabled or from files merged previously

Returns
array

Definition at line 267 of file ServiceMetadata.php.

268  {
269  if (null === $this->routes) {
270  $routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID);
271  $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
272  if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) {
273  $this->routes = $this->serializer->unserialize($routesConfig);
274  $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
275  } else {
276  $this->routes = $this->initRoutesMetadata();
277  $this->cache->save(
278  $this->serializer->serialize($this->routes),
280  );
281  $this->cache->save(
282  $this->serializer->serialize($this->typeProcessor->getTypesData()),
283  self::REFLECTED_TYPES_CACHE_ID
284  );
285  }
286  }
287  return $this->routes;
288  }

◆ getServiceMetadata()

getServiceMetadata (   $serviceName)

Retrieve specific service interface data.

Parameters
string$serviceName
Returns
array
Exceptions

Definition at line 177 of file ServiceMetadata.php.

178  {
179  $servicesConfig = $this->getServicesConfig();
180  if (!isset($servicesConfig[$serviceName]) || !is_array($servicesConfig[$serviceName])) {
181  throw new \RuntimeException(__('Requested service is not available: "%1"', $serviceName));
182  }
183  return $servicesConfig[$serviceName];
184  }
__()
Definition: __.php:13

◆ getServiceName()

getServiceName (   $interfaceName,
  $version,
  $preserveVersion = true 
)

Translate service interface name into service name.

Example:

  • \Magento\Customer\Api\CustomerAccountInterface::class, 'V1', false => customerCustomerAccount
  • \Magento\Customer\Api\CustomerAddressInterface::class, 'V1', true => customerCustomerAddressV1
Parameters
string$interfaceName
string$version
bool$preserveVersionShould version be preserved during interface name conversion into service name
Returns
string
Exceptions

Avoid duplication of words in service name

Avoid duplication of words in service name

Definition at line 202 of file ServiceMetadata.php.

203  {
204  if (!preg_match(\Magento\Webapi\Model\Config::SERVICE_CLASS_PATTERN, $interfaceName, $matches)) {
205  $apiClassPattern = "#^(.+?)\\\\(.+?)\\\\Api\\\\(.+?)(Interface)?$#";
206  preg_match($apiClassPattern, $interfaceName, $matches);
207  }
208 
209  if (!empty($matches)) {
210  $moduleNamespace = $matches[1];
211  $moduleName = $matches[2];
212  $moduleNamespace = ($moduleNamespace == 'Magento') ? '' : $moduleNamespace;
213  if ($matches[4] === 'Interface') {
214  $matches[4] = $matches[3];
215  }
216  $serviceNameParts = explode('\\', trim($matches[4], '\\'));
217  if ($moduleName == $serviceNameParts[0]) {
219  $moduleName = '';
220  }
221  $parentServiceName = $moduleNamespace . $moduleName . array_shift($serviceNameParts);
222  array_unshift($serviceNameParts, $parentServiceName);
223  if ($preserveVersion) {
224  $serviceNameParts[] = $version;
225  }
226  } elseif (preg_match(\Magento\Webapi\Model\Config::API_PATTERN, $interfaceName, $matches)) {
227  $moduleNamespace = $matches[1];
228  $moduleName = $matches[2];
229  $moduleNamespace = ($moduleNamespace == 'Magento') ? '' : $moduleNamespace;
230  $serviceNameParts = explode('\\', trim($matches[3], '\\'));
231  if ($moduleName == $serviceNameParts[0]) {
233  $moduleName = '';
234  }
235  $parentServiceName = $moduleNamespace . $moduleName . array_shift($serviceNameParts);
236  array_unshift($serviceNameParts, $parentServiceName);
237  if ($preserveVersion) {
238  $serviceNameParts[] = $version;
239  }
240  } else {
241  throw new \InvalidArgumentException(sprintf('The service interface name "%s" is invalid.', $interfaceName));
242  }
243  return lcfirst(implode('', $serviceNameParts));
244  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ getServicesConfig()

getServicesConfig ( )

Return services loaded from cache if enabled or from files merged previously

Returns
array

Definition at line 147 of file ServiceMetadata.php.

148  {
149  if (null === $this->services) {
150  $servicesConfig = $this->cache->load(self::SERVICES_CONFIG_CACHE_ID);
151  $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
152  if ($servicesConfig && is_string($servicesConfig) && $typesData && is_string($typesData)) {
153  $this->services = $this->serializer->unserialize($servicesConfig);
154  $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
155  } else {
156  $this->services = $this->initServicesMetadata();
157  $this->cache->save(
158  $this->serializer->serialize($this->services),
160  );
161  $this->cache->save(
162  $this->serializer->serialize($this->typeProcessor->getTypesData()),
163  self::REFLECTED_TYPES_CACHE_ID
164  );
165  }
166  }
167  return $this->services;
168  }

◆ initRoutesMetadata()

initRoutesMetadata ( )
protected

Collect the list of services with routes and request types for use in REST.

Returns
array

Definition at line 295 of file ServiceMetadata.php.

296  {
297  $routes = $this->getServicesConfig();
298  foreach ($this->config->getServices()[Converter::KEY_ROUTES] as $url => $routeData) {
299  foreach ($routeData as $method => $data) {
301  $version = explode('/', ltrim($url, '/'))[0];
302  $serviceName = $this->getServiceName($serviceClass, $version);
304  $routes[$serviceName][self::KEY_ROUTES][$url][$method][self::KEY_ROUTE_METHOD] = $methodName;
307  }
308  }
309  return $routes;
310  }
getServiceName($interfaceName, $version, $preserveVersion=true)
$method
Definition: info.phtml:13

◆ initServicesMetadata()

initServicesMetadata ( )
protected

Collect the list of services metadata

Returns
array

Definition at line 110 of file ServiceMetadata.php.

111  {
112  $services = [];
113  foreach ($this->config->getServices()[Converter::KEY_SERVICES] as $serviceClass => $serviceVersionData) {
114  foreach ($serviceVersionData as $version => $serviceData) {
115  $serviceName = $this->getServiceName($serviceClass, $version);
116  foreach ($serviceData[Converter::KEY_METHODS] as $methodName => $methodMetadata) {
117  $services[$serviceName][self::KEY_SERVICE_METHODS][$methodName] = [
118  self::KEY_METHOD => $methodName,
119  self::KEY_IS_REQUIRED => (bool)$methodMetadata[Converter::KEY_SECURE],
120  self::KEY_IS_SECURE => $methodMetadata[Converter::KEY_SECURE],
121  self::KEY_ACL_RESOURCES => $methodMetadata[Converter::KEY_ACL_RESOURCES],
122  ];
123  $services[$serviceName][self::KEY_CLASS] = $serviceClass;
124  }
125  $reflectedMethodsMetadata = $this->classReflector->reflectClassMethods(
126  $serviceClass,
127  $services[$serviceName][self::KEY_SERVICE_METHODS]
128  );
129  $services[$serviceName][self::KEY_SERVICE_METHODS] = array_merge_recursive(
130  $services[$serviceName][self::KEY_SERVICE_METHODS],
131  $reflectedMethodsMetadata
132  );
133  $services[$serviceName][Converter::KEY_DESCRIPTION] = $this->classReflector->extractClassDescription(
134  $serviceClass
135  );
136  }
137  }
138 
139  return $services;
140  }
getServiceName($interfaceName, $version, $preserveVersion=true)

Field Documentation

◆ $cache

$cache
protected

Definition at line 60 of file ServiceMetadata.php.

◆ $classReflector

$classReflector
protected

Definition at line 70 of file ServiceMetadata.php.

◆ $config

$config
protected

Definition at line 65 of file ServiceMetadata.php.

◆ $routes

$routes
protected

Definition at line 55 of file ServiceMetadata.php.

◆ $services

$services
protected

#- #-

Definition at line 48 of file ServiceMetadata.php.

◆ $typeProcessor

$typeProcessor
protected

Definition at line 75 of file ServiceMetadata.php.

◆ KEY_ACL_RESOURCES

const KEY_ACL_RESOURCES = 'resources'

Definition at line 31 of file ServiceMetadata.php.

◆ KEY_CLASS

const KEY_CLASS = 'class'

#+ Keys that a used for service config internal representation.

Definition at line 21 of file ServiceMetadata.php.

◆ KEY_IS_REQUIRED

const KEY_IS_REQUIRED = 'inputRequired'

Definition at line 29 of file ServiceMetadata.php.

◆ KEY_IS_SECURE

const KEY_IS_SECURE = 'isSecure'

Definition at line 23 of file ServiceMetadata.php.

◆ KEY_METHOD

const KEY_METHOD = 'method'

Definition at line 27 of file ServiceMetadata.php.

◆ KEY_ROUTE_METHOD

const KEY_ROUTE_METHOD = 'method'

Definition at line 35 of file ServiceMetadata.php.

◆ KEY_ROUTE_PARAMS

const KEY_ROUTE_PARAMS = 'parameters'

Definition at line 37 of file ServiceMetadata.php.

◆ KEY_ROUTES

const KEY_ROUTES = 'routes'

Definition at line 33 of file ServiceMetadata.php.

◆ KEY_SERVICE_METHODS

const KEY_SERVICE_METHODS = 'methods'

Definition at line 25 of file ServiceMetadata.php.

◆ REFLECTED_TYPES_CACHE_ID

const REFLECTED_TYPES_CACHE_ID = 'soap-reflected-types'

Definition at line 43 of file ServiceMetadata.php.

◆ ROUTES_CONFIG_CACHE_ID

const ROUTES_CONFIG_CACHE_ID = 'routes-services-config'

Definition at line 41 of file ServiceMetadata.php.

◆ SERVICES_CONFIG_CACHE_ID

const SERVICES_CONFIG_CACHE_ID = 'services-services-config'

Definition at line 39 of file ServiceMetadata.php.


The documentation for this class was generated from the following file: