Container::object()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * Container
4
 * User: moyo
5
 * Date: 2018/8/16
6
 * Time: 4:45 PM
7
 */
8
9
namespace Carno\Container;
10
11
use Carno\Container\Exception\ObjectNotFoundException;
12
use Carno\Container\Injection\Constructor;
13
use Psr\Container\ContainerInterface;
14
15
class Container implements ContainerInterface
16
{
17
    use Constructor;
18
19
    /**
20
     * @var object[]
21
     */
22
    private $instances = [];
23
24
    /**
25
     * @param string $id
26
     * @return bool
27
     */
28
    public function has($id) : bool
29
    {
30
        return isset($this->instances[$id]);
31
    }
32
33
    /**
34
     * @param string $id
35
     * @return object|mixed
36
     */
37
    public function get($id)
38
    {
39
        if (isset($this->instances[$id])) {
40
            return $this->instances[$id];
41
        }
42
43
        throw new ObjectNotFoundException($id);
44
    }
45
46
    /**
47
     * @param string $id
48
     * @param object $object
49
     * @return object|mixed
50
     */
51
    public function set(string $id, $object)
52
    {
53
        assert(is_object($object));
54
        return $this->instances[$id] = $object;
55
    }
56
57
    /**
58
     * @param string $class
59
     * @param mixed ...$args
60
     * @return object|mixed
61
     */
62
    public function object(string $class, ...$args)
63
    {
64
        return $this->creating($class, ...$args);
65
    }
66
}
67