Completed
Pull Request — master (#28)
by Tom
02:29
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
use League\Container\ServiceProvider\ServiceProviderInterface;
8
9
final class ConfigServiceProvider extends AbstractServiceProvider implements
10
    BootableServiceProviderInterface
11
{
12
    const DEFAULT_PREFIX         = 'config';
13
    const DEFAULT_SEPARATOR      = '.';
14
    const DEFAULT_INFLECTORS_KEY = 'inflectors';
15
    const DEFAULT_DI_KEY         = 'di';
16
17
    const SETTING_PREFIX    = 'prefix';
18
    const SETTING_SEPARATOR = 'separator';
19
20
    /**
21
     * @var array
22
     */
23
    private $config;
24
25
    /**
26
     * @var ConfigurableServiceProvider[]
27
     */
28
    private $subProviders;
29
30
    /**
31
     * @api
32
     *
33
     * @param array|Config $config
34
     * @param array        $settings
35
     *
36
     * @return ConfigServiceProvider
37
     */
38
    public static function fromConfig($config, array $settings = [])
39
    {
40
        return new self(
41
            $config,
42
            self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
43
            self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR),
44
            [
45
                self::DEFAULT_INFLECTORS_KEY => new InflectorConfigServiceProvider([]),
46
                self::DEFAULT_DI_KEY         => new DIConfigServiceProvider([]),
47
            ]
48
        );
49
    }
50
51
    /**
52
     * @api
53
     *
54
     * @param string[] $patterns
55
     * @param array    $settings
56
     *
57
     * @return ConfigServiceProvider
58
     */
59
    public static function fromFiles(array $patterns, array $settings = [])
60
    {
61
        $separator = self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR);
62
63
        return self::fromConfig(Config::fromFiles($patterns, $separator), $settings);
64
    }
65
66
    /**
67
     * @api
68
     *
69
     * @param array|Config                  $config
70
     * @param string                        $prefix
71
     * @param string                        $separator
72
     * @param ConfigurableServiceProvider[] $subProviders
73
     */
74
    public function __construct(
75
        $config,
76
        $prefix = self::DEFAULT_PREFIX,
77
        $separator = self::DEFAULT_SEPARATOR,
78
        array $subProviders = []
79
    ) {
80
        $this->config = [];
81
82
        $config = ($config instanceof Config) ? $config : new Config($config, $separator);
83
84
        $configurator = new League\Configurator();
85
        $configurator->addConfig($config, $prefix);
86
87
        $this->subProviders = [__FILE__ => $configurator->getServiceProvider()] + $subProviders;
0 ignored issues
show
Documentation Bug introduced by
It seems like array(__FILE__ => $confi...ider()) + $subProviders of type array<integer|string,obj...urableServiceProvider>> is incompatible with the declared type array<integer,object<Tom...urableServiceProvider>> of property $subProviders.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

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