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

DI   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 7 2
A make() 0 11 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
}