-
Notifications
You must be signed in to change notification settings - Fork 938
/
Context.php
295 lines (264 loc) · 8.7 KB
/
Context.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi;
use OpenApi\Annotations as OA;
use OpenApi\Loggers\DefaultLogger;
use Psr\Log\LoggerInterface;
/**
* The context in which the annotation is parsed.
*
* Contexts are nested to reflect the code/parsing hierarchy. They include useful metadata
* which the processors can use to augment the annotations.
*
* @property string|null $comment The PHP DocComment
* @property string|null $filename
* @property int|null $line
* @property int|null $character
* @property string|null $namespace
* @property array|null $uses
* @property string|null $class
* @property string|null $interface
* @property string|null $trait
* @property string|null $enum
* @property array|string|null $extends Interfaces may extend a list of interfaces
* @property array|null $implements
* @property string|null $method
* @property string|null $property
* @property string|\ReflectionType|null $type
* @property bool|null $static Indicate a static method
* @property bool|null $nullable Indicate a nullable value
* @property bool|null $generated Indicate the context was generated by a processor or
* the serializer
* @property OA\AbstractAnnotation|null $nested
* @property OA\AbstractAnnotation[]|null $annotations
* @property OA\AbstractAnnotation[]|null $other Annotations not related to OpenApi
* @property LoggerInterface|null $logger Guaranteed to be set when using the `Generator`
* @property array|null $scanned Details of file scanner when using ReflectionAnalyser
* @property string|null $version The OpenAPI version in use
*/
#[\AllowDynamicProperties]
class Context
{
/**
* Prototypical inheritance for properties.
*
* @var Context|null
*/
private $parent;
/**
* @deprecated
*/
public function clone()
{
return new Context(get_object_vars($this), $this->parent);
}
public function __construct(array $properties = [], ?Context $parent = null)
{
foreach ($properties as $property => $value) {
$this->{$property} = $value;
}
$this->parent = $parent;
$this->logger = $this->logger ?: new DefaultLogger();
if (!$this->version) {
$this->root()->version = OA\OpenApi::DEFAULT_VERSION;
}
}
/**
* Ensure this context is part of the context tree.
*/
public function ensureRoot(?Context $rootContext): void
{
if ($rootContext === $this) {
return;
}
if (!$this->parent) {
// use root fallback for these...
foreach (['logger', 'version'] as $property) {
unset($this->{$property});
}
$this->parent = $rootContext;
}
}
/**
* Check if a property is set directly on this context and not its parent context.
*
* Example: $c->is('method') or $c->is('class')
*/
public function is(string $property): bool
{
return property_exists($this, $property);
}
/**
* Check if a property is NOT set directly on this context and its parent context.
*
* Example: $c->not('method') or $c->not('class')
*/
public function not(string $property): bool
{
return $this->is($property) === false;
}
/**
* Return the context containing the specified property.
*/
public function with(string $property): ?Context
{
if ($this->is($property)) {
return $this;
}
if ($this->parent !== null) {
return $this->parent->with($property);
}
return null;
}
/**
* Get the root context.
*/
public function root(): Context
{
if ($this->parent !== null) {
return $this->parent->root();
}
return $this;
}
/**
* Check if one of the given version numbers matches the current OpenAPI version.
*
* @param string|array $versions One or more version numbers
*/
public function isVersion($versions): bool
{
$versions = (array) $versions;
$currentVersion = $this->version ?: OA\OpenApi::DEFAULT_VERSION;
return in_array($currentVersion, $versions);
}
/**
* Export location for debugging.
*
* @return string Example: "file1.php on line 12"
*/
public function getDebugLocation(): string
{
$location = '';
if ($this->class && ($this->method || $this->property)) {
$location .= $this->fullyQualifiedName($this->class);
if ($this->method) {
$location .= ($this->static ? '::' : '->') . $this->method . '()';
} elseif ($this->property) {
$location .= ($this->static ? '::$' : '->') . $this->property;
}
}
if ($this->filename) {
if ($location !== '') {
$location .= ' in ';
}
$location .= $this->filename;
}
if ($this->line) {
if ($location !== '') {
$location .= ' on';
}
$location .= ' line ' . $this->line;
if ($this->character) {
$location .= ':' . $this->character;
}
}
return $location;
}
/**
* Traverse the context tree to get the property value.
*/
public function __get(string $property)
{
if ($this->parent !== null) {
return $this->parent->{$property};
}
return null;
}
public function __toString()
{
return $this->getDebugLocation();
}
public function __debugInfo()
{
return ['-' => $this->getDebugLocation()];
}
/**
* Create a Context based on `debug_backtrace`.
*
* @deprecated
*/
public static function detect(int $index = 0): Context
{
$context = new Context();
$backtrace = debug_backtrace();
$position = $backtrace[$index];
if (isset($position['file'])) {
$context->filename = $position['file'];
}
if (isset($position['line'])) {
$context->line = $position['line'];
}
$caller = $backtrace[$index + 1] ?? null;
if (isset($caller['function'])) {
$context->method = $caller['function'];
if (isset($caller['type']) && $caller['type'] === '::') {
$context->static = true;
}
}
if (isset($caller['class'])) {
$fqn = explode('\\', $caller['class']);
$context->class = array_pop($fqn);
if (count($fqn)) {
$context->namespace = implode('\\', $fqn);
}
}
// @todo extract namespaces and use statements
return $context;
}
/**
* Resolve the fully qualified name.
*/
public function fullyQualifiedName(?string $source): string
{
if ($source === null) {
return '';
}
if ($this->namespace) {
$namespace = str_replace('\\\\', '\\', '\\' . $this->namespace . '\\');
} else {
// global namespace
$namespace = '\\';
}
$thisSource = $this->class ?? $this->interface ?? $this->trait;
if ($thisSource && strcasecmp($source, $thisSource) === 0) {
return $namespace . $thisSource;
}
$pos = strpos($source, '\\');
if ($pos !== false) {
if ($pos === 0) {
// Fully qualified name (\Foo\Bar)
return $source;
}
// Qualified name (Foo\Bar)
if ($this->uses) {
foreach ($this->uses as $alias => $aliasedNamespace) {
$alias .= '\\';
if (strcasecmp(substr($source, 0, strlen($alias)), $alias) === 0) {
// Aliased namespace (use \Long\Namespace as Foo)
return '\\' . $aliasedNamespace . substr($source, strlen($alias) - 1);
}
}
}
} elseif ($this->uses) {
// Unqualified name (Foo)
foreach ($this->uses as $alias => $aliasedNamespace) {
if (strcasecmp($alias, $source) === 0) {
return '\\' . $aliasedNamespace;
}
}
}
return $namespace . $source;
}
}