Container::withContainerConfiguration()   A
last analyzed

Complexity

Conditions 6
Paths 24

Size

Total Lines 34
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
nc 24
nop 2
dl 0
loc 34
rs 9.2222
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Framework\Container;
6
7
use Gacela\Container\Container as GacelaContainer;
8
use Gacela\Framework\Bootstrap\ContainerConfigurationInterface;
9
use Gacela\Framework\Config\Config;
10
11
/**
12
 * This is a decorator class to simplify the usage of the decoupled Container
13
 */
14
final class Container extends GacelaContainer implements ContainerInterface
15
{
16
    public static function withConfig(Config $config): self
17
    {
18
        return self::withContainerConfiguration(
19
            $config->getSetupGacela(),
20
            $config->getFactory()->createGacelaFileConfig()->getBindings(),
21
        );
22
    }
23
24
    /**
25
     * @param array<class-string,class-string|object|callable> $bindings
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<class-string,class-string|object|callable> at position 2 could not be parsed: Unknown type name 'class-string' at position 2 in array<class-string,class-string|object|callable>.
Loading history...
26
     */
27
    public static function withContainerConfiguration(
28
        ContainerConfigurationInterface $containerConfig,
29
        array $bindings,
30
    ): self {
31
        $container = new self(
32
            $bindings,
33
            $containerConfig->getServicesToExtend(),
34
        );
35
36
        // Register factory services
37
        foreach ($containerConfig->getFactories() as $id => $factory) {
38
            $container->set($id, $container->factory($factory));
39
        }
40
41
        // Register protected services
42
        foreach ($containerConfig->getProtectedServices() as $id => $service) {
43
            $container->set($id, $container->protect($service));
44
        }
45
46
        // Register aliases
47
        foreach ($containerConfig->getAliases() as $alias => $id) {
48
            $container->alias($alias, $id);
49
        }
50
51
        // Register contextual bindings
52
        foreach ($containerConfig->getContextualBindings() as $concrete => $needs) {
53
            foreach ($needs as $abstract => $implementation) {
54
                /** @var class-string $concrete */
55
                /** @var class-string $abstract */
56
                $container->when($concrete)->needs($abstract)->give($implementation);
57
            }
58
        }
59
60
        return $container;
61
    }
62
63
    public function getLocator(): LocatorInterface
64
    {
65
        return Locator::getInstance($this);
66
    }
67
}
68