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
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function add(string $id, $definition, bool $shared = false): DefinitionInterface |
22
|
|
|
{ |
23
|
|
|
if (! $definition instanceof DefinitionInterface) { |
24
|
|
|
$definition = (new Definition($id, $definition)); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$this->definitions[] = $definition |
28
|
|
|
->setContainer($this->getContainer()) |
29
|
|
|
->setAlias($id) |
30
|
|
|
->setShared($shared) |
31
|
|
|
; |
32
|
|
|
|
33
|
|
|
return $definition; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public function has(string $id): bool |
40
|
|
|
{ |
41
|
|
|
foreach ($this->getIterator() as $definition) { |
42
|
|
|
if ($id === $definition->getAlias()) { |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getDefinition(string $id): DefinitionInterface |
54
|
|
|
{ |
55
|
|
|
foreach ($this->getIterator() as $definition) { |
56
|
|
|
if ($id === $definition->getAlias()) { |
57
|
|
|
return $definition; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
|
|
public function resolve(string $id, array $args = [], bool $new = false) |
68
|
|
|
{ |
69
|
|
|
return $this->getDefinition($id)->resolve($args, $new); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getIterator(): Generator |
76
|
|
|
{ |
77
|
|
|
$count = count($this->definitions); |
78
|
|
|
|
79
|
|
|
for ($i = 0; $i < $count; $i++) { |
80
|
|
|
yield $this->definitions[$i]; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|