ServiceConfig.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\WebapiAsync\Model\ServiceConfig\Converter;
  11. use Magento\WebapiAsync\Model\ServiceConfig\Reader;
  12. /**
  13. * This class gives access to consolidated web API configuration from <Module_Name>/etc/webapi_async.xml files.
  14. *
  15. * @api
  16. * @since 100.2.0
  17. */
  18. class ServiceConfig
  19. {
  20. const CACHE_ID = 'webapi_async_service_config';
  21. /**
  22. * @var WebapiCache
  23. */
  24. private $cache;
  25. /**
  26. * @var Reader
  27. */
  28. private $configReader;
  29. /**
  30. * @var array
  31. */
  32. private $services;
  33. /**
  34. * @var SerializerInterface
  35. */
  36. private $serializer;
  37. /**
  38. * Initialize dependencies.
  39. *
  40. * @param WebapiCache $cache
  41. * @param Reader $configReader
  42. * @param SerializerInterface $serializer
  43. */
  44. public function __construct(
  45. WebapiCache $cache,
  46. Reader $configReader,
  47. SerializerInterface $serializer
  48. ) {
  49. $this->cache = $cache;
  50. $this->configReader = $configReader;
  51. $this->serializer = $serializer;
  52. }
  53. /**
  54. * Return services loaded from cache if enabled or from files merged previously
  55. *
  56. * @return array
  57. * @since 100.2.0
  58. */
  59. public function getServices()
  60. {
  61. if (null === $this->services) {
  62. $services = $this->cache->load(self::CACHE_ID);
  63. if ($services && is_string($services)) {
  64. $this->services = $this->serializer->unserialize($services);
  65. } else {
  66. $this->services = $this->configReader->read();
  67. $this->cache->save($this->serializer->serialize($this->services), self::CACHE_ID);
  68. }
  69. }
  70. return $this->services;
  71. }
  72. }