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

Configurator::addApplicationConfig()   A

Complexity

Conditions 3
Paths 4

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 3
eloc 5
nc 4
nop 3
1
<?php
2
3
namespace TomPHP\ConfigServiceProvider\Pimple;
4
5
use Pimple\Container;
6
use ReflectionClass;
7
use TomPHP\ConfigServiceProvider\ApplicationConfig;
8
use TomPHP\ConfigServiceProvider\ContainerConfigurator;
9
use TomPHP\ConfigServiceProvider\InflectorConfig;
10
use TomPHP\ConfigServiceProvider\ServiceConfig;
11
use TomPHP\ConfigServiceProvider\Exception\UnsupportedFeatureException;
12
13
final class Configurator implements ContainerConfigurator
14
{
15
    public function addApplicationConfig($container, ApplicationConfig $config, $prefix = 'config')
16
    {
17
        if (!empty($prefix)) {
18
            $prefix .= $config->getSeparator();
19
        }
20
21
        foreach ($config as $key => $value) {
22
            $container[$prefix . $key] = $value;
23
        }
24
    }
25
26
    public function addServiceConfig($container, ServiceConfig $config)
27
    {
28
        foreach ($config as $definition) {
29
            $factory = function () use ($definition) {
30
                $reflection = new ReflectionClass($definition->getClass());
31
32
                $instance = $reflection->newInstanceArgs($definition->getArguments());
33
34
                foreach ($definition->getMethods() as $name => $args) {
35
                    call_user_func_array([$instance, $name], $args);
36
                }
37
38
                return $instance;
39
            };
40
41
            if (!$definition->isSingleton()) {
42
                $factory = $container->factory($factory);
43
            }
44
45
            $container[$definition->getName()] = $factory;
46
        }
47
    }
48
49
    public function addInflectorConfig($container, InflectorConfig $config)
50
    {
51
        throw UnsupportedFeatureException::forInflectors('Pimple');
52
    }
53
}
54