Config.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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\Webapi\Model\Cache\Type\Webapi as WebapiCache;
  8. use Magento\Webapi\Model\Config\Reader;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\Serialize\SerializerInterface;
  11. /**
  12. * This class gives access to consolidated web API configuration from <Module_Name>/etc/webapi.xml files.
  13. *
  14. * @api
  15. * @since 100.0.2
  16. */
  17. class Config implements ConfigInterface
  18. {
  19. const CACHE_ID = 'webapi_config';
  20. /**
  21. * Pattern for Web API interface name.
  22. */
  23. const SERVICE_CLASS_PATTERN = '/^(.+?)\\\\(.+?)\\\\Service\\\\(V\d+)+(\\\\.+)Interface$/';
  24. const API_PATTERN = '/^(.+?)\\\\(.+?)\\\\Api(\\\\.+)Interface$/';
  25. /**
  26. * @var WebapiCache
  27. */
  28. protected $cache;
  29. /**
  30. * @var Reader
  31. */
  32. protected $configReader;
  33. /**
  34. * @var array
  35. */
  36. protected $services;
  37. /**
  38. * @var SerializerInterface
  39. */
  40. private $serializer;
  41. /**
  42. * Initialize dependencies.
  43. *
  44. * @param WebapiCache $cache
  45. * @param Reader $configReader
  46. * @param SerializerInterface|null $serializer
  47. */
  48. public function __construct(
  49. WebapiCache $cache,
  50. Reader $configReader,
  51. SerializerInterface $serializer = null
  52. ) {
  53. $this->cache = $cache;
  54. $this->configReader = $configReader;
  55. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getServices()
  61. {
  62. if (null === $this->services) {
  63. $services = $this->cache->load(self::CACHE_ID);
  64. if ($services && is_string($services)) {
  65. $this->services = $this->serializer->unserialize($services);
  66. } else {
  67. $this->services = $this->configReader->read();
  68. $this->cache->save($this->serializer->serialize($this->services), self::CACHE_ID);
  69. }
  70. }
  71. return $this->services;
  72. }
  73. }