1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Core\Internal; |
6
|
|
|
|
7
|
|
|
use Spiral\Core\Exception\Scope\NamedScopeDuplicationException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @internal |
11
|
|
|
*/ |
12
|
|
|
final class Scope |
13
|
|
|
{ |
14
|
|
|
private ?\Spiral\Core\Container $parent = null; |
15
|
|
|
private ?self $parentScope = null; |
16
|
|
|
private ?Actor $parentActor = null; |
17
|
|
|
|
18
|
1443 |
|
public function __construct( |
19
|
|
|
private readonly ?string $scopeName = null, |
20
|
1443 |
|
) {} |
21
|
|
|
|
22
|
1294 |
|
public function getScopeName(): ?string |
23
|
|
|
{ |
24
|
1294 |
|
return $this->scopeName; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Link the current scope with its parent scope and container. |
29
|
|
|
* |
30
|
|
|
* @throws NamedScopeDuplicationException |
31
|
|
|
*/ |
32
|
342 |
|
public function setParent( |
33
|
|
|
\Spiral\Core\Container $parent, |
34
|
|
|
self $parentScope, |
35
|
|
|
Actor $actor, |
36
|
|
|
): void { |
37
|
342 |
|
$this->parent = $parent; |
38
|
342 |
|
$this->parentScope = $parentScope; |
39
|
342 |
|
$this->parentActor = $actor; |
40
|
|
|
|
41
|
|
|
// Check a scope with the same name is not already registered |
42
|
342 |
|
if ($this->scopeName !== null) { |
43
|
301 |
|
$tmp = $this; |
44
|
301 |
|
while ($tmp->parentScope !== null) { |
45
|
301 |
|
$tmp = $tmp->parentScope; |
46
|
301 |
|
$tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
610 |
|
public function getParent(): ?\Spiral\Core\Container |
52
|
|
|
{ |
53
|
610 |
|
return $this->parent; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return list of parent scope names. |
58
|
|
|
* The first element is the current scope name, and the next is the closest parent scope name... |
59
|
|
|
* |
60
|
|
|
* @return array<int<0, max>, string|null> |
|
|
|
|
61
|
|
|
*/ |
62
|
|
|
public function getParentScopeNames(): array |
63
|
|
|
{ |
64
|
|
|
$result = [$this->scopeName]; |
65
|
|
|
|
66
|
|
|
$parent = $this; |
67
|
|
|
while ($parent->parentScope !== null) { |
68
|
|
|
$parent = $parent->parentScope; |
69
|
|
|
$result[] = $parent->scopeName; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
1069 |
|
public function getParentScope(): ?self |
76
|
|
|
{ |
77
|
1069 |
|
return $this->parentScope; |
78
|
|
|
} |
79
|
|
|
|
80
|
1093 |
|
public function getParentActor(): ?Actor |
81
|
|
|
{ |
82
|
1093 |
|
return $this->parentActor; |
83
|
|
|
} |
84
|
|
|
|
85
|
609 |
|
public function destruct(): void |
86
|
|
|
{ |
87
|
609 |
|
$this->parent = null; |
88
|
609 |
|
$this->parentScope = null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|