Completed
Pull Request — master (#22)
by Sergey
13:25
created

DI::make()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace Isswp101\Persimmon\DI;
4
5
final class DI
6
{
7
    const SINGLETON = true;
8
9
    private static $containers = [];
10
    private static $singletons = [];
11
12
    public static function bind(string $key, $abstract, bool $singleton = false): void
13
    {
14
        DI::$singletons[$key] = $singleton;
15
        DI::$containers[$key] = is_callable($abstract) ? $abstract : function () use ($abstract) {
16
            return new $abstract;
17
        };
18
    }
19
20
    public static function make(string $key)
21
    {
22
        $container = DI::$containers[$key] ?? null;
23
        if (is_callable($container)) {
24
            $container = $container();
25
            if (DI::$singletons[$key]) {
26
                DI::$containers[$key] = $container;
27
            }
28
        }
29
        return $container;
30
    }
31
}