1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Service; |
13
|
|
|
|
14
|
|
|
use Translation\Bundle\Model\Configuration; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A service to easily access different configurations. |
18
|
|
|
* |
19
|
|
|
* @author Tobias Nyholm <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
final class ConfigurationManager |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Configuration[] |
25
|
|
|
*/ |
26
|
|
|
private $configuration = []; |
27
|
|
|
|
28
|
5 |
|
public function addConfiguration(string $name, Configuration $configuration): void |
29
|
|
|
{ |
30
|
5 |
|
$this->configuration[$name] = $configuration; |
31
|
5 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string|string[]|null $name |
35
|
|
|
*/ |
36
|
3 |
|
public function getConfiguration($name = null): Configuration |
37
|
|
|
{ |
38
|
3 |
|
if (empty($name)) { |
39
|
2 |
|
return $this->getConfiguration('default'); |
40
|
|
|
} |
41
|
|
|
|
42
|
3 |
|
if (isset($this->configuration[$name])) { |
43
|
1 |
|
return $this->configuration[$name]; |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
if ('default' === $name) { |
47
|
1 |
|
$name = $this->getFirstName(); |
48
|
1 |
|
if (isset($this->configuration[$name])) { |
49
|
1 |
|
return $this->configuration[$name]; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
throw new \InvalidArgumentException(\sprintf('No configuration found for "%s"', $name)); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
public function getFirstName(): ?string |
57
|
|
|
{ |
58
|
3 |
|
foreach ($this->configuration as $name => $config) { |
59
|
2 |
|
return $name; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function getNames(): array |
66
|
|
|
{ |
67
|
2 |
|
return \array_keys($this->configuration); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|