-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathConfigServiceProvider.php
More file actions
136 lines (109 loc) · 3.6 KB
/
ConfigServiceProvider.php
File metadata and controls
136 lines (109 loc) · 3.6 KB
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
127
128
129
130
131
132
133
134
135
136
<?php
/*
* This file is part of ConfigServiceProvider.
*
* (c) Igor Wiedler <igor@wiedler.ch>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Igorw\Silex;
use Silex\Application;
use Silex\ServiceProviderInterface;
class ConfigServiceProvider implements ServiceProviderInterface
{
private $filename;
private $replacements = array();
private $driver;
private $prefix = null;
public function __construct($filename, array $replacements = array(), ConfigDriver $driver = null, $prefix = null)
{
$this->filename = $filename;
$this->prefix = $prefix;
if ($replacements) {
foreach ($replacements as $key => $value) {
$this->replacements['%'.$key.'%'] = $value;
}
}
$this->driver = $driver ?: new ChainConfigDriver(array(
new PhpConfigDriver(),
new YamlConfigDriver(),
new JsonConfigDriver(),
new TomlConfigDriver(),
));
}
public function register(Application $app)
{
$config = $this->readConfig();
foreach ($config as $name => $value)
if ('%' === substr($name, 0, 1))
$this->replacements[$name] = (string) $value;
$this->merge($app, $config);
}
public function load()
{
$config = $this->readConfig();
foreach ($config as $name => $value)
if ('%' === substr($name, 0, 1))
$this->replacements[$name] = (string) $value;
return $config;
}
public function boot(Application $app)
{
}
private function merge(Application $app, array $config)
{
if ($this->prefix) {
$config = array($this->prefix => $config);
}
foreach ($config as $name => $value) {
if (isset($app[$name]) && is_array($value)) {
$app[$name] = $this->mergeRecursively($app[$name], $value);
} else {
$app[$name] = $this->doReplacements($value);
}
}
}
private function mergeRecursively(array $currentValue, array $newValue)
{
foreach ($newValue as $name => $value) {
if (is_array($value) && isset($currentValue[$name])) {
$currentValue[$name] = $this->mergeRecursively($currentValue[$name], $value);
} else {
$currentValue[$name] = $this->doReplacements($value);
}
}
return $currentValue;
}
private function doReplacements($value)
{
if (!$this->replacements) {
return $value;
}
if (is_array($value)) {
foreach ($value as $k => $v) {
$value[$k] = $this->doReplacements($v);
}
return $value;
}
if (is_string($value)) {
return strtr($value, $this->replacements);
}
return $value;
}
private function readConfig()
{
if (!$this->filename) {
throw new \RuntimeException('A valid configuration file must be passed before reading the config.');
}
if (!file_exists($this->filename)) {
throw new \InvalidArgumentException(
sprintf("The config file '%s' does not exist.", $this->filename));
}
if ($this->driver->supports($this->filename)) {
return $this->driver->load($this->filename);
}
throw new \InvalidArgumentException(
sprintf("The config file '%s' appears to have an invalid format.", $this->filename));
}
}