1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace League\Container\Definition; |
4
|
|
|
|
5
|
|
|
use Generator; |
6
|
|
|
use League\Container\Argument\RawArgument; |
7
|
|
|
use League\Container\ContainerAwareTrait; |
8
|
|
|
use League\Container\Exception\NotFoundException; |
9
|
|
|
|
10
|
|
|
class DefinitionAggregate implements DefinitionAggregateInterface |
11
|
|
|
{ |
12
|
|
|
use ContainerAwareTrait; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var \League\Container\Definition\DefinitionInterface[] |
16
|
|
|
*/ |
17
|
|
|
protected $definitions = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* {@inheritdoc} |
21
|
|
|
*/ |
22
|
|
|
public function add(string $id, $definition, bool $shared = false): DefinitionInterface |
23
|
|
|
{ |
24
|
|
|
if (! $definition instanceof DefinitionInterface) { |
25
|
|
|
$definition = $this->factory($definition); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$definition->setAlias($id)->setShared($shared); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$this->definitions[] = $definition; |
31
|
|
|
|
32
|
|
|
return $this; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
|
|
public function factory($concrete): DefinitionInterface |
39
|
|
|
{ |
40
|
|
|
// TODO handle the never ending struggle of __invoke |
41
|
|
|
if (is_callable($concrete)) { |
42
|
|
|
return (new CallableDefinition($concrete))->setContainer($this->getContainer()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (is_string($concrete) && class_exists($concrete)) { |
46
|
|
|
return (new ClassDefinition($concrete))->setContainer($this->getContainer()); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$concrete = ($concrete instanceof RawArgument) ? $concrete : new RawArgument($concrete); |
50
|
|
|
|
51
|
|
|
return new Definition($concrete); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function resolve(string $id) |
58
|
|
|
{ |
59
|
|
|
foreach ($this->getIterator() as $definition) { |
60
|
|
|
if ($id === $definition->getAlias()) { |
61
|
|
|
return $definition->resolve(); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
throw new NotFoundException(sprintf('Alias (%s) is not being handled by the container', $id)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getIterator(): Generator |
72
|
|
|
{ |
73
|
|
|
$count = count($this->definitions); |
74
|
|
|
|
75
|
|
|
for ($i = 0; $i < $count; $i++) { |
76
|
|
|
yield $this->definitions[$i]; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.