|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Core\FactoryInterface; |
|
8
|
|
|
use Spiral\Core\Internal\Common\DestructorTrait; |
|
9
|
|
|
use Spiral\Core\Internal\Common\Registry; |
|
10
|
|
|
use Spiral\Core\Internal\Factory\Ctx; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
final class Factory implements FactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use DestructorTrait; |
|
18
|
|
|
|
|
19
|
|
|
private State $state; |
|
20
|
|
|
private Scope $scope; |
|
21
|
|
|
private Actor $actor; |
|
22
|
|
|
|
|
23
|
1443 |
|
public function __construct(Registry $constructor) |
|
24
|
|
|
{ |
|
25
|
1443 |
|
$constructor->set('factory', $this); |
|
26
|
|
|
|
|
27
|
1443 |
|
$this->state = $constructor->get('state', State::class); |
|
28
|
1443 |
|
$this->scope = $constructor->get('scope', Scope::class); |
|
29
|
1443 |
|
$this->actor = $constructor->get('actor', Actor::class); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param \Stringable|string|null $context Related to parameter caused injection if any. |
|
34
|
|
|
* |
|
35
|
|
|
* @throws \Throwable |
|
36
|
|
|
*/ |
|
37
|
1238 |
|
public function make( |
|
38
|
|
|
string $alias, |
|
39
|
|
|
array $parameters = [], |
|
40
|
|
|
\Stringable|string|null $context = null, |
|
41
|
|
|
?Tracer $tracer = null, |
|
42
|
|
|
): mixed { |
|
43
|
1238 |
|
if ($parameters === [] && \array_key_exists($alias, $this->state->singletons)) { |
|
44
|
638 |
|
return $this->state->singletons[$alias]; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
1238 |
|
$this->actor->resolveType($alias, $binding, $singleton, $injector, $actor, false); |
|
49
|
1237 |
|
if ($parameters === [] && $singleton !== null) { |
|
50
|
99 |
|
return $singleton; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1237 |
|
$tracer ??= new Tracer(); |
|
54
|
|
|
|
|
55
|
|
|
// Resolve without binding |
|
56
|
1237 |
|
if ($binding === null) { |
|
57
|
1080 |
|
$tracer->push( |
|
58
|
1080 |
|
false, |
|
59
|
1080 |
|
action: 'autowire', |
|
|
|
|
|
|
60
|
1080 |
|
alias: $alias, |
|
61
|
1080 |
|
scope: $this->scope->getScopeName(), |
|
|
|
|
|
|
62
|
1080 |
|
context: $context, |
|
|
|
|
|
|
63
|
1080 |
|
); |
|
64
|
|
|
try { |
|
65
|
|
|
// No direct instructions how to construct class, make is automatically |
|
66
|
1080 |
|
return $this->actor->autowire( |
|
67
|
1080 |
|
new Ctx(alias: $alias, class: $alias, context: $context, singleton: $parameters === [] ? null : false), |
|
68
|
1080 |
|
$parameters, |
|
69
|
1080 |
|
$actor, |
|
70
|
1080 |
|
$tracer, |
|
71
|
1080 |
|
); |
|
72
|
|
|
} finally { |
|
73
|
1080 |
|
$tracer->pop(false); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
try { |
|
78
|
1108 |
|
$tracer->push( |
|
79
|
1108 |
|
false, |
|
80
|
1108 |
|
action: 'resolve from binding', |
|
81
|
1108 |
|
alias: $alias, |
|
82
|
1108 |
|
scope: $this->scope->getScopeName(), |
|
83
|
1108 |
|
context: $context, |
|
84
|
1108 |
|
binding: $binding, |
|
85
|
1108 |
|
); |
|
86
|
1108 |
|
$tracer->push(true); |
|
87
|
|
|
|
|
88
|
1108 |
|
$actor->disableBinding($alias); |
|
89
|
1108 |
|
return $actor->resolveBinding($binding, $alias, $context, $parameters, $tracer); |
|
90
|
|
|
} finally { |
|
91
|
1108 |
|
$actor->enableBinding($alias, $binding); |
|
92
|
1108 |
|
$tracer->pop(true); |
|
93
|
1108 |
|
$tracer->pop(false); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|