-
Notifications
You must be signed in to change notification settings - Fork 16
/
ContextRegistry.php
148 lines (118 loc) 路 4.66 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
140
141
142
143
144
145
146
147
148
<?php
namespace Castor;
use Symfony\Component\PropertyAccess\PropertyAccess;
/** @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)) {
throw new \RuntimeException(sprintf('You cannot defined two context with the same name "%s". There is one defined in "%s" and another in "%s".', $name, $this->describeFunction($this->descriptors[$name]->function), $this->describeFunction($descriptor->function)));
}
$this->descriptors[$name] = $descriptor;
if ($descriptor->contextAttribute->default) {
if ($this->defaultName) {
throw new \RuntimeException(sprintf('You cannot set multiple "default: true" context. There is one defined in "%s" and another in "%s".', $this->defaultName, $this->describeFunction($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 \LogicException(sprintf('The context generator "%s", defined at "%s:%s" must return an instance of "%s", "%s" returned', $name, $this->descriptors[$name]->function->getFileName(), $this->descriptors[$name]->function->getStartLine(), Context::class, get_debug_type($context)));
}
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 (str_starts_with($key, '[')) {
return PropertyAccess::createPropertyAccessor()->getValue($context, $key) ?? $default;
}
return $context[$key] ?? $default;
}
/**
* @return array<string>
*/
public function getNames(): array
{
$names = array_keys($this->descriptors);
sort($names);
return $names;
}
private function describeFunction(\ReflectionFunction $function): string
{
$name = $function->getName();
$shortFilename = str_replace(PathHelper::getRoot() . '/', '', (string) $function->getFileName());
$location = sprintf('%s:%d', $shortFilename, $function->getStartLine());
if (str_contains($name, '{closure}')) {
return $location;
}
return sprintf('%s@%s', $name, $location);
}
}