1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\ConfigServiceProvider; |
4
|
|
|
|
5
|
|
|
final class ConfigureContainer |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @api |
9
|
|
|
* |
10
|
|
|
* @param object $container |
11
|
|
|
* @param array $patterns |
12
|
|
|
* @param array $settings |
13
|
|
|
* |
14
|
|
|
* @return void |
15
|
|
|
*/ |
16
|
|
|
public static function fromFiles($container, array $patterns, $settings = []) |
17
|
|
|
{ |
18
|
|
|
$settings = self::prepareSettings($settings); |
|
|
|
|
19
|
|
|
$appConfig = ApplicationConfig::fromFiles($patterns, $settings['config_separator']); |
20
|
|
|
self::configureContainer($container, $appConfig, $settings); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @api |
25
|
|
|
* |
26
|
|
|
* @param object $container |
27
|
|
|
* @param array $config |
28
|
|
|
* @param array $settings |
29
|
|
|
* |
30
|
|
|
* @return void |
31
|
|
|
*/ |
32
|
|
|
public static function fromArray($container, array $config, $settings = []) |
33
|
|
|
{ |
34
|
|
|
$settings = self::prepareSettings($settings); |
|
|
|
|
35
|
|
|
$appConfig = new ApplicationConfig($config, $settings['config_separator']); |
36
|
|
|
self::configureContainer($container, $appConfig, $settings); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $settings |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
private static function prepareSettings(array $settings) |
45
|
|
|
{ |
46
|
|
|
return array_merge( |
47
|
|
|
[ |
48
|
|
|
'config_prefix' => 'config', |
49
|
|
|
'config_separator' => '.', |
50
|
|
|
'services_key' => 'di.services', |
51
|
|
|
'inflectors_key' => 'di.inflectors', |
52
|
|
|
'singleton_services' => false, |
53
|
|
|
], |
54
|
|
|
$settings |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private static function configureContainer($container, ApplicationConfig $appConfig, array $settings) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$factory = new ConfiguratorFactory([ |
61
|
|
|
'League\Container\Container' => 'TomPHP\ConfigServiceProvider\League\Configurator', |
62
|
|
|
'Pimple\Container' => 'TomPHP\ConfigServiceProvider\Pimple\Configurator', |
63
|
|
|
]); |
64
|
|
|
|
65
|
|
|
$configurator = $factory->create($container); |
66
|
|
|
|
67
|
|
|
$configurator->addApplicationConfig($appConfig, $settings['config_prefix']); |
68
|
|
|
|
69
|
|
|
if (isset($appConfig[$settings['services_key']])) { |
70
|
|
|
$configurator->addServiceConfig( |
71
|
|
|
new ServiceConfig($appConfig[$settings['services_key']], $settings['singleton_services']) |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($appConfig[$settings['inflectors_key']])) { |
76
|
|
|
$configurator->addInflectorConfig(new InflectorConfig($appConfig[$settings['inflectors_key']])); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.