1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\ConfigServiceProvider; |
4
|
|
|
|
5
|
|
|
use TomPHP\ConfigServiceProvider\FileReader\FileLocator; |
6
|
|
|
use TomPHP\ConfigServiceProvider\FileReader\ReaderFactory; |
7
|
|
|
use TomPHP\ConfigServiceProvider\Exception\NoMatchingFilesException; |
8
|
|
|
use TomPHP\ConfigServiceProvider\Exception\UnknownSettingException; |
9
|
|
|
use Assert\Assertion; |
10
|
|
|
|
11
|
|
|
final class Configurator |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ApplicationConfig |
15
|
|
|
*/ |
16
|
|
|
private $config; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $settings = [ |
22
|
|
|
'config_prefix' => 'config', |
23
|
|
|
'config_separator' => '.', |
24
|
|
|
'services_key' => 'di.services', |
25
|
|
|
'inflectors_key' => 'di.inflectors', |
26
|
|
|
'singleton_services' => false, |
27
|
|
|
]; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @api |
31
|
|
|
* |
32
|
|
|
* @return Configurator |
33
|
|
|
*/ |
34
|
|
|
public static function apply() |
35
|
|
|
{ |
36
|
|
|
return new self(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function __construct() |
40
|
|
|
{ |
41
|
|
|
$this->config = new ApplicationConfig([]); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @api |
46
|
|
|
* |
47
|
|
|
* @param array $config |
48
|
|
|
* |
49
|
|
|
* @return Configurator |
50
|
|
|
*/ |
51
|
|
|
public function configFromArray(array $config) |
52
|
|
|
{ |
53
|
|
|
$this->config->merge($config); |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @api |
60
|
|
|
* |
61
|
|
|
* @param string $pattern |
62
|
|
|
* |
63
|
|
|
* @return Configurator |
64
|
|
|
*/ |
65
|
|
|
public function configFromFiles($pattern) |
66
|
|
|
{ |
67
|
|
|
Assertion::string($pattern); |
68
|
|
|
|
69
|
|
|
$locator = new FileLocator(); |
70
|
|
|
|
71
|
|
|
$factory = new ReaderFactory([ |
72
|
|
|
'.json' => 'TomPHP\ConfigServiceProvider\FileReader\JSONFileReader', |
73
|
|
|
'.php' => 'TomPHP\ConfigServiceProvider\FileReader\PHPFileReader', |
74
|
|
|
]); |
75
|
|
|
|
76
|
|
|
$files = $locator->locate($pattern); |
77
|
|
|
|
78
|
|
|
if (count($files) === 0) { |
79
|
|
|
throw NoMatchingFilesException::fromPattern($pattern); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
foreach ($files as $filename) { |
83
|
|
|
$reader = $factory->create($filename); |
84
|
|
|
$this->config->merge($reader->read($filename)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @api |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @param mixed $value |
95
|
|
|
* |
96
|
|
|
* @return Configurator |
97
|
|
|
*/ |
98
|
|
|
public function withSetting($name, $value) |
99
|
|
|
{ |
100
|
|
|
Assertion::string($name); |
101
|
|
|
Assertion::scalar($value); |
102
|
|
|
|
103
|
|
|
if (!array_key_exists($name, $this->settings)) { |
104
|
|
|
throw UnknownSettingException::fromSetting($name, array_keys($this->settings)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->settings[$name] = $value; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @api |
114
|
|
|
* |
115
|
|
|
* @param object $container |
116
|
|
|
* |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
|
|
public function to($container) |
120
|
|
|
{ |
121
|
|
|
$this->config->setSeparator($this->settings['config_separator']); |
122
|
|
|
|
123
|
|
|
$factory = new ContainerAdapterFactory([ |
124
|
|
|
'League\Container\Container' => 'TomPHP\ConfigServiceProvider\League\LeagueContainerAdapter', |
125
|
|
|
'Pimple\Container' => 'TomPHP\ConfigServiceProvider\Pimple\PimpleContainerAdapter', |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
$configurator = $factory->create($container); |
129
|
|
|
|
130
|
|
|
$configurator->addApplicationConfig($this->config, $this->settings['config_prefix']); |
131
|
|
|
|
132
|
|
|
if (isset($this->config[$this->settings['services_key']])) { |
133
|
|
|
$configurator->addServiceConfig(new ServiceConfig( |
134
|
|
|
$this->config[$this->settings['services_key']], |
135
|
|
|
$this->settings['singleton_services'] |
136
|
|
|
)); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if (isset($this->config[$this->settings['inflectors_key']])) { |
140
|
|
|
$configurator->addInflectorConfig(new InflectorConfig($this->config[$this->settings['inflectors_key']])); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|