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

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

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
182
            $configurator->addServiceConfig(new ServiceConfig(
183
                $this->config[$this->settings[self::SETTING_SERVICES_KEY]],
184
                $this->settings[self::SETTING_DEFAULT_SINGLETON_SERVICES]
185
            ));
186
        }
187
188 View Code Duplication
        if (isset($this->config[$this->settings[self::SETTING_INFLECTORS_KEY]])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
            $configurator->addInflectorConfig(new InflectorConfig(
190
                $this->config[$this->settings[self::SETTING_INFLECTORS_KEY]]
191
            ));
192
        }
193
    }
194
195
    /**
196
     * @param string $filename
197
     *
198
     * @return void
199
     */
200
    private function readFileAndMergeConfig($filename)
201
    {
202
        $reader = $this->getReaderFor($filename);
203
204
        $this->config->merge($reader->read($filename));
205
    }
206
207
    /**
208
     * @param string $filename
209
     *
210
     * @return FileReader\FileReader
211
     */
212
    private function getReaderFor($filename)
213
    {
214
        if (!$this->readerFactory) {
215
            $this->readerFactory = new FileReader\ReaderFactory(self::FILE_READERS);
216
        }
217
218
        return $this->readerFactory->create($filename);
219
    }
220
}
221