Config.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\ResourceConnection;
  7. use Magento\Framework\Config\ConfigOptionsListConstants;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. /**
  10. * Resource configuration, uses application configuration to retrieve resource connection information
  11. */
  12. class Config extends \Magento\Framework\Config\Data\Scoped implements ConfigInterface
  13. {
  14. /**
  15. * List of connection names per resource
  16. *
  17. * @var array
  18. */
  19. protected $_connectionNames = [];
  20. /**
  21. * @var \Magento\Framework\App\DeploymentConfig
  22. */
  23. private $deploymentConfig;
  24. /**
  25. * @var bool
  26. */
  27. private $initialized = false;
  28. /**
  29. * Constructor
  30. *
  31. * @param Config\Reader $reader
  32. * @param \Magento\Framework\Config\ScopeInterface $configScope
  33. * @param \Magento\Framework\Config\CacheInterface $cache
  34. * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
  35. * @param string|null $cacheId
  36. * @param SerializerInterface|null $serializer
  37. * @throws \InvalidArgumentException
  38. */
  39. public function __construct(
  40. Config\Reader $reader,
  41. \Magento\Framework\Config\ScopeInterface $configScope,
  42. \Magento\Framework\Config\CacheInterface $cache,
  43. \Magento\Framework\App\DeploymentConfig $deploymentConfig,
  44. $cacheId = 'resourcesCache',
  45. SerializerInterface $serializer = null
  46. ) {
  47. parent::__construct($reader, $configScope, $cache, $cacheId, $serializer);
  48. $this->deploymentConfig = $deploymentConfig;
  49. }
  50. /**
  51. * Retrieve resource connection instance name
  52. *
  53. * @param string $resourceName
  54. * @return string
  55. */
  56. public function getConnectionName($resourceName)
  57. {
  58. $this->initConnections();
  59. $connectionName = \Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION;
  60. if (!isset($this->_connectionNames[$resourceName])) {
  61. $resourcesConfig = $this->get();
  62. $pointerResourceName = $resourceName;
  63. while (true) {
  64. if (isset($resourcesConfig[$pointerResourceName]['connection'])) {
  65. $connectionName = $resourcesConfig[$pointerResourceName]['connection'];
  66. $this->_connectionNames[$resourceName] = $connectionName;
  67. break;
  68. } elseif (isset($this->_connectionNames[$pointerResourceName])) {
  69. $this->_connectionNames[$resourceName] = $this->_connectionNames[$pointerResourceName];
  70. $connectionName = $this->_connectionNames[$resourceName];
  71. break;
  72. } elseif (isset($resourcesConfig[$pointerResourceName]['extends'])) {
  73. $pointerResourceName = $resourcesConfig[$pointerResourceName]['extends'];
  74. } else {
  75. break;
  76. }
  77. }
  78. } else {
  79. $connectionName = $this->_connectionNames[$resourceName];
  80. }
  81. return $connectionName;
  82. }
  83. /**
  84. * Initialise connections
  85. *
  86. * @return void
  87. */
  88. private function initConnections()
  89. {
  90. if (!$this->initialized) {
  91. $this->initialized = true;
  92. $resource = $this->deploymentConfig->getConfigData(ConfigOptionsListConstants::KEY_RESOURCE) ?: [];
  93. foreach ($resource as $resourceName => $resourceData) {
  94. if (!isset($resourceData['connection'])) {
  95. throw new \InvalidArgumentException('Invalid initial resource configuration');
  96. }
  97. $this->_connectionNames[$resourceName] = $resourceData['connection'];
  98. }
  99. }
  100. }
  101. }