Completed
Push — master ( fe91ee...d74635 )
by Andreas
02:39
created

Configurator::configFromFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider;
4
5
use TomPHP\ConfigServiceProvider\Exception\NoMatchingFilesException;
6
use TomPHP\ConfigServiceProvider\Exception\UnknownSettingException;
7
use Assert\Assertion;
8
9
final class Configurator
10
{
11
    const FILE_READERS = [
12
        '.json' => FileReader\JSONFileReader::class,
13
        '.php'  => FileReader\PHPFileReader::class,
14
    ];
15
16
    const CONTAINER_ADAPTERS = [
17
        \League\Container\Container::class => League\LeagueContainerAdapter::class,
18
        \Pimple\Container::class           => Pimple\PimpleContainerAdapter::class,
19
    ];
20
21
    /**
22
     * @var ApplicationConfig
23
     */
24
    private $config;
25
26
    /**
27
     * @var FileReader\ReaderFactory
28
     */
29
    private $readerFactory;
30
31
    /**
32
     * @var array
33
     */
34
    private $settings = [
35
        'config_prefix'      => 'config',
36
        'config_separator'   => '.',
37
        'services_key'       => 'di.services',
38
        'inflectors_key'     => 'di.inflectors',
39
        'singleton_services' => false,
40
    ];
41
42
    /**
43
     * @api
44
     *
45
     * @return Configurator
46
     */
47
    public static function apply()
48
    {
49
        return new self();
50
    }
51
52
    private function __construct()
53
    {
54
        $this->config = new ApplicationConfig([]);
55
    }
56
57
    /**
58
     * @api
59
     *
60
     * @param array $config
61
     *
62
     * @return Configurator
63
     */
64
    public function configFromArray(array $config)
65
    {
66
        $this->config->merge($config);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @api
73
     *
74
     * @param string $filename
75
     *
76
     * @return Configurator
77
     */
78
    public function configFromFile($filename)
79
    {
80
        Assertion::file($filename);
81
82
        $this->readFileAndMergeConfig($filename);
83
84
        return $this;
85
    }
86
87
    /**
88
     * @api
89
     *
90
     * @param string $pattern
91
     *
92
     * @return Configurator
93
     *
94
     * @throws NoMatchingFilesException
95
     */
96
    public function configFromFiles($pattern)
97
    {
98
        Assertion::string($pattern);
99
100
        $locator = new FileReader\FileLocator();
101
102
        $files = $locator->locate($pattern);
103
104
        if (count($files) === 0) {
105
            throw NoMatchingFilesException::fromPattern($pattern);
106
        }
107
108
        foreach ($files as $filename) {
109
            $this->readFileAndMergeConfig($filename);
110
        }
111
112
        return $this;
113
    }
114
115
    /**
116
     * @api
117
     *
118
     * @param string $name
119
     * @param mixed  $value
120
     *
121
     * @return Configurator
122
     *
123
     * @throws UnknownSettingException
124
     */
125
    public function withSetting($name, $value)
126
    {
127
        Assertion::string($name);
128
        Assertion::scalar($value);
129
130
        if (!array_key_exists($name, $this->settings)) {
131
            throw UnknownSettingException::fromSetting($name, array_keys($this->settings));
132
        }
133
134
        $this->settings[$name] = $value;
135
136
        return $this;
137
    }
138
139
    /**
140
     * @api
141
     *
142
     * @param object $container
143
     *
144
     * @return void
145
     */
146
    public function to($container)
147
    {
148
        $this->config->setSeparator($this->settings['config_separator']);
149
150
        $factory = new ContainerAdapterFactory(self::CONTAINER_ADAPTERS);
151
152
        $configurator = $factory->create($container);
153
154
        $configurator->addApplicationConfig($this->config, $this->settings['config_prefix']);
155
156
        if (isset($this->config[$this->settings['services_key']])) {
157
            $configurator->addServiceConfig(new ServiceConfig(
158
                $this->config[$this->settings['services_key']],
159
                $this->settings['singleton_services']
160
            ));
161
        }
162
163
        if (isset($this->config[$this->settings['inflectors_key']])) {
164
            $configurator->addInflectorConfig(new InflectorConfig($this->config[$this->settings['inflectors_key']]));
165
        }
166
    }
167
168
    /**
169
     * @param string $filename
170
     *
171
     * @return void
172
     */
173
    private function readFileAndMergeConfig($filename)
174
    {
175
        $reader = $this->getReaderFor($filename);
176
177
        $this->config->merge($reader->read($filename));
178
    }
179
180
    /**
181
     * @param string $filename
182
     *
183
     * @return FileReader\FileReader
184
     */
185
    private function getReaderFor($filename)
186
    {
187
        if (!$this->readerFactory) {
188
            $this->readerFactory = new FileReader\ReaderFactory(self::FILE_READERS);
189
        }
190
191
        return $this->readerFactory->create($filename);
192
    }
193
}
194