-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
PhpNamespace.php
346 lines (264 loc) · 8.55 KB
/
PhpNamespace.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\PhpGenerator;
use Nette;
use Nette\InvalidStateException;
/**
* Namespaced part of a PHP file.
*
* Generates:
* - namespace statement
* - variable amount of use statements
* - one or more class declarations
*/
final class PhpNamespace
{
public const
NameNormal = 'n',
NameFunction = 'f',
NameConstant = 'c';
/** @deprecated use PhpNamespace::NameNormal */
public const NAME_NORMAL = self::NameNormal;
/** @deprecated use PhpNamespace::NameFunction */
public const NAME_FUNCTION = self::NameFunction;
/** @deprecated use PhpNamespace::NameConstant */
public const NAME_CONSTANT = self::NameConstant;
private string $name;
private bool $bracketedSyntax = false;
/** @var string[][] */
private array $aliases = [
self::NameNormal => [],
self::NameFunction => [],
self::NameConstant => [],
];
/** @var (ClassType|InterfaceType|TraitType|EnumType)[] */
private array $classes = [];
/** @var GlobalFunction[] */
private array $functions = [];
public function __construct(string $name)
{
if ($name !== '' && !Helpers::isNamespaceIdentifier($name)) {
throw new Nette\InvalidArgumentException("Value '$name' is not valid name.");
}
$this->name = $name;
}
public function getName(): string
{
return $this->name;
}
/**
* @internal
*/
public function setBracketedSyntax(bool $state = true): static
{
$this->bracketedSyntax = $state;
return $this;
}
public function hasBracketedSyntax(): bool
{
return $this->bracketedSyntax;
}
/**
* @throws InvalidStateException
*/
public function addUse(string $name, ?string $alias = null, string $of = self::NameNormal): static
{
if (
!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)
|| (Helpers::isIdentifier($name) && isset(Helpers::Keywords[strtolower($name)]))
) {
throw new Nette\InvalidArgumentException("Value '$name' is not valid class/function/constant name.");
} elseif ($alias && (!Helpers::isIdentifier($alias) || isset(Helpers::Keywords[strtolower($alias)]))) {
throw new Nette\InvalidArgumentException("Value '$alias' is not valid alias.");
}
$name = ltrim($name, '\\');
$aliases = array_change_key_case($this->aliases[$of]);
$used = [self::NameNormal => $this->classes, self::NameFunction => $this->functions, self::NameConstant => []][$of];
if ($alias === null) {
$base = Helpers::extractShortName($name);
$counter = null;
do {
$alias = $base . $counter;
$lower = strtolower($alias);
$counter++;
} while ((isset($aliases[$lower]) && strcasecmp($aliases[$lower], $name) !== 0) || isset($used[$lower]));
} else {
$lower = strtolower($alias);
if (isset($aliases[$lower]) && strcasecmp($aliases[$lower], $name) !== 0) {
throw new InvalidStateException(
"Alias '$alias' used already for '{$aliases[$lower]}', cannot use for '$name'.",
);
} elseif (isset($used[$lower])) {
throw new Nette\InvalidStateException("Name '$alias' used already for '$this->name\\{$used[$lower]->getName()}'.");
}
}
$this->aliases[$of][$alias] = $name;
return $this;
}
public function removeUse(string $name, string $of = self::NameNormal): void
{
foreach ($this->aliases[$of] as $alias => $item) {
if (strcasecmp($item, $name) === 0) {
unset($this->aliases[$of][$alias]);
}
}
}
public function addUseFunction(string $name, ?string $alias = null): static
{
return $this->addUse($name, $alias, self::NameFunction);
}
public function addUseConstant(string $name, ?string $alias = null): static
{
return $this->addUse($name, $alias, self::NameConstant);
}
/** @return string[] */
public function getUses(string $of = self::NameNormal): array
{
uasort($this->aliases[$of], fn(string $a, string $b): int => strtr($a, '\\', ' ') <=> strtr($b, '\\', ' '));
return array_filter(
$this->aliases[$of],
fn($name, $alias) => strcasecmp(($this->name ? $this->name . '\\' : '') . $alias, $name),
ARRAY_FILTER_USE_BOTH,
);
}
public function resolveName(string $name, string $of = self::NameNormal): string
{
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
return $name;
} elseif ($name[0] === '\\') {
return substr($name, 1);
}
$aliases = array_change_key_case($this->aliases[$of]);
if ($of !== self::NameNormal) {
return $aliases[strtolower($name)]
?? $this->resolveName(Helpers::extractNamespace($name) . '\\') . Helpers::extractShortName($name);
}
$parts = explode('\\', $name, 2);
return ($res = $aliases[strtolower($parts[0])] ?? null)
? $res . (isset($parts[1]) ? '\\' . $parts[1] : '')
: $this->name . ($this->name ? '\\' : '') . $name;
}
public function simplifyType(string $type, string $of = self::NameNormal): string
{
return preg_replace_callback('~[\w\x7f-\xff\\\\]+~', fn($m) => $this->simplifyName($m[0], $of), $type);
}
public function simplifyName(string $name, string $of = self::NameNormal): string
{
if (isset(Helpers::Keywords[strtolower($name)]) || $name === '') {
return $name;
}
$name = ltrim($name, '\\');
if ($of !== self::NameNormal) {
foreach ($this->aliases[$of] as $alias => $original) {
if (strcasecmp($original, $name) === 0) {
return $alias;
}
}
return $this->simplifyName(Helpers::extractNamespace($name) . '\\') . Helpers::extractShortName($name);
}
$shortest = null;
$relative = self::startsWith($name, $this->name . '\\')
? substr($name, strlen($this->name) + 1)
: null;
foreach ($this->aliases[$of] as $alias => $original) {
if ($relative && self::startsWith($relative . '\\', $alias . '\\')) {
$relative = null;
}
if (self::startsWith($name . '\\', $original . '\\')) {
$short = $alias . substr($name, strlen($original));
if (!isset($shortest) || strlen($shortest) > strlen($short)) {
$shortest = $short;
}
}
}
if (isset($shortest, $relative) && strlen($shortest) < strlen($relative)) {
return $shortest;
}
return $relative ?? $shortest ?? ($this->name ? '\\' : '') . $name;
}
public function add(ClassType|InterfaceType|TraitType|EnumType $class): static
{
$name = $class->getName();
if ($name === null) {
throw new Nette\InvalidArgumentException('Class does not have a name.');
}
$lower = strtolower($name);
if (isset($this->classes[$lower]) && $this->classes[$lower] !== $class) {
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
} elseif ($orig = array_change_key_case($this->aliases[self::NameNormal])[$lower] ?? null) {
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
}
$this->classes[$lower] = $class;
return $this;
}
public function addClass(string $name): ClassType
{
$this->add($class = new ClassType($name, $this));
return $class;
}
public function addInterface(string $name): InterfaceType
{
$this->add($iface = new InterfaceType($name, $this));
return $iface;
}
public function addTrait(string $name): TraitType
{
$this->add($trait = new TraitType($name, $this));
return $trait;
}
public function addEnum(string $name): EnumType
{
$this->add($enum = new EnumType($name, $this));
return $enum;
}
public function removeClass(string $name): static
{
unset($this->classes[strtolower($name)]);
return $this;
}
public function addFunction(string $name): GlobalFunction
{
$lower = strtolower($name);
if (isset($this->functions[$lower])) {
throw new Nette\InvalidStateException("Cannot add '$name', because it already exists.");
} elseif ($orig = array_change_key_case($this->aliases[self::NameFunction])[$lower] ?? null) {
throw new Nette\InvalidStateException("Name '$name' used already as alias for $orig.");
}
return $this->functions[$lower] = new GlobalFunction($name);
}
public function removeFunction(string $name): static
{
unset($this->functions[strtolower($name)]);
return $this;
}
/** @return (ClassType|InterfaceType|TraitType|EnumType)[] */
public function getClasses(): array
{
$res = [];
foreach ($this->classes as $class) {
$res[$class->getName()] = $class;
}
return $res;
}
/** @return GlobalFunction[] */
public function getFunctions(): array
{
$res = [];
foreach ($this->functions as $fn) {
$res[$fn->getName()] = $fn;
}
return $res;
}
private static function startsWith(string $a, string $b): bool
{
return strncasecmp($a, $b, strlen($b)) === 0;
}
public function __toString(): string
{
return (new Printer)->printNamespace($this);
}
}