BulkServiceConfig.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Model;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Webapi\Model\Cache\Type\Webapi as WebapiCache;
  10. use Magento\Webapi\Model\Config\Converter as WebapiConverter;
  11. use Magento\Webapi\Model\Config;
  12. /**
  13. * @api
  14. * @since 100.2.0
  15. */
  16. class BulkServiceConfig implements \Magento\Webapi\Model\ConfigInterface
  17. {
  18. const CACHE_ID = 'webapi_bulk_async_service_config';
  19. const URL_PARAM_PREFIX_PLACEHOLDER = 'by';
  20. /**
  21. * @var WebapiCache
  22. */
  23. private $cache;
  24. /**
  25. * @var Config
  26. */
  27. private $webapiConfig;
  28. /**
  29. * @var array
  30. */
  31. private $services;
  32. /**
  33. * @var SerializerInterface
  34. */
  35. private $serializer;
  36. /**
  37. * Initialize dependencies.
  38. *
  39. * @param WebapiCache $cache
  40. * @param Config $webapiConfig
  41. * @param SerializerInterface $serializer
  42. */
  43. public function __construct(
  44. WebapiCache $cache,
  45. Config $webapiConfig,
  46. SerializerInterface $serializer
  47. ) {
  48. $this->cache = $cache;
  49. $this->webapiConfig = $webapiConfig;
  50. $this->serializer = $serializer;
  51. }
  52. /**
  53. * Return services loaded from cache if enabled or from files merged previously
  54. *
  55. * @return array
  56. * @since 100.2.0
  57. */
  58. public function getServices()
  59. {
  60. if (null === $this->services) {
  61. $services = $this->cache->load(self::CACHE_ID);
  62. if ($services && is_string($services)) {
  63. $this->services = $this->serializer->unserialize($services);
  64. } else {
  65. $this->services = $this->getBulkServicesConfig();
  66. $this->cache->save($this->serializer->serialize($this->services), self::CACHE_ID);
  67. }
  68. }
  69. return $this->services;
  70. }
  71. /**
  72. * @return array
  73. */
  74. private function getBulkServicesConfig()
  75. {
  76. $bulkServices = [];
  77. $webapiServices = $this->webapiConfig->getServices();
  78. foreach ($webapiServices[WebapiConverter::KEY_ROUTES] as $routePath => $routeConfig) {
  79. foreach ($routeConfig as $httpMethod => $httpMethodConfig) {
  80. if ($httpMethod !== \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET) {
  81. $routePath = preg_replace_callback(
  82. '/\/:(\w+)/',
  83. function ($matches) {
  84. return '/' . self::URL_PARAM_PREFIX_PLACEHOLDER . ucfirst($matches[1]);
  85. },
  86. $routePath
  87. );
  88. $bulkServices[WebapiConverter::KEY_ROUTES][$routePath][$httpMethod] = $httpMethodConfig;
  89. }
  90. }
  91. }
  92. $bulkServices[WebapiConverter::KEY_SERVICES] = $webapiServices[WebapiConverter::KEY_SERVICES];
  93. return $bulkServices;
  94. }
  95. }