Completed
Pull Request — master (#28)
by Tom
02:28
created

ConfigServiceProvider::expandSubGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 17
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use League\Container\ServiceProvider\BootableServiceProviderInterface;
7
8
final class ConfigServiceProvider extends AbstractServiceProvider implements
9
    BootableServiceProviderInterface
10
{
11
    const DEFAULT_PREFIX         = 'config';
12
    const DEFAULT_SEPARATOR      = '.';
13
    const DEFAULT_INFLECTORS_KEY = 'inflectors';
14
    const DEFAULT_DI_KEY         = 'di';
15
16
    const SETTING_PREFIX    = 'prefix';
17
    const SETTING_SEPARATOR = 'separator';
18
19
    /**
20
     * @var array
21
     */
22
    private $config;
23
24
    /**
25
     * @var ConfigurableServiceProvider[]
26
     */
27
    private $subProviders;
28
29
    /**
30
     * @api
31
     *
32
     * @param array|Config $config
33
     * @param array        $settings
34
     *
35
     * @return ConfigServiceProvider
36
     */
37
    public static function fromConfig($config, array $settings = [])
38
    {
39
        return new self(
40
            $config,
41
            self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
42
            self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR),
43
            [
44
                self::DEFAULT_INFLECTORS_KEY => new InflectorConfigServiceProvider([]),
45
                self::DEFAULT_DI_KEY         => new DIConfigServiceProvider([]),
46
            ]
47
        );
48
    }
49
50
    /**
51
     * @api
52
     *
53
     * @param string[] $patterns
54
     * @param array    $settings
55
     *
56
     * @return ConfigServiceProvider
57
     */
58
    public static function fromFiles(array $patterns, array $settings = [])
59
    {
60
        $separator = self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX);
61
62
        return self::fromConfig(Config::fromFiles($patterns, $separator), $settings);
63
    }
64
65
    /**
66
     * @api
67
     *
68
     * @param array|Config                  $config
69
     * @param string                        $prefix
70
     * @param string                        $separator
71
     * @param ConfigurableServiceProvider[] $subProviders
72
     */
73
    public function __construct(
74
        $config,
75
        $prefix = self::DEFAULT_PREFIX,
76
        $separator = self::DEFAULT_SEPARATOR,
77
        array $subProviders = []
78
    ) {
79
        $this->config = [];
80
81
        $config = ($config instanceof Config) ? $config : new Config($config, $separator);
82
83
        $iterator = new ConfigIterator($config, $separator);
0 ignored issues
show
Unused Code introduced by
The call to ConfigIterator::__construct() has too many arguments starting with $separator.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
84
        $prefix = $prefix ? $prefix . $separator : '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

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

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
85
86
        foreach ($iterator as $key => $value) {
87
            $this->config[$prefix . $key] = $value;
88
        }
89
90
        $this->subProviders = $subProviders;
91
92
        $this->provides = array_keys($this->config);
93
94
        foreach ($this->subProviders as $key => $provider) {
95
            $this->configureSubProvider($key, $config->asArray(), $provider);
96
        }
97
    }
98
99
    public function register()
100
    {
101
        foreach ($this->config as $key => $value) {
102
            $this->getContainer()->add($key, function () use ($value) {
103
                return $value;
104
            });
105
        }
106
107
        foreach ($this->subProviders as $provider) {
108
            $provider->setContainer($this->getContainer());
109
            $provider->register();
110
        }
111
    }
112
113
    public function boot()
114
    {
115
        foreach ($this->subProviders as $provider) {
116
            if (!$provider instanceof BootableServiceProviderInterface) {
117
                continue;
118
            }
119
120
            $provider->setContainer($this->getContainer());
121
            $provider->boot();
122
        }
123
    }
124
125
    /**
126
     * @param string $key
127
     * @param array $config
128
     * @param ConfigurableServiceProvider $provider
129
     */
130
    private function configureSubProvider($key, array $config, ConfigurableServiceProvider $provider)
131
    {
132
        if (!array_key_exists($key, $config)) {
133
            return;
134
        }
135
136
        $provider->configure($config[$key]);
137
138
        $this->provides = array_merge($this->provides, $provider->provides());
139
    }
140
141
    /**
142
     * @param string $name
143
     * @param array  $settings
144
     * @param mixed  $default
145
     *
146
     * @return mixed
147
     */
148
    private static function getSettingOrDefault($name, array $settings, $default)
149
    {
150
        return isset($settings[$name]) ? $settings[$name] : $default;
151
    }
152
}
153