|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the LaravelYaml package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\DependencyInjection\Builder\Instantiator; |
|
13
|
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\FactoryInterface; |
|
15
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Reference; |
|
16
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface; |
|
17
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Resolver\ParameterResolverInterface; |
|
18
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Resolver\ReferenceResolverInterface; |
|
19
|
|
|
use Illuminate\Contracts\Foundation\Application; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author Théo FIDRY <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class ServiceInstantiator |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Application |
|
28
|
|
|
*/ |
|
29
|
|
|
private $application; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var ParameterResolverInterface |
|
33
|
|
|
*/ |
|
34
|
|
|
private $parameterResolver; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var ReferenceResolverInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $referenceResolver; |
|
40
|
|
|
|
|
41
|
8 |
|
public function __construct( |
|
42
|
|
|
ParameterResolverInterface $parameterResolver, |
|
43
|
|
|
ReferenceResolverInterface $referenceResolver, |
|
44
|
|
|
Application $application |
|
45
|
|
|
) { |
|
46
|
8 |
|
$this->parameterResolver = $parameterResolver; |
|
47
|
8 |
|
$this->referenceResolver = $referenceResolver; |
|
48
|
8 |
|
$this->application = $application; |
|
49
|
8 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param ServiceInterface $service |
|
53
|
|
|
* |
|
54
|
|
|
* @return object |
|
55
|
|
|
*/ |
|
56
|
8 |
|
public function create(ServiceInterface $service) |
|
57
|
|
|
{ |
|
58
|
8 |
|
if ($service instanceof FactoryInterface) { |
|
59
|
4 |
|
return $this->factoryInstantiator($service); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
return $this->constructorInstantiator($service); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param ServiceInterface $service |
|
67
|
|
|
* |
|
68
|
|
|
* @return object |
|
69
|
|
|
*/ |
|
70
|
4 |
|
private function constructorInstantiator(ServiceInterface $service) |
|
71
|
|
|
{ |
|
72
|
4 |
|
$constructor = $service->getClass(); |
|
73
|
4 |
|
$resolvedArguments = $this->resolveArguments($service); |
|
74
|
|
|
|
|
75
|
4 |
|
return new $constructor(...$resolvedArguments); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param FactoryInterface $service |
|
80
|
|
|
* |
|
81
|
|
|
* @return object |
|
82
|
|
|
*/ |
|
83
|
4 |
|
private function factoryInstantiator(FactoryInterface $service) |
|
84
|
|
|
{ |
|
85
|
|
|
$factory = [ |
|
86
|
4 |
|
$this->resolveArgument($service->getFactory()[0]), |
|
87
|
4 |
|
$service->getFactory()[1], |
|
88
|
2 |
|
]; |
|
89
|
4 |
|
$resolvedArguments = $this->resolveArguments($service); |
|
90
|
|
|
|
|
91
|
4 |
|
return call_user_func_array($factory, $resolvedArguments); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* @param ServiceInterface $service |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
8 |
|
private function resolveArguments(ServiceInterface $service) |
|
100
|
|
|
{ |
|
101
|
8 |
|
$resolvedArguments = []; |
|
102
|
8 |
|
foreach ($service->getArguments() as $argument) { |
|
103
|
4 |
|
$resolvedArguments[] = $this->resolveArgument($argument); |
|
104
|
4 |
|
} |
|
105
|
|
|
|
|
106
|
8 |
|
return $resolvedArguments; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param Reference|string $argument |
|
111
|
|
|
* |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
6 |
|
private function resolveArgument($argument) |
|
115
|
|
|
{ |
|
116
|
6 |
|
if ($argument instanceof Reference) { |
|
117
|
4 |
|
return $this->referenceResolver->resolve($argument, $this->application); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
6 |
|
return $this->parameterResolver->resolve($argument); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|