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