Completed
Pull Request — master (#53)
by Tom
02:23
created

Configurator::getReaderFor()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 4
nc 2
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
    public function configFromFiles($pattern)
95
    {
96
        Assertion::string($pattern);
97
98
        $locator = new FileReader\FileLocator();
99
100
        $files = $locator->locate($pattern);
101
102
        if (count($files) === 0) {
103
            throw NoMatchingFilesException::fromPattern($pattern);
104
        }
105
106
        foreach ($files as $filename) {
107
            $this->readFileAndMergeConfig($filename);
108
        }
109
110
        return $this;
111
    }
112
113
    /**
114
     * @api
115
     *
116
     * @param string $name
117
     * @param mixed  $value
118
     *
119
     * @return Configurator
120
     */
121
    public function withSetting($name, $value)
122
    {
123
        Assertion::string($name);
124
        Assertion::scalar($value);
125
126
        if (!array_key_exists($name, $this->settings)) {
127
            throw UnknownSettingException::fromSetting($name, array_keys($this->settings));
128
        }
129
130
        $this->settings[$name] = $value;
131
132
        return $this;
133
    }
134
135
    /**
136
     * @api
137
     *
138
     * @param object $container
139
     *
140
     * @return void
141
     */
142
    public function to($container)
143
    {
144
        $this->config->setSeparator($this->settings['config_separator']);
145
146
        $factory = new ContainerAdapterFactory(self::CONTAINER_ADAPTERS);
147
148
        $configurator = $factory->create($container);
149
150
        $configurator->addApplicationConfig($this->config, $this->settings['config_prefix']);
151
152
        if (isset($this->config[$this->settings['services_key']])) {
153
            $configurator->addServiceConfig(new ServiceConfig(
154
                $this->config[$this->settings['services_key']],
155
                $this->settings['singleton_services']
156
            ));
157
        }
158
159
        if (isset($this->config[$this->settings['inflectors_key']])) {
160
            $configurator->addInflectorConfig(new InflectorConfig($this->config[$this->settings['inflectors_key']]));
161
        }
162
    }
163
164
    /**
165
     * @param string $filename
166
     *
167
     * @return void
168
     */
169
    private function readFileAndMergeConfig($filename)
170
    {
171
        $reader = $this->getReaderFor($filename);
172
173
        $this->config->merge($reader->read($filename));
174
    }
175
176
    /**
177
     * @param string $filename
178
     *
179
     * @return FileReader\FileReader
180
     */
181
    private function getReaderFor($filename)
182
    {
183
        if (!$this->readerFactory) {
184
            $this->readerFactory = new FileReader\ReaderFactory(self::FILE_READERS);
185
        }
186
187
        return $this->readerFactory->create($filename);
188
    }
189
}
190