Completed
Push — context ( 640cfc )
by Akihito
02:39
created

DependencyProvider::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Ray.Di package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace Ray\Di;
8
9
final class DependencyProvider implements DependencyInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    public $context;
15
16
    /**
17
     * Provider dependency
18
     *
19
     * @var Dependency
20
     */
21
    private $dependency;
22
23
    /**
24
     * @var bool
25
     */
26
    private $isSingleton = false;
27
28
    /**
29
     * @var mixed
30
     */
31
    private $instance;
32
33 47
    public function __construct(Dependency $dependency, $context = null)
34
    {
35 47
        $this->dependency = $dependency;
36 47
        $this->context = $context;
37 47
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 47
    public function register(array &$container, Bind $bind)
43
    {
44 47
        $container[(string) $bind] = $bind->getBound();
45 47
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 13
    public function inject(Container $container)
51
    {
52 13
        if ($this->isSingleton && $this->instance) {
53 1
            return $this->instance;
54
        }
55 13
        $provider = $this->dependency->inject($container);
56 13
        if ($provider instanceof SetContextInterface) {
57 2
            $this->setContext($provider);
58
        }
59 13
        $this->instance = $provider->get();
60
61 13
        return $this->instance;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 40
    public function setScope($scope)
68
    {
69 40
        if ($scope === Scope::SINGLETON) {
70 40
            $this->isSingleton = true;
71
        }
72 40
    }
73
74 2
    public function setContext(SetContextInterface $provider)
75
    {
76 2
        $provider->setContext($this->context);
77 2
    }
78
79 2
    public function __sleep()
80
    {
81 2
        return ['dependency', 'isSingleton'];
82
    }
83
}
84