ConsolidatedConfig.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Integration\Model;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Integration\Model\Cache\TypeConsolidated;
  10. /**
  11. * ConsolidatedConfig to deliver information for config-based integrations that use integration.xml
  12. */
  13. class ConsolidatedConfig
  14. {
  15. const CACHE_ID = 'integration-consolidated';
  16. /**
  17. * @var \Magento\Framework\App\Cache\Type\Config
  18. */
  19. protected $configCacheType;
  20. /**
  21. * @var \Magento\Integration\Model\Config\Consolidated\Reader
  22. */
  23. protected $configReader;
  24. /**
  25. * Array of integrations
  26. *
  27. * @var array
  28. */
  29. protected $integrations;
  30. /**
  31. * @var SerializerInterface
  32. */
  33. private $serializer;
  34. /**
  35. * @param Cache\TypeConsolidated $configCacheType
  36. * @param Config\Consolidated\Reader $configReader
  37. * @param SerializerInterface $serializer
  38. */
  39. public function __construct(
  40. Cache\TypeConsolidated $configCacheType,
  41. Config\Consolidated\Reader $configReader,
  42. SerializerInterface $serializer = null
  43. ) {
  44. $this->configCacheType = $configCacheType;
  45. $this->configReader = $configReader;
  46. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(SerializerInterface::class);
  47. }
  48. /**
  49. * Return integrations loaded from cache if enabled or from files merged previously
  50. *
  51. * @return array
  52. */
  53. public function getIntegrations()
  54. {
  55. if (null === $this->integrations) {
  56. $integrations = $this->configCacheType->load(self::CACHE_ID);
  57. if ($integrations && is_string($integrations)) {
  58. $this->integrations = $this->serializer->unserialize($integrations);
  59. } else {
  60. $this->integrations = $this->configReader->read();
  61. $this->configCacheType->save(
  62. $this->serializer->serialize($this->integrations),
  63. self::CACHE_ID,
  64. [TypeConsolidated::CACHE_TAG]
  65. );
  66. }
  67. }
  68. return $this->integrations;
  69. }
  70. }