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

ConfigServiceProvider::getContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use League\Container\ServiceProvider\AbstractServiceProvider;
6
use League\Container\ServiceProvider\BootableServiceProviderInterface;
7
use TomPHP\ConfigServiceProvider\League\AggregateServiceProvider;
8
use League\Container\ContainerInterface;
9
10
final class ConfigServiceProvider extends AbstractServiceProvider implements 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 ApplicationConfig
22
     */
23
    private $config;
24
25
    /**
26
     * @var string
27
     */
28
    private $prefix;
29
30
    /**
31
     * @api
32
     *
33
     * @param array|ApplicationConfig $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
    }
46
47
    /**
48
     * @api
49
     *
50
     * @param string[] $patterns
51
     * @param array    $settings
52
     *
53
     * @return ConfigServiceProvider
54
     */
55
    public static function fromFiles(array $patterns, array $settings = [])
56
    {
57
        $separator = self::getSettingOrDefault(self::SETTING_SEPARATOR, $settings, self::DEFAULT_SEPARATOR);
58
59
        return self::fromConfig(ApplicationConfig::fromFiles($patterns, $separator), $settings);
60
    }
61
62
    /**
63
     * @api
64
     *
65
     * @param array|ApplicationConfig    $config
66
     * @param string                     $prefix
67
     * @param string                     $separator
68
     */
69
    public function __construct(
70
        $config,
71
        $prefix = self::DEFAULT_PREFIX,
72
        $separator = self::DEFAULT_SEPARATOR
73
    ) {
74
        $this->prefix = $prefix;
75
        $this->config = ($config instanceof ApplicationConfig) ? $config : new ApplicationConfig($config, $separator);
76
    }
77
78
    public function boot()
79
    {
80
        $configurator = new League\Configurator();
81
        $configurator->addApplicationConfig($this->container, $this->config, $this->prefix);
82
83
        if (isset($this->config[self::DEFAULT_DI_KEY])) {
84
            $configurator->addServiceConfig(
85
                $this->container,
86
                new ServiceConfig($this->config[self::DEFAULT_DI_KEY])
87
            );
88
        }
89
90
        if (isset($this->config[self::DEFAULT_INFLECTORS_KEY])) {
91
            $configurator->addInflectorConfig(
92
                $this->container,
93
                new InflectorConfig($this->config[self::DEFAULT_INFLECTORS_KEY])
94
            );
95
        }
96
    }
97
98
    public function register()
99
    {
100
    }
101
102
    /**
103
     * @param string $name
104
     * @param array  $settings
105
     * @param mixed  $default
106
     *
107
     * @return mixed
108
     */
109
    private static function getSettingOrDefault($name, array $settings, $default)
110
    {
111
        return isset($settings[$name]) ? $settings[$name] : $default;
112
    }
113
}
114