Completed
Pull Request — 2.x (#349)
by Alexander
02:20
created

Container   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 102
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

5 Methods

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