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
|
|
|
|
17
|
1281 |
|
public function __construct( |
18
|
|
|
private readonly ?string $scopeName = null, |
19
|
|
|
) { |
20
|
1281 |
|
} |
21
|
|
|
|
22
|
1119 |
|
public function getScopeName(): ?string |
23
|
|
|
{ |
24
|
1119 |
|
return $this->scopeName; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Link the current scope with its parent scope and container. |
29
|
|
|
* |
30
|
|
|
* @throws NamedScopeDuplicationException |
31
|
|
|
*/ |
32
|
25 |
|
public function setParent(\Spiral\Core\Container $parent, self $parentScope): void |
33
|
|
|
{ |
34
|
25 |
|
$this->parent = $parent; |
35
|
25 |
|
$this->parentScope = $parentScope; |
36
|
|
|
|
37
|
|
|
// Check a scope with the same name is not already registered |
38
|
25 |
|
if ($this->scopeName !== null) { |
39
|
8 |
|
$tmp = $this; |
40
|
8 |
|
while ($tmp->parentScope !== null) { |
41
|
8 |
|
$tmp = $tmp->parentScope; |
42
|
8 |
|
$tmp->scopeName !== $this->scopeName ?: throw new NamedScopeDuplicationException($this->scopeName); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
914 |
|
public function getParent(): ?\Spiral\Core\Container |
48
|
|
|
{ |
49
|
914 |
|
return $this->parent; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return list of parent scope names. |
54
|
|
|
* The first element is the current scope name, and the next is the closest parent scope name... |
55
|
|
|
* |
56
|
|
|
* @return array<int<0, max>, string|null> |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function getParentScopeNames(): array |
59
|
|
|
{ |
60
|
|
|
$result = [$this->scopeName]; |
61
|
|
|
|
62
|
|
|
$parent = $this; |
63
|
|
|
while ($parent->parentScope !== null) { |
64
|
|
|
$parent = $parent->parentScope; |
65
|
|
|
$result[] = $parent->scopeName; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $result; |
69
|
|
|
} |
70
|
|
|
|
71
|
871 |
|
public function getParentScope(): ?self |
72
|
|
|
{ |
73
|
871 |
|
return $this->parentScope; |
74
|
|
|
} |
75
|
|
|
|
76
|
614 |
|
public function destruct(): void |
77
|
|
|
{ |
78
|
614 |
|
$this->parent = null; |
79
|
614 |
|
$this->parentScope = null; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|