Sharding.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup\Declaration\Schema;
  7. use Magento\Framework\App\DeploymentConfig;
  8. use Magento\Framework\Config\ConfigOptionsListConstants;
  9. /**
  10. * Sharding provider.
  11. *
  12. * Sharding distributes structural elements among various shards (connections) described in deployment configuration.
  13. */
  14. class Sharding
  15. {
  16. /**
  17. * Name of default connection.
  18. */
  19. const DEFAULT_CONNECTION = 'default';
  20. /**
  21. * @var DeploymentConfig
  22. */
  23. private $deploymentConfig;
  24. /**
  25. * Connection names.
  26. *
  27. * Each connection name represents each shard.
  28. *
  29. * @var array
  30. */
  31. private $resources;
  32. /**
  33. * Constructor.
  34. *
  35. * @param DeploymentConfig $deploymentConfig
  36. * @param array $resources
  37. */
  38. public function __construct(DeploymentConfig $deploymentConfig, array $resources)
  39. {
  40. $this->deploymentConfig = $deploymentConfig;
  41. $this->resources = $resources;
  42. }
  43. /**
  44. * Depends on different settings we should have different qty of connection names.
  45. *
  46. * @return array
  47. */
  48. public function getResources()
  49. {
  50. $resources = [];
  51. foreach ($this->resources as $resource) {
  52. if ($this->canUseResource($resource)) {
  53. $resources[] = $resource;
  54. }
  55. }
  56. return $resources;
  57. }
  58. /**
  59. * Check whether our resource is valid one.
  60. *
  61. * @param string $scopeName
  62. * @return bool
  63. */
  64. public function canUseResource($scopeName)
  65. {
  66. $connections = $this->deploymentConfig
  67. ->get(ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTIONS);
  68. return isset($connections[$scopeName]);
  69. }
  70. /**
  71. * Retrieve default resource name, that is used by the system.
  72. *
  73. * @return string
  74. */
  75. public function getDefaultResource()
  76. {
  77. return self::DEFAULT_CONNECTION;
  78. }
  79. }