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

Configurator::addInflectorConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 2
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
12
final class Configurator implements ContainerConfigurator
13
{
14
    public function addApplicationConfig($container, ApplicationConfig $config, $prefix = 'config')
15
    {
16
        if (!empty($prefix)) {
17
            $prefix .= $config->getSeparator();
18
        }
19
20
        foreach ($config as $key => $value) {
21
            $container[$prefix . $key] = $value;
22
        }
23
    }
24
25
    public function addServiceConfig($container, ServiceConfig $config)
26
    {
27
        foreach ($config as $definition) {
28
            $factory = function () use ($definition) {
29
                $reflection = new ReflectionClass($definition->getClass());
30
31
                $instance = $reflection->newInstanceArgs($definition->getArguments());
32
33
                foreach ($definition->getMethods() as $name => $args) {
34
                    call_user_func_array([$instance, $name], $args);
35
                }
36
37
                return $instance;
38
            };
39
40
            if (!$definition->isSingleton()) {
41
                $factory = $container->factory($factory);
42
            }
43
44
            $container[$definition->getName()] = $factory;
45
        }
46
    }
47
48
    public function addInflectorConfig($container, InflectorConfig $config)
49
    {
50
    }
51
}
52