Completed
Push — 1.x ( c183a4...05b51a )
by Alexander
8s
created

Container   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 86.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 107
ccs 26
cts 30
cp 0.8667
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 7 2
A share() 0 16 3
A get() 0 11 3
A has() 0 4 1
A getByTag() 0 11 3
1
<?php
2
/*
3
 * Go! AOP framework
4
 *
5
 * @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
6
 *
7
 * This source file is subject to the license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
11
namespace Go\Core;
12
13
/**
14
 * DI-container
15
 */
16
class Container
17
{
18
    /**
19
     * List of services in the container
20
     *
21
     * @var array
22
     */
23
    protected $values = [];
24
25
    /**
26
     * Store identifiers os services by tags
27
     *
28
     * @var array
29
     */
30
    protected $tags = [];
31
32
    /**
33
     * Set a service into the container
34
     *
35
     * @param string $id Identifier
36
     * @param mixed $value Value to store
37
     * @param array $tags Additional tags
38
     */
39 10
    public function set($id, $value, array $tags = [])
40
    {
41 10
        $this->values[$id] = $value;
42 10
        foreach ($tags as $tag) {
43 3
            $this->tags[$tag][] = $id;
44
        }
45 10
    }
46
47
    /**
48
     * Set a shared value in the container
49
     *
50
     * @param string $id Identifier
51
     * @param callable $value Value to store
52
     * @param array $tags Additional tags
53
     *
54
     * @throws \InvalidArgumentException if value is not callable
55
     */
56 10
    public function share($id, $value, array $tags = [])
57
    {
58 10
        if (!is_callable($value)) {
59
            throw new \InvalidArgumentException("Only callable values can be shared in the container");
60
        }
61 10
        $value = function($container) use ($value) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
62 6
            static $sharedValue;
63
64 6
            if (null === $sharedValue) {
65 6
                $sharedValue = $value($container);
66
            }
67
68 6
            return $sharedValue;
69 10
        };
70 10
        $this->set($id, $value, $tags);
71 10
    }
72
73
    /**
74
     * Return a service or value from the container
75
     *
76
     * @param string $id Identifier
77
     *
78
     * @return mixed
79
     * @throws \OutOfBoundsException if service was not found
80
     */
81 9
    public function get($id)
82
    {
83 9
        if (!isset($this->values[$id])) {
84
            throw new \OutOfBoundsException("Value {$id} is not defined in the container");
85
        }
86 9
        if (is_callable($this->values[$id])) {
87 6
            return $this->values[$id]($this);
88
        } else {
89 6
            return $this->values[$id];
90
        }
91
    }
92
93
    /**
94
     * Checks if item with specified id is present in the container
95
     *
96
     * @param string $id Identifier
97
     *
98
     * @return bool
99
     */
100
    public function has($id)
101
    {
102
        return isset($this->values[$id]);
103
    }
104
105
    /**
106
     * Return list of service tagged with marker
107
     *
108
     * @param string $tag Tag to select
109
     * @return array
110
     */
111 3
    public function getByTag($tag)
112
    {
113 3
        $result = [];
114 3
        if (isset($this->tags[$tag])) {
115 3
            foreach ($this->tags[$tag] as $id) {
116 3
                $result[$id] = $this->get($id);
117
            }
118
        }
119
120 3
        return $result;
121
    }
122
}
123