ServiceMetadata.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Webapi\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Webapi\Model\Cache\Type\Webapi as WebApiCache;
  10. use Magento\Webapi\Model\Config\Converter;
  11. /**
  12. * Service Metadata Model
  13. */
  14. class ServiceMetadata
  15. {
  16. /**#@+
  17. * Keys that a used for service config internal representation.
  18. */
  19. const KEY_CLASS = 'class';
  20. const KEY_IS_SECURE = 'isSecure';
  21. const KEY_SERVICE_METHODS = 'methods';
  22. const KEY_METHOD = 'method';
  23. const KEY_IS_REQUIRED = 'inputRequired';
  24. const KEY_ACL_RESOURCES = 'resources';
  25. const KEY_ROUTES = 'routes';
  26. const KEY_ROUTE_METHOD = 'method';
  27. const KEY_ROUTE_PARAMS = 'parameters';
  28. const SERVICES_CONFIG_CACHE_ID = 'services-services-config';
  29. const ROUTES_CONFIG_CACHE_ID = 'routes-services-config';
  30. const REFLECTED_TYPES_CACHE_ID = 'soap-reflected-types';
  31. /**#@-*/
  32. /**#@-*/
  33. protected $services;
  34. /**
  35. * List of services with route data
  36. *
  37. * @var array
  38. */
  39. protected $routes;
  40. /**
  41. * @var WebApiCache
  42. */
  43. protected $cache;
  44. /**
  45. * @var \Magento\Webapi\Model\Config
  46. */
  47. protected $config;
  48. /**
  49. * @var \Magento\Webapi\Model\Config\ClassReflector
  50. */
  51. protected $classReflector;
  52. /**
  53. * @var \Magento\Framework\Reflection\TypeProcessor
  54. */
  55. protected $typeProcessor;
  56. /**
  57. * @var SerializerInterface
  58. */
  59. private $serializer;
  60. /**
  61. * Initialize dependencies.
  62. *
  63. * @param \Magento\Webapi\Model\Config $config
  64. * @param WebApiCache $cache
  65. * @param \Magento\Webapi\Model\Config\ClassReflector $classReflector
  66. * @param \Magento\Framework\Reflection\TypeProcessor $typeProcessor
  67. * @param SerializerInterface|null $serializer
  68. */
  69. public function __construct(
  70. \Magento\Webapi\Model\Config $config,
  71. WebApiCache $cache,
  72. \Magento\Webapi\Model\Config\ClassReflector $classReflector,
  73. \Magento\Framework\Reflection\TypeProcessor $typeProcessor,
  74. SerializerInterface $serializer = null
  75. ) {
  76. $this->config = $config;
  77. $this->cache = $cache;
  78. $this->classReflector = $classReflector;
  79. $this->typeProcessor = $typeProcessor;
  80. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  81. }
  82. /**
  83. * Collect the list of services metadata
  84. *
  85. * @return array
  86. */
  87. protected function initServicesMetadata()
  88. {
  89. $services = [];
  90. foreach ($this->config->getServices()[Converter::KEY_SERVICES] as $serviceClass => $serviceVersionData) {
  91. foreach ($serviceVersionData as $version => $serviceData) {
  92. $serviceName = $this->getServiceName($serviceClass, $version);
  93. foreach ($serviceData[Converter::KEY_METHODS] as $methodName => $methodMetadata) {
  94. $services[$serviceName][self::KEY_SERVICE_METHODS][$methodName] = [
  95. self::KEY_METHOD => $methodName,
  96. self::KEY_IS_REQUIRED => (bool)$methodMetadata[Converter::KEY_SECURE],
  97. self::KEY_IS_SECURE => $methodMetadata[Converter::KEY_SECURE],
  98. self::KEY_ACL_RESOURCES => $methodMetadata[Converter::KEY_ACL_RESOURCES],
  99. ];
  100. $services[$serviceName][self::KEY_CLASS] = $serviceClass;
  101. }
  102. $reflectedMethodsMetadata = $this->classReflector->reflectClassMethods(
  103. $serviceClass,
  104. $services[$serviceName][self::KEY_SERVICE_METHODS]
  105. );
  106. $services[$serviceName][self::KEY_SERVICE_METHODS] = array_merge_recursive(
  107. $services[$serviceName][self::KEY_SERVICE_METHODS],
  108. $reflectedMethodsMetadata
  109. );
  110. $services[$serviceName][Converter::KEY_DESCRIPTION] = $this->classReflector->extractClassDescription(
  111. $serviceClass
  112. );
  113. }
  114. }
  115. return $services;
  116. }
  117. /**
  118. * Return services loaded from cache if enabled or from files merged previously
  119. *
  120. * @return array
  121. */
  122. public function getServicesConfig()
  123. {
  124. if (null === $this->services) {
  125. $servicesConfig = $this->cache->load(self::SERVICES_CONFIG_CACHE_ID);
  126. $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
  127. if ($servicesConfig && is_string($servicesConfig) && $typesData && is_string($typesData)) {
  128. $this->services = $this->serializer->unserialize($servicesConfig);
  129. $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
  130. } else {
  131. $this->services = $this->initServicesMetadata();
  132. $this->cache->save(
  133. $this->serializer->serialize($this->services),
  134. self::SERVICES_CONFIG_CACHE_ID
  135. );
  136. $this->cache->save(
  137. $this->serializer->serialize($this->typeProcessor->getTypesData()),
  138. self::REFLECTED_TYPES_CACHE_ID
  139. );
  140. }
  141. }
  142. return $this->services;
  143. }
  144. /**
  145. * Retrieve specific service interface data.
  146. *
  147. * @param string $serviceName
  148. * @return array
  149. * @throws \RuntimeException
  150. */
  151. public function getServiceMetadata($serviceName)
  152. {
  153. $servicesConfig = $this->getServicesConfig();
  154. if (!isset($servicesConfig[$serviceName]) || !is_array($servicesConfig[$serviceName])) {
  155. throw new \RuntimeException(__('Requested service is not available: "%1"', $serviceName));
  156. }
  157. return $servicesConfig[$serviceName];
  158. }
  159. /**
  160. * Translate service interface name into service name.
  161. *
  162. * Example:
  163. * <pre>
  164. * - \Magento\Customer\Api\CustomerAccountInterface::class, 'V1', false => customerCustomerAccount
  165. * - \Magento\Customer\Api\CustomerAddressInterface::class, 'V1', true => customerCustomerAddressV1
  166. * </pre>
  167. *
  168. * @param string $interfaceName
  169. * @param string $version
  170. * @param bool $preserveVersion Should version be preserved during interface name conversion into service name
  171. * @return string
  172. * @throws \InvalidArgumentException
  173. * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  174. */
  175. public function getServiceName($interfaceName, $version, $preserveVersion = true)
  176. {
  177. if (!preg_match(\Magento\Webapi\Model\Config::SERVICE_CLASS_PATTERN, $interfaceName, $matches)) {
  178. $apiClassPattern = "#^(.+?)\\\\(.+?)\\\\Api\\\\(.+?)(Interface)?$#";
  179. preg_match($apiClassPattern, $interfaceName, $matches);
  180. }
  181. if (!empty($matches)) {
  182. $moduleNamespace = $matches[1];
  183. $moduleName = $matches[2];
  184. $moduleNamespace = ($moduleNamespace == 'Magento') ? '' : $moduleNamespace;
  185. if ($matches[4] === 'Interface') {
  186. $matches[4] = $matches[3];
  187. }
  188. $serviceNameParts = explode('\\', trim($matches[4], '\\'));
  189. if ($moduleName == $serviceNameParts[0]) {
  190. /** Avoid duplication of words in service name */
  191. $moduleName = '';
  192. }
  193. $parentServiceName = $moduleNamespace . $moduleName . array_shift($serviceNameParts);
  194. array_unshift($serviceNameParts, $parentServiceName);
  195. if ($preserveVersion) {
  196. $serviceNameParts[] = $version;
  197. }
  198. } elseif (preg_match(\Magento\Webapi\Model\Config::API_PATTERN, $interfaceName, $matches)) {
  199. $moduleNamespace = $matches[1];
  200. $moduleName = $matches[2];
  201. $moduleNamespace = ($moduleNamespace == 'Magento') ? '' : $moduleNamespace;
  202. $serviceNameParts = explode('\\', trim($matches[3], '\\'));
  203. if ($moduleName == $serviceNameParts[0]) {
  204. /** Avoid duplication of words in service name */
  205. $moduleName = '';
  206. }
  207. $parentServiceName = $moduleNamespace . $moduleName . array_shift($serviceNameParts);
  208. array_unshift($serviceNameParts, $parentServiceName);
  209. if ($preserveVersion) {
  210. $serviceNameParts[] = $version;
  211. }
  212. } else {
  213. throw new \InvalidArgumentException(sprintf('The service interface name "%s" is invalid.', $interfaceName));
  214. }
  215. return lcfirst(implode('', $serviceNameParts));
  216. }
  217. /**
  218. * Retrieve specific service interface data with route.
  219. *
  220. * @param string $serviceName
  221. * @return array
  222. * @throws \RuntimeException
  223. */
  224. public function getRouteMetadata($serviceName)
  225. {
  226. $routesConfig = $this->getRoutesConfig();
  227. if (!isset($routesConfig[$serviceName]) || !is_array($routesConfig[$serviceName])) {
  228. throw new \RuntimeException(__('Requested service is not available: "%1"', $serviceName));
  229. }
  230. return $routesConfig[$serviceName];
  231. }
  232. /**
  233. * Return routes loaded from cache if enabled or from files merged previously
  234. *
  235. * @return array
  236. */
  237. public function getRoutesConfig()
  238. {
  239. if (null === $this->routes) {
  240. $routesConfig = $this->cache->load(self::ROUTES_CONFIG_CACHE_ID);
  241. $typesData = $this->cache->load(self::REFLECTED_TYPES_CACHE_ID);
  242. if ($routesConfig && is_string($routesConfig) && $typesData && is_string($typesData)) {
  243. $this->routes = $this->serializer->unserialize($routesConfig);
  244. $this->typeProcessor->setTypesData($this->serializer->unserialize($typesData));
  245. } else {
  246. $this->routes = $this->initRoutesMetadata();
  247. $this->cache->save(
  248. $this->serializer->serialize($this->routes),
  249. self::ROUTES_CONFIG_CACHE_ID
  250. );
  251. $this->cache->save(
  252. $this->serializer->serialize($this->typeProcessor->getTypesData()),
  253. self::REFLECTED_TYPES_CACHE_ID
  254. );
  255. }
  256. }
  257. return $this->routes;
  258. }
  259. /**
  260. * Collect the list of services with routes and request types for use in REST.
  261. *
  262. * @return array
  263. */
  264. protected function initRoutesMetadata()
  265. {
  266. $routes = $this->getServicesConfig();
  267. foreach ($this->config->getServices()[Converter::KEY_ROUTES] as $url => $routeData) {
  268. foreach ($routeData as $method => $data) {
  269. $serviceClass = $data[Converter::KEY_SERVICE][Converter::KEY_SERVICE_CLASS];
  270. $version = explode('/', ltrim($url, '/'))[0];
  271. $serviceName = $this->getServiceName($serviceClass, $version);
  272. $methodName = $data[Converter::KEY_SERVICE][Converter::KEY_METHOD];
  273. $routes[$serviceName][self::KEY_ROUTES][$url][$method][self::KEY_ROUTE_METHOD] = $methodName;
  274. $routes[$serviceName][self::KEY_ROUTES][$url][$method][self::KEY_ROUTE_PARAMS]
  275. = $data[Converter::KEY_DATA_PARAMETERS];
  276. }
  277. }
  278. return $routes;
  279. }
  280. }