-
Notifications
You must be signed in to change notification settings - Fork 0
/
TemporaryContextInitializer.php
84 lines (66 loc) · 1.87 KB
/
TemporaryContextInitializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
declare(strict_types=1);
namespace Honeystone\Context;
use Closure;
use Honeystone\Context\Contracts\DefinesContext;
use Honeystone\Context\Contracts\InitializesContext;
use Honeystone\Context\Contracts\InitializesTemporaryContext;
use Honeystone\Context\Contracts\ManagesContext;
use Honeystone\Context\Contracts\ResolvesContext;
final class TemporaryContextInitializer implements InitializesTemporaryContext
{
public function __construct(
private readonly Closure $up,
private readonly Closure $down,
private readonly InitializesContext $initializer,
) {
}
public function start(): ManagesContext
{
return ($this->up)($this);
}
public function end(): ManagesContext
{
return ($this->down)($this);
}
public function run(Closure $closure): mixed
{
$this->start();
try {
$result = $closure();
} finally {
$this->end();
}
return $result;
}
public function resolve(ResolvesContext $resolver): self
{
$this->initializer->resolve($resolver);
return $this;
}
public function hasResolved(string $name, bool $strict = true): bool
{
return $this->initializer->hasResolved($name, $strict);
}
public function getResolved(string $name, bool $strict = true): ?object
{
return $this->initializer->getResolved($name, $strict);
}
public function getAllResolved(): array
{
return $this->initializer->getAllResolved();
}
public function getDefinition(): DefinesContext
{
return $this->initializer->getDefinition();
}
public function serialize(): array
{
return $this->initializer->serialize();
}
public function deserialize(array $data): self
{
$this->initializer->deserialize($data);
return $this;
}
}