Passed
Push — master ( b86482...6c4e2a )
by Vee
01:42
created

Container::has()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace veejay\container;
4
5
use Psr\Container\ContainerInterface;
6
7
/**
8
 * DI контейнер.
9
 *
10
 * 1. Вариант использования по названию класса.
11
 * $c = new Container;
12
 * $c->set(Component::class, fn() => new Component);
13
 * $c->get(Component::class);
14
 *
15
 * 2. Вариант использования с произвольным названием компонента, если один класс используется в двух разных компонентах.
16
 * $c = new Container;
17
 * $c->set('one', fn() => new Component);
18
 * $c->set('two', function(Container $di) {
19
 *     return new Component;
20
 * });
21
 * $c->get('one'); // Идентично
22
 * $c->one;        // Идентично
23
 *
24
 * Class Container
25
 */
26
class Container implements ContainerInterface
27
{
28
    /**
29
     * Экземпляры компонентов.
30
     * @var array
31
     */
32
    protected array $instances = [];
33
34
    /**
35
     * Конструкторы компонентов.
36
     * @var array
37
     */
38
    protected array $definitions = [];
39
40
    /**
41
     * @param string $name
42
     * @return object|null
43
     */
44
    public function __get(string $name)
45
    {
46
        return $this->get($name);
47
    }
48
49
    /**
50
     * @param string $name
51
     * @param mixed $value
52
     * @return void
53
     */
54
    public function __set(string $name, mixed $value): void
55
    {
56
        $this->set($name, $value);
57
    }
58
59
    /**
60
     * @param string $name
61
     * @return bool
62
     */
63
    public function __isset(string $name): bool
64
    {
65
        return $this->has($name);
66
    }
67
68
    /**
69
     * @param string $name
70
     * @return void
71
     */
72
    public function __unset(string $name): void
73
    {
74
        $this->unset($name);
75
    }
76
77
    /**
78
     * Получение компонента.
79
     * @param string $id
80
     * @return object|null
81
     */
82
    public function get(string $id): ?object
83
    {
84
        if (isset($this->instances[$id])) {
85
            return $this->instances[$id];
86
        }
87
88
        if (!array_key_exists($id, $this->definitions)) {
89
            return null;
90
        }
91
92
        $definition = $this->definitions[$id];
93
        $instance = $definition($this);
94
95
        return $this->instances[$id] = $instance;
96
    }
97
98
    /**
99
     * Регистрация компонента.
100
     * @param string $id
101
     * @param callable $definition
102
     * @return void
103
     */
104
    public function set(string $id, callable $definition): void
105
    {
106
        $this->unset($id);
107
        $this->definitions[$id] = $definition;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function has(string $id): bool
114
    {
115
        return array_key_exists($id, $this->definitions);
116
    }
117
118
    /**
119
     * Удаление компонента.
120
     * @param string $id
121
     * @return void
122
     */
123
    public function unset(string $id): void
124
    {
125
        if (array_key_exists($id, $this->instances)) {
126
            unset($this->instances[$id]);
127
        }
128
129
        if (array_key_exists($id, $this->definitions)) {
130
            unset($this->definitions[$id]);
131
        }
132
    }
133
}
134