Completed
Pull Request — master (#107)
by
unknown
13:44
created

SingleInstanceContainer::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
c 1
b 0
f 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace League\Container;
4
5
use Interop\Container\ContainerInterface as InteropContainerInterface;
6
7
/**
8
 * Wraps a container intercepting requests for dependencies and caching the return.
9
 *
10
 * This ensures that a single instance of any service ID is only ever returned. This
11
 * be used with a reflection container to provide zero-configuration, single instance
12
 * DI.
13
 */
14
class SingleInstanceContainer implements InteropContainerInterface
15
{
16
    /**
17
     * @var InteropContainerInterface
18
     */
19
    private $wrapped;
20
    
21
    /**
22
     * @var mixed[]
23
     */
24
    private $instances = [];
25
    
26
    
27
    /**
28
     * @param InteropContainerInterface $container
29
     */
30
    public function __construct(InteropContainerInterface $container)
31
    {
32
        $this->wrapped = $container;
33
    }
34
    
35
    /**
36
     * @inheritdoc
37
     */
38
    public function get($id)
39
    {
40
        if (!isset($this->instances[$id])) {
41
            $this->instances[$id] = $this->wrapped->get($id);
42
        }
43
        
44
        return $this->instances[$id];
45
    }
46
    
47
    /**
48
     * @inheritdoc
49
     */
50
    public function has($id)
51
    {
52
        return $this->wrapped->has($id);
53
    }
54
}
55