ConfigServiceProvider::register()   B
last analyzed

Complexity

Conditions 6
Paths 1

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 14
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of vaibhavpandeyvpz/pimple-config package.
5
 *
6
 * (c) Vaibhav Pandey <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 */
11
12
namespace Pimple\Config;
13
14
use InvalidArgumentException;
15
use RuntimeException;
16
use Pimple\Container;
17
use Pimple\ServiceProviderInterface;
18
19
/**
20
 * Class ConfigServiceProvider
21
 * @package Pimple\Config
22
 */
23
class ConfigServiceProvider implements ServiceProviderInterface
24
{
25
    /**
26
     * @inheritdoc
27
     */
28
    public function register(Container $pimple)
29
    {
30
        $pimple['config.files'] = function ($pimple) {
31
            return [$pimple['config.file']];
32
        };
33
        $pimple['config.hydrate'] = $pimple->protect(function ($pimple) {
34
            foreach ($pimple['config.files'] as $file) {
35
                if (is_file($file) && is_readable($file)) {
36
                    /** @noinspection PhpIncludeInspection */
37
                    $values = require $file;
38
                    if (is_array($values)) {
39
                        foreach ($values as $key => $value) {
40
                            $pimple[$key] = $value;
41
                        }
42
                    } else {
43
                        throw new InvalidArgumentException("Configuration file must return an array");
44
                    }
45
                } else {
46
                    throw new RuntimeException("Could not find or read from {$file}");
47
                }
48
            }
49
        });
50
    }
51
}
52