RuntimeConfigSource.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\App\Config\Source;
  7. use Magento\Framework\App\Config\ConfigSourceInterface;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\DB\Adapter\AdapterInterface;
  11. /**
  12. * Config source. Retrieve all configuration for scopes from db
  13. */
  14. class RuntimeConfigSource implements ConfigSourceInterface
  15. {
  16. /**
  17. * @var DeploymentConfig
  18. */
  19. private $deploymentConfig;
  20. /**
  21. * @var ResourceConnection
  22. */
  23. private $resourceConnection;
  24. /**
  25. * @var AdapterInterface
  26. */
  27. private $connection;
  28. /**
  29. * @param DeploymentConfig $deploymentConfig
  30. * @param ResourceConnection $resourceConnection
  31. */
  32. public function __construct(
  33. DeploymentConfig $deploymentConfig,
  34. ResourceConnection $resourceConnection
  35. ) {
  36. $this->deploymentConfig = $deploymentConfig;
  37. $this->resourceConnection = $resourceConnection;
  38. }
  39. /**
  40. * Return whole scopes config data from db.
  41. * Ignore $path argument due to config source must return all config data
  42. *
  43. * @param string $path
  44. * @return array
  45. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  46. */
  47. public function get($path = '')
  48. {
  49. if ($this->canUseDatabase()) {
  50. return [
  51. 'websites' => $this->getEntities('store_website', 'code'),
  52. 'groups' => $this->getEntities('store_group', 'group_id'),
  53. 'stores' => $this->getEntities('store', 'code'),
  54. ];
  55. }
  56. return [];
  57. }
  58. /**
  59. * @return AdapterInterface
  60. */
  61. private function getConnection()
  62. {
  63. if (null === $this->connection) {
  64. $this->connection = $this->resourceConnection->getConnection();
  65. }
  66. return $this->connection;
  67. }
  68. /**
  69. * Get entities from specified table in format [entityKeyField => [entity data], ...]
  70. *
  71. * @param string $table
  72. * @param string $keyField
  73. * @return array
  74. */
  75. private function getEntities($table, $keyField)
  76. {
  77. $entities = $this->getConnection()->fetchAll(
  78. $this->getConnection()->select()->from($this->resourceConnection->getTableName($table))
  79. );
  80. $data = [];
  81. foreach ($entities as $entity) {
  82. $data[$entity[$keyField]] = $entity;
  83. }
  84. return $data;
  85. }
  86. /**
  87. * Check whether db connection is available and can be used
  88. *
  89. * @return bool
  90. */
  91. private function canUseDatabase()
  92. {
  93. return $this->deploymentConfig->get('db');
  94. }
  95. }