generated from yiisoft/package-template
/
MergePlan.php
126 lines (114 loc) · 3.3 KB
/
MergePlan.php
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Yiisoft\Config;
use Yiisoft\Config\Composer\Options;
/**
* @internal
*/
final class MergePlan
{
/**
* @psalm-param array<string, array<string, array<string, string[]>>> $mergePlan
*/
public function __construct(
private array $mergePlan = [],
) {
}
/**
* Adds an item to the merge plan.
*
* @param string $file The config file.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function add(
string $file,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package][] = $file;
}
/**
* Adds a multiple items to the merge plan.
*
* @param string[] $files The config files.
* @param string $package The package name.
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addMultiple(
array $files,
string $package,
string $group,
string $environment = Options::DEFAULT_ENVIRONMENT
): void {
$this->mergePlan[$environment][$group][$package] = $files;
}
/**
* Adds an empty environment item to the merge plan.
*
* @param string $environment The environment name.
*/
public function addEnvironmentWithoutConfigs(string $environment): void
{
$this->mergePlan[$environment] = [];
}
/**
* Add empty group if it not exists.
*
* @param string $group The group name.
* @param string $environment The environment name.
*/
public function addGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): void
{
if (!isset($this->mergePlan[$environment][$group])) {
$this->mergePlan[$environment][$group] = [];
}
}
/**
* Returns the merge plan group.
*
* @param string $group The group name.
* @param string $environment The environment name.
*
* @return array<string, string[]>
*/
public function getGroup(string $group, string $environment = Options::DEFAULT_ENVIRONMENT): array
{
return $this->mergePlan[$environment][$group] ?? [];
}
/**
* Returns the merge plan as an array.
*
* @psalm-return array<string, array<string, array<string, string[]>>>
*/
public function toArray(): array
{
return $this->mergePlan;
}
/**
* Checks whether the group exists in the merge plan.
*
* @param string $group The group name.
* @param string $environment The environment name.
*
* @return bool Whether the group exists in the merge plan.
*/
public function hasGroup(string $group, string $environment): bool
{
return isset($this->mergePlan[$environment][$group]);
}
/**
* Checks whether the environment exists in the merge plan.
*
* @param string $environment The environment name.
*
* @return bool Whether the environment exists in the merge plan.
*/
public function hasEnvironment(string $environment): bool
{
return isset($this->mergePlan[$environment]);
}
}