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

ConfigServiceProvider::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 17
Bugs 0 Features 8
Metric Value
c 17
b 0
f 8
dl 0
loc 12
rs 9.4285
cc 2
eloc 8
nc 2
nop 3
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 BootableServiceProviderInterface
9
{
10
    const DEFAULT_PREFIX         = 'config';
11
    const DEFAULT_SEPARATOR      = '.';
12
    const DEFAULT_INFLECTORS_KEY = 'inflectors';
13
    const DEFAULT_DI_KEY         = 'di';
14
15
    const SETTING_PREFIX    = 'prefix';
16
    const SETTING_SEPARATOR = 'separator';
17
18
    /**
19
     * @var ApplicationConfig
20
     */
21
    private $config;
22
23
    /**
24
     * @var string
25
     */
26
    private $prefix;
27
28
    /**
29
     * @api
30
     *
31
     * @param array|ApplicationConfig $config
32
     * @param array                   $settings
33
     *
34
     * @return ConfigServiceProvider
35
     */
36
    public static function fromConfig($config, array $settings = [])
37
    {
38
        return new self(
39
            $config,
40
            self::getSettingOrDefault(self::SETTING_PREFIX, $settings, self::DEFAULT_PREFIX),
41
            self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR)
42
        );
43
    }
44
45
    /**
46
     * @api
47
     *
48
     * @param string[] $patterns
49
     * @param array    $settings
50
     *
51
     * @return ConfigServiceProvider
52
     */
53
    public static function fromFiles(array $patterns, array $settings = [])
54
    {
55
        $separator = self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR);
56
57
        return self::fromConfig(ApplicationConfig::fromFiles($patterns, $separator), $settings);
58
    }
59
60
    /**
61
     * @api
62
     *
63
     * @param array|ApplicationConfig $config
64
     * @param string                  $prefix
65
     * @param string                  $separator
66
     */
67
    public function __construct(
68
        $config,
69
        $prefix = self::DEFAULT_PREFIX,
70
        $separator = self::DEFAULT_SEPARATOR
71
    ) {
72
        if (!$config instanceof ApplicationConfig) {
73
            $config = new ApplicationConfig($config, $separator);
74
        }
75
76
        $this->prefix = $prefix;
77
        $this->config = $config;
78
    }
79
80
    public function boot()
81
    {
82
        $configurator = new League\Configurator();
83
        $configurator->addApplicationConfig($this->container, $this->config, $this->prefix);
84
85
        if (isset($this->config[self::DEFAULT_DI_KEY])) {
86
            $configurator->addServiceConfig(
87
                $this->container,
88
                new ServiceConfig($this->config[self::DEFAULT_DI_KEY])
89
            );
90
        }
91
92
        if (isset($this->config[self::DEFAULT_INFLECTORS_KEY])) {
93
            $configurator->addInflectorConfig(
94
                $this->container,
95
                new InflectorConfig($this->config[self::DEFAULT_INFLECTORS_KEY])
96
            );
97
        }
98
    }
99
100
    public function register()
101
    {
102
    }
103
104
    /**
105
     * @param string $name
106
     * @param array  $settings
107
     * @param mixed  $default
108
     *
109
     * @return mixed
110
     */
111
    private static function getSettingOrDefault($name, array $settings, $default)
112
    {
113
        return isset($settings[$name]) ? $settings[$name] : $default;
114
    }
115
}
116