-
Notifications
You must be signed in to change notification settings - Fork 16
/
ContextRegistry.php
139 lines (110 loc) 路 4.27 KB
/
ContextRegistry.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Castor;
use Castor\Exception\FunctionConfigurationException;
/** @internal */
class ContextRegistry
{
/** @var array<string, ContextDescriptor> */
private array $descriptors = [];
private ?string $defaultName = null;
private Context $currentContext;
public function addDescriptor(ContextDescriptor $descriptor): void
{
$name = $descriptor->contextAttribute->name;
if (\array_key_exists($name, $this->descriptors)) {
$alreadyDefined = $this->descriptors[$name]->function;
throw new FunctionConfigurationException(sprintf('You cannot define two contexts with the same name "%s". There is one already defined in "%s:%d".', $name, PathHelper::makeRelative((string) $alreadyDefined->getFileName()), $alreadyDefined->getStartLine()), $descriptor->function);
}
$this->descriptors[$name] = $descriptor;
if ($descriptor->contextAttribute->default) {
if ($this->defaultName) {
$alreadyDefined = $this->descriptors[$this->defaultName]->function;
throw new FunctionConfigurationException(sprintf('You cannot set multiple "default: true" context. There is one already defined in "%s:%d".', PathHelper::makeRelative((string) $alreadyDefined->getFileName()), $alreadyDefined->getStartLine()), $descriptor->function);
}
$this->defaultName = $name;
}
}
/**
* @param \Closure(): Context $callable
*/
public function addContext(string $name, callable $callable, bool $default = false): void
{
$this->addDescriptor(new ContextDescriptor(
new Attribute\AsContext(name: $name, default: $default),
new \ReflectionFunction($callable),
));
}
public function setDefaultIfEmpty(): void
{
if ($this->defaultName) {
return;
}
if (!$this->descriptors) {
return;
}
if (1 < \count($this->descriptors)) {
throw new \RuntimeException(sprintf('Since there are multiple contexts "%s", you must set a "default: true" context.', implode('", "', $this->getNames())));
}
$this->defaultName = array_key_first($this->descriptors);
}
public function getDefaultName(): string
{
return $this->defaultName ?? throw new \LogicException('Default context name not set yet.');
}
public function get(?string $name = null): Context
{
if (null === $name) {
return $this->getCurrentContext();
}
if (!\array_key_exists($name, $this->descriptors)) {
throw new \RuntimeException(sprintf('Context "%s" not found.', $name));
}
$context = $this->descriptors[$name]->function->invoke();
if (!$context instanceof Context) {
throw new FunctionConfigurationException(sprintf('The context generator must return an instance of "%s", "%s" returned.', Context::class, get_debug_type($context)), $this->descriptors[$name]->function);
}
return $context;
}
public function hasCurrentContext(): bool
{
return isset($this->currentContext);
}
public function setCurrentContext(Context $context): void
{
$this->currentContext = $context;
}
public function getCurrentContext(): Context
{
if (isset($this->currentContext)) {
return $this->currentContext;
}
trigger_deprecation('castor', '0.11.1', 'Calling getCurrentContext() without setCurrentContext() is deprecated. Pass a context instead to the function, or set a Current context before.');
return new Context();
}
/**
* @template TKey of key-of<ContextData>
* @template TDefault
*
* @param TKey|string $key
* @param TDefault $default
*
* @phpstan-return ($key is TKey ? ContextData[TKey] : TDefault)
*/
public function getVariable(string $key, mixed $default = null): mixed
{
$context = $this->getCurrentContext();
if (!isset($context[$key])) {
return $default;
}
return $context[$key];
}
/**
* @return array<string>
*/
public function getNames(): array
{
$names = array_keys($this->descriptors);
sort($names);
return $names;
}
}