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

ConfigServiceProvider::configureSubProvider()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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