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

DI::bind()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
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
}