|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Container\Definition; |
|
4
|
|
|
|
|
5
|
|
|
use Generator; |
|
6
|
|
|
use League\Container\ContainerAwareTrait; |
|
7
|
|
|
use League\Container\Exception\NotFoundException; |
|
8
|
|
|
|
|
9
|
|
|
class DefinitionAggregate implements DefinitionAggregateInterface |
|
10
|
|
|
{ |
|
11
|
|
|
use ContainerAwareTrait; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var \League\Container\Definition\DefinitionInterface[] |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $definitions = []; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Construct. |
|
20
|
|
|
* |
|
21
|
|
|
* @param \League\Container\Definition\DefinitionInterface[] $definitions |
|
22
|
|
|
*/ |
|
23
|
|
|
public function __construct(array $definitions = []) |
|
24
|
|
|
{ |
|
25
|
60 |
|
$this->definitions = array_filter($definitions, function ($definition) { |
|
26
|
3 |
|
return ($definition instanceof DefinitionInterface); |
|
27
|
60 |
|
}); |
|
28
|
60 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
36 |
|
public function add(string $id, $definition, bool $shared = false): DefinitionInterface |
|
34
|
|
|
{ |
|
35
|
36 |
|
if (! $definition instanceof DefinitionInterface) { |
|
36
|
27 |
|
$definition = (new Definition($id, $definition)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
36 |
|
$this->definitions[] = $definition |
|
40
|
36 |
|
->setAlias($id) |
|
41
|
36 |
|
->setShared($shared) |
|
42
|
|
|
; |
|
43
|
|
|
|
|
44
|
36 |
|
return $definition; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
30 |
|
public function has(string $id): bool |
|
51
|
|
|
{ |
|
52
|
30 |
|
foreach ($this->getIterator() as $definition) { |
|
53
|
21 |
|
if ($id === $definition->getAlias()) { |
|
54
|
21 |
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
18 |
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* {@inheritdoc} |
|
63
|
|
|
*/ |
|
64
|
18 |
|
public function hasTag(string $tag): bool |
|
65
|
|
|
{ |
|
66
|
18 |
|
foreach ($this->getIterator() as $definition) { |
|
67
|
6 |
|
if ($definition->hasTag($tag)) { |
|
68
|
6 |
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
12 |
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
21 |
|
public function getDefinition(string $id): DefinitionInterface |
|
79
|
|
|
{ |
|
80
|
21 |
|
foreach ($this->getIterator() as $definition) { |
|
81
|
21 |
|
if ($id === $definition->getAlias()) { |
|
82
|
21 |
|
return $definition->setContainer($this->getContainer()); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
15 |
|
public function resolve(string $id, bool $new = false) |
|
93
|
|
|
{ |
|
94
|
15 |
|
return $this->getDefinition($id)->resolve($new); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* {@inheritdoc} |
|
99
|
|
|
*/ |
|
100
|
6 |
|
public function resolveTagged(string $tag, bool $new = false): array |
|
101
|
|
|
{ |
|
102
|
6 |
|
$arrayOf = []; |
|
103
|
|
|
|
|
104
|
6 |
|
foreach ($this->getIterator() as $definition) { |
|
105
|
6 |
|
if ($definition->hasTag($tag)) { |
|
106
|
6 |
|
$arrayOf[] = $definition->setContainer($this->getContainer())->resolve($new); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
6 |
|
return $arrayOf; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
42 |
|
public function getIterator(): Generator |
|
117
|
|
|
{ |
|
118
|
42 |
|
$count = count($this->definitions); |
|
119
|
|
|
|
|
120
|
42 |
|
for ($i = 0; $i < $count; $i++) { |
|
121
|
33 |
|
yield $this->definitions[$i]; |
|
122
|
|
|
} |
|
123
|
27 |
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|