|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Frontend\Definition; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
|
13
|
|
|
use Railt\SDL\Exception\NotFoundException; |
|
14
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
|
15
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
|
16
|
|
|
use Railt\SDL\Frontend\Deferred\Deferred; |
|
17
|
|
|
use Railt\SDL\Frontend\Deferred\DeferredInterface; |
|
18
|
|
|
use Railt\SDL\Frontend\Map; |
|
19
|
|
|
use Railt\SDL\IR\Definition\DefinitionValueObject; |
|
20
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
|
21
|
|
|
use Railt\SDL\Naming\StrategyInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Storage |
|
25
|
|
|
*/ |
|
26
|
|
|
class Storage |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|DefinitionInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $definitions; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array|DeferredInterface[] |
|
35
|
|
|
*/ |
|
36
|
|
|
private $resolvers; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var DefinitionValueObject[] |
|
40
|
|
|
*/ |
|
41
|
|
|
private $resolved = []; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var StrategyInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $naming; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Pipeline constructor. |
|
50
|
|
|
* @param StrategyInterface $naming |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(StrategyInterface $naming) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->naming = $naming; |
|
55
|
|
|
|
|
56
|
|
|
$this->definitions = new Map(function (TypeNameInterface $name): string { |
|
|
|
|
|
|
57
|
|
|
return $name->getFullyQualifiedName(); |
|
58
|
|
|
}); |
|
59
|
|
|
|
|
60
|
|
|
$this->resolvers = new Map(function (DefinitionInterface $definition): string { |
|
|
|
|
|
|
61
|
|
|
return $definition->getName()->getFullyQualifiedName(); |
|
62
|
|
|
}); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param DefinitionInterface $definition |
|
67
|
|
|
* @param DeferredInterface $deferred |
|
68
|
|
|
* @return $this|Storage |
|
69
|
|
|
*/ |
|
70
|
|
|
public function remember(DefinitionInterface $definition, DeferredInterface $deferred): Storage |
|
71
|
|
|
{ |
|
72
|
|
|
$this->resolvers[$definition] = $deferred; |
|
73
|
|
|
$this->definitions[$definition->getName()] = $definition; |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param InvocationInterface $invocation |
|
80
|
|
|
* @return \Generator|mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public function invoke(InvocationInterface $invocation) |
|
83
|
|
|
{ |
|
84
|
|
|
$name = $this->naming->resolve($invocation->getName(), $invocation->getArguments()); |
|
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
if ($this->isResolved($name)) { |
|
87
|
|
|
return $this->resolved[$name]; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
[$definition, $deferred] = $this->get($invocation->getName(), $invocation->getContext()); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
return $deferred->invoke($definition, $invocation); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $name |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
private function isResolved(string $name): bool |
|
100
|
|
|
{ |
|
101
|
|
|
return isset($this->resolved[$name]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param TypeNameInterface $name |
|
106
|
|
|
* @param ContextInterface $ctx |
|
107
|
|
|
* @return array |
|
108
|
|
|
*/ |
|
109
|
|
|
private function get(TypeNameInterface $name, ContextInterface $ctx): array |
|
110
|
|
|
{ |
|
111
|
|
|
$result = $this->find($name) ?? $this->find($name->in($ctx->getName())); |
|
112
|
|
|
|
|
113
|
|
|
if ($result === null) { |
|
114
|
|
|
$error = 'Type %s not found or could not be loaded'; |
|
115
|
|
|
throw new NotFoundException(\sprintf($error, $name)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $result; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param TypeNameInterface $name |
|
123
|
|
|
* @return array|null |
|
124
|
|
|
*/ |
|
125
|
|
|
private function find(TypeNameInterface $name): ?array |
|
126
|
|
|
{ |
|
127
|
|
|
if (isset($this->definitions[$name])) { |
|
128
|
|
|
$definition = $this->definitions[$name]; |
|
129
|
|
|
|
|
130
|
|
|
return [$definition, $this->resolvers[$definition]]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return null; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @param InvocationInterface $name |
|
138
|
|
|
* @return bool |
|
139
|
|
|
*/ |
|
140
|
|
|
public function resolved(InvocationInterface $name): bool |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->isResolved($this->naming->resolve($name->getName(), $name->getArguments())); |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param \Closure $filter |
|
147
|
|
|
* @return \Generator|DeferredInterface[] |
|
148
|
|
|
*/ |
|
149
|
|
|
public function export(\Closure $filter): \Generator |
|
150
|
|
|
{ |
|
151
|
|
|
/** |
|
152
|
|
|
* @var DefinitionInterface $definition |
|
153
|
|
|
* @var DeferredInterface $then |
|
154
|
|
|
*/ |
|
155
|
|
|
foreach ($this->resolvers as $definition => $then) { |
|
156
|
|
|
if ($filter($definition)) { |
|
157
|
|
|
yield $this->toInvocationDeferred($definition, $then); |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param DefinitionInterface $def |
|
164
|
|
|
* @param DeferredInterface $then |
|
165
|
|
|
* @return DeferredInterface |
|
166
|
|
|
*/ |
|
167
|
|
|
private function toInvocationDeferred(DefinitionInterface $def, DeferredInterface $then): DeferredInterface |
|
168
|
|
|
{ |
|
169
|
|
|
return new Deferred($this->toInvocationDeferredCallback($def, $then)); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param DefinitionInterface $original |
|
174
|
|
|
* @param DeferredInterface $then |
|
175
|
|
|
* @return \Closure |
|
176
|
|
|
*/ |
|
177
|
|
|
private function toInvocationDeferredCallback(DefinitionInterface $original, DeferredInterface $then): \Closure |
|
178
|
|
|
{ |
|
179
|
|
|
return function (DefinitionInterface $definition = null, InvocationInterface $invocation = null) use ( |
|
180
|
|
|
$original, |
|
181
|
|
|
$then |
|
182
|
|
|
) { |
|
183
|
|
|
$definition = $definition ?? $original; |
|
184
|
|
|
$invocation = $invocation ?? new Invocation($definition->getName(), $definition->getContext()); |
|
185
|
|
|
|
|
186
|
|
|
return $then->invoke($definition, $invocation); |
|
187
|
|
|
}; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..