-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayDefinition.php
More file actions
110 lines (98 loc) · 3.2 KB
/
ArrayDefinition.php
File metadata and controls
110 lines (98 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
namespace Peak\Di;
use function array_shift;
use function class_exists;
use function is_array;
use function is_callable;
use function is_object;
use Peak\Di\Exception\InfiniteLoopResolutionException;
class ArrayDefinition
{
/**
* If true, check in the container before create a new instance of an object
* @var bool
*/
private $newInstanceOnly = false;
/**
* @var ClassInstantiator
*/
private $instantiator;
/**
* @var ClassResolver
*/
private $classResolver;
/**
* @var int
*/
private $n = 0;
/**
* Constructor.
*/
public function __construct()
{
$this->instantiator = new ClassInstantiator();
$this->classResolver = new ClassResolver();
}
/**
* @param array $definition
* @param Container $container
* @param array $args
* @return object
* @throws Exception\AmbiguousResolutionException
* @throws Exception\ClassDefinitionNotFoundException
* @throws Exception\InterfaceNotFoundException
* @throws InfiniteLoopResolutionException
* @throws \ReflectionException
*/
public function resolve(array $definition, Container $container, array $args = [])
{
$final_args = $definition;
if (!empty($args)) { // add create argument at the end
foreach ($args as $arg) {
$final_args[] = $arg;
}
}
$definition = array_shift($final_args);
foreach ($final_args as $index => $arg) {
if (is_array($arg)) {
$final_args[$index] = $this->resolve($arg, $container);
} elseif (is_callable($arg)) {
$final_args[$index] = $arg($container);
} elseif (is_object($arg)) {
$final_args[$index] = $arg;
} elseif (class_exists($arg) && $this->newInstanceOnly) {
$this->n++;
if ($this->n > 1) {
throw new InfiniteLoopResolutionException($definition);
}
$subArg = $this->classResolver->resolve($arg, $container, $args);
$final_args[$index] = $this->instantiator->instantiate($arg, $subArg);
} elseif (class_exists($arg) && !$this->newInstanceOnly) {
if ($container->has($arg)) {
$final_args[$index] = $container->get($arg);
} elseif ($container->hasDefinition($arg)) {
$final_args[$index] = $container->resolve($arg);
} else {
$this->n++;
if ($this->n > 1) {
throw new InfiniteLoopResolutionException($definition);
}
$subArg = $this->classResolver->resolve($arg, $container, $args);
$final_args[$index] = $this->instantiator->instantiate($arg, $subArg);
}
}
}
return $this->instantiator->instantiate($definition, $final_args);
}
/**
* Set string resolution to new instance only
*
* @return $this
*/
public function newInstancesOnly()
{
$this->newInstanceOnly = true;
return $this;
}
}