1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\SiteAlias; |
3
|
|
|
|
4
|
|
|
use Consolidation\Config\Config; |
5
|
|
|
use Consolidation\Config\ConfigInterface; |
6
|
|
|
use Consolidation\Config\Util\ConfigRuntimeInterface; |
7
|
|
|
use Consolidation\SiteAlias\SiteAlias; |
8
|
|
|
use Consolidation\SiteAlias\SiteAliasInterface; |
9
|
|
|
use Consolidation\SiteAlias\SiteAliasTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* SiteAliasWithConfig delegates to a site alias, and |
13
|
|
|
* also combines it with two config stores: |
14
|
|
|
* |
15
|
|
|
* - Runtime config (set on commandline): Options that override site alias contents |
16
|
|
|
* - Default config (set from config files): Default options |
17
|
|
|
*/ |
18
|
|
|
class SiteAliasWithConfig implements SiteAliasInterface, ConfigRuntimeInterface |
19
|
|
|
{ |
20
|
|
|
use SiteAliasTrait; |
21
|
|
|
|
22
|
|
|
protected $runtimeConfig; |
23
|
|
|
protected $siteAlias; |
24
|
|
|
protected $defaultConfig; |
25
|
|
|
|
26
|
|
|
public function __construct(SiteAliasInterface $siteAlias, ConfigInterface $defaultConfig, ConfigInterface $runtimeConfig) |
27
|
|
|
{ |
28
|
|
|
$this->siteAlias = $siteAlias; |
29
|
|
|
$this->defaultConfig = $defaultConfig; |
30
|
|
|
$this->runtimeConfig = $runtimeConfig; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* combine the provided site alias with configuration. |
35
|
|
|
* |
36
|
|
|
* @return SiteAlias read-only site alias combined with the runtime |
37
|
|
|
* config (overrides the site alias values) and the default config. |
38
|
|
|
*/ |
39
|
|
|
public static function create(SiteAliasInterface $siteAlias, ConfigInterface $defaultConfig, ConfigInterface $runtimeConfig = null) |
40
|
|
|
{ |
41
|
|
|
$runtimeConfig = static::determineCorrectRuntimeConfig($defaultConfig, $runtimeConfig); |
|
|
|
|
42
|
|
|
|
43
|
|
|
return new self($siteAlias, $defaultConfig, $runtimeConfig); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Determine what object to use for the runtime config. If a specific |
48
|
|
|
* runtime config is given, use that. Otherwise, pull it from the default |
49
|
|
|
* configuration if available. |
50
|
|
|
*/ |
51
|
|
|
protected static function determineCorrectRuntimeConfig(ConfigInterface $defaultConfig, ConfigInterface $runtimeConfig) |
52
|
|
|
{ |
53
|
|
|
if ($runtimeConfig) { |
54
|
|
|
return $runtimeConfig; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($defaultConfig instanceof ConfigRuntimeInterface) { |
58
|
|
|
return $defaultConfig->runtimeConfig(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new Config(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritdoc |
66
|
|
|
*/ |
67
|
|
|
public function runtimeConfig() |
68
|
|
|
{ |
69
|
|
|
return $this->runtimeConfig; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function name() |
76
|
|
|
{ |
77
|
|
|
return $this->siteAlias->name(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function has($key) |
84
|
|
|
{ |
85
|
|
|
return |
86
|
|
|
$this->runtimeConfig->has($key) || |
87
|
|
|
$this->siteAlias->has($key) || |
88
|
|
|
$this->defaultConfig->has($key); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
|
|
public function get($key, $defaultFallback = null) |
95
|
|
|
{ |
96
|
|
|
if ($this->runtimeConfig->has($key)) { |
97
|
|
|
return $this->runtimeConfig->get($key); |
98
|
|
|
} |
99
|
|
|
if ($this->siteAlias->has($key)) { |
100
|
|
|
return $this->siteAlias->get($key); |
101
|
|
|
} |
102
|
|
|
if ($this->defaultConfig->has($key)) { |
103
|
|
|
return $this->defaultConfig->get($key); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return $defaultFallback; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @inheritdoc |
111
|
|
|
*/ |
112
|
|
|
public function set($key, $value) |
113
|
|
|
{ |
114
|
|
|
$this->changesProhibited(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @inheritdoc |
119
|
|
|
*/ |
120
|
|
|
public function import($data) |
121
|
|
|
{ |
122
|
|
|
$this->changesProhibited(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
|
|
public function replace($data) |
|
|
|
|
129
|
|
|
{ |
130
|
|
|
$this->changesProhibited(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritdoc |
135
|
|
|
*/ |
136
|
|
|
public function combine($data) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$this->changesProhibited(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Export all configuration as a nested array. |
143
|
|
|
*/ |
144
|
|
|
public function export() |
145
|
|
|
{ |
146
|
|
|
throw new \Exception('SiteAliasWithConfig::export() not supported.'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
|
|
public function hasDefault($key) |
153
|
|
|
{ |
154
|
|
|
return false; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
160
|
|
|
public function getDefault($key, $defaultFallback = null) |
161
|
|
|
{ |
162
|
|
|
return $defaultFallback; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @inheritdoc |
167
|
|
|
*/ |
168
|
|
|
public function setDefault($key, $value) |
169
|
|
|
{ |
170
|
|
|
$this->changesProhibited(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @inheritdoc |
175
|
|
|
*/ |
176
|
|
|
public function exportConfig() |
177
|
|
|
{ |
178
|
|
|
throw new \Exception('SiteAliasWithConfig::exportConfig() not supported.'); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
protected function changesProhibited() |
182
|
|
|
{ |
183
|
|
|
throw new \Exception('Changing a SiteAliasWithConfig is not permitted.'); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: