|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AuraPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/auraphp/Aura.Di |
|
14
|
|
|
* @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\Container\Injection; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\Container\Resolver\Blueprint; |
|
22
|
|
|
use Phalcon\Container\Resolver\Resolver; |
|
23
|
|
|
use Psr\Container\ContainerInterface; |
|
24
|
|
|
use ReflectionException; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* A factory to create objects and values for injection into the Container. |
|
28
|
|
|
* |
|
29
|
|
|
* @property Resolver $resolver |
|
30
|
|
|
*/ |
|
31
|
|
|
class InjectionFactory |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* A Resolver to provide class-creation specifics. |
|
35
|
|
|
* |
|
36
|
|
|
* @var Resolver |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $resolver; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param Resolver $resolver A Resolver to provide class-creation specifics. |
|
44
|
|
|
*/ |
|
45
|
40 |
|
public function __construct(Resolver $resolver) |
|
46
|
|
|
{ |
|
47
|
40 |
|
$this->resolver = $resolver; |
|
48
|
40 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Returns the Resolver. |
|
52
|
|
|
* |
|
53
|
|
|
* @return Resolver |
|
54
|
|
|
*/ |
|
55
|
38 |
|
public function getResolver(): Resolver |
|
56
|
|
|
{ |
|
57
|
38 |
|
return $this->resolver; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* |
|
62
|
|
|
* Returns a new class instance. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Blueprint $blueprint The blueprint to instantiate. |
|
65
|
|
|
* |
|
66
|
|
|
* @return object |
|
67
|
|
|
* @throws ReflectionException |
|
68
|
|
|
*/ |
|
69
|
8 |
|
public function newInstance(Blueprint $blueprint): object |
|
70
|
|
|
{ |
|
71
|
8 |
|
return $this->resolver->resolve($blueprint); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns a new Factory. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $class The class to create. |
|
78
|
|
|
* @param array $params Override params for the class. |
|
79
|
|
|
* @param array $setters Override setters for the class. |
|
80
|
|
|
* |
|
81
|
|
|
* @return Factory |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function newFactory( |
|
84
|
|
|
$class, |
|
85
|
|
|
array $params = [], |
|
86
|
|
|
array $setters = [] |
|
87
|
|
|
): Factory { |
|
88
|
1 |
|
return new Factory($this->resolver, new Blueprint($class, $params, $setters)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Returns a new Lazy. |
|
93
|
|
|
* |
|
94
|
|
|
* @param callable $callable The callable to invoke. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $params Arguments for the callable. |
|
97
|
|
|
* |
|
98
|
|
|
* @return Lazy |
|
99
|
|
|
*/ |
|
100
|
4 |
|
public function newLazy($callable, array $params = []): Lazy |
|
101
|
|
|
{ |
|
102
|
4 |
|
return new Lazy($callable, $params); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns a new LazyArray. |
|
107
|
|
|
* |
|
108
|
|
|
* @param array $callables The callables to invoke. |
|
109
|
|
|
* |
|
110
|
|
|
* @return LazyArray |
|
111
|
|
|
*/ |
|
112
|
4 |
|
public function newLazyArray(array $callables): LazyArray |
|
113
|
|
|
{ |
|
114
|
4 |
|
return new LazyArray($callables); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns a new LazyCallable. |
|
119
|
|
|
* |
|
120
|
|
|
* @param callable $callable The callable to invoke. |
|
121
|
|
|
* |
|
122
|
|
|
* @return LazyCallable |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public function newLazyCallable($callable): LazyCallable |
|
125
|
|
|
{ |
|
126
|
2 |
|
return new LazyCallable($callable); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns a new LazyGet. |
|
131
|
|
|
* |
|
132
|
|
|
* @param ContainerInterface $container The service container. |
|
133
|
|
|
* @param string $service The service to retrieve. |
|
134
|
|
|
* |
|
135
|
|
|
* @return LazyGet |
|
136
|
|
|
*/ |
|
137
|
3 |
|
public function newLazyGet(ContainerInterface $container, string $service): LazyGet |
|
138
|
|
|
{ |
|
139
|
3 |
|
return new LazyGet($container, $service); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns a new LazyInclude. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $file The file to include. |
|
146
|
|
|
* |
|
147
|
|
|
* @return LazyInclude |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function newLazyInclude(string $file): LazyInclude |
|
150
|
|
|
{ |
|
151
|
1 |
|
return new LazyInclude($file); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Returns a new LazyNew. |
|
156
|
|
|
* |
|
157
|
|
|
* @param string $class The class to instantiate. |
|
158
|
|
|
* @param array $params Params for the instantiation. |
|
159
|
|
|
* @param array $setters Setters for the instantiation. |
|
160
|
|
|
* |
|
161
|
|
|
* @return LazyNew |
|
162
|
|
|
*/ |
|
163
|
11 |
|
public function newLazyNew( |
|
164
|
|
|
string $class, |
|
165
|
|
|
array $params = [], |
|
166
|
|
|
array $setters = [] |
|
167
|
|
|
): LazyNew { |
|
168
|
11 |
|
return new LazyNew($this->resolver, new Blueprint($class, $params, $setters)); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns a new LazyRequire. |
|
173
|
|
|
* |
|
174
|
|
|
* @param string $file The file to require. |
|
175
|
|
|
* |
|
176
|
|
|
* @return LazyRequire |
|
177
|
|
|
*/ |
|
178
|
1 |
|
public function newLazyRequire(string $file): LazyRequire |
|
179
|
|
|
{ |
|
180
|
1 |
|
return new LazyRequire($file); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Returns a new LazyValue. |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $key The value key to use. |
|
187
|
|
|
* |
|
188
|
|
|
* @return LazyValue |
|
189
|
|
|
*/ |
|
190
|
3 |
|
public function newLazyValue(string $key): LazyValue |
|
191
|
|
|
{ |
|
192
|
3 |
|
return new LazyValue($this->resolver, $key); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|