-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathComponentReflection.php
226 lines (187 loc) · 6.1 KB
/
ComponentReflection.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
<?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\Application\UI;
use Nette\Application\Attributes;
use Nette\Utils\Reflection;
/**
* Helpers for Presenter & Component.
* @property-deprecated string $name
* @property-deprecated string $fileName
* @internal
*/
final class ComponentReflection extends \ReflectionClass
{
private static array $ppCache = [];
private static array $pcCache = [];
private static array $armCache = [];
/**
* Returns array of class properties that are public and have attribute #[Persistent] or #[Parameter].
* @return array<string, array{def: mixed, type: string, since: ?string}>
*/
public function getParameters(): array
{
$params = &self::$ppCache[$this->getName()];
if ($params !== null) {
return $params;
}
$params = [];
$isPresenter = $this->isSubclassOf(Presenter::class);
foreach ($this->getProperties(\ReflectionProperty::IS_PUBLIC) as $prop) {
if ($prop->isStatic()) {
continue;
} elseif (
self::parseAnnotation($prop, 'persistent')
|| $prop->getAttributes(Attributes\Persistent::class)
) {
$params[$prop->getName()] = [
'def' => $prop->getDefaultValue(),
'type' => ParameterConverter::getType($prop),
'since' => $isPresenter ? Reflection::getPropertyDeclaringClass($prop)->getName() : null,
];
} elseif ($prop->getAttributes(Attributes\Parameter::class)) {
$params[$prop->getName()] = [
'type' => (string) ($prop->getType() ?? 'mixed'),
];
}
}
if ($this->getParentClass()->isSubclassOf(Component::class)) {
$parent = new self($this->getParentClass()->getName());
foreach ($parent->getParameters() as $name => $meta) {
if (!isset($params[$name])) {
$params[$name] = $meta;
} elseif (array_key_exists('since', $params[$name])) {
$params[$name]['since'] = $meta['since'];
}
}
}
return $params;
}
/**
* Returns array of persistent properties. They are public and have attribute #[Persistent].
* @return array<string, array{def: mixed, type: string, since: string}>
*/
public function getPersistentParams(): array
{
return array_filter($this->getParameters(), fn($param) => array_key_exists('since', $param));
}
/**
* Returns array of persistent components. They are tagged with class-level attribute
* #[Persistent] or annotation @persistent or returned by Presenter::getPersistentComponents().
* @return array<string, array{since: string}>
*/
public function getPersistentComponents(): array
{
$class = $this->getName();
$components = &self::$pcCache[$class];
if ($components !== null) {
return $components;
}
$attrs = $this->getAttributes(Attributes\Persistent::class);
$names = $attrs
? $attrs[0]->getArguments()
: (array) self::parseAnnotation($this, 'persistent');
$names = array_merge($names, $class::getPersistentComponents());
$components = array_fill_keys($names, ['since' => $class]);
if ($this->isSubclassOf(Presenter::class)) {
$parent = new self($this->getParentClass()->getName());
$components = $parent->getPersistentComponents() + $components;
}
return $components;
}
/**
* Is a method callable? It means class is instantiable and method has
* public visibility, is non-static and non-abstract.
*/
public function hasCallableMethod(string $method): bool
{
return $this->isInstantiable()
&& $this->hasMethod($method)
&& ($rm = $this->getMethod($method))
&& $rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic();
}
/** Returns action*() or render*() method if available */
public function getActionRenderMethod(string $action): ?\ReflectionMethod
{
$class = $this->name;
return self::$armCache[$class][$action] ??=
$this->hasCallableMethod($name = $class::formatActionMethod($action))
|| $this->hasCallableMethod($name = $class::formatRenderMethod($action))
? parent::getMethod($name)
: null;
}
/** Returns handle*() method if available */
public function getSignalMethod(string $signal): ?\ReflectionMethod
{
$class = $this->name;
return $this->hasCallableMethod($name = $class::formatSignalMethod($signal))
? parent::getMethod($name)
: null;
}
/**
* Returns an annotation value.
* @deprecated
*/
public static function parseAnnotation(\Reflector $ref, string $name): ?array
{
if (!preg_match_all('#[\s*]@' . preg_quote($name, '#') . '(?:\(\s*([^)]*)\s*\)|\s|$)#', (string) $ref->getDocComment(), $m)) {
return null;
}
$tokens = ['true' => true, 'false' => false, 'null' => null];
$res = [];
foreach ($m[1] as $s) {
foreach (preg_split('#\s*,\s*#', $s, -1, PREG_SPLIT_NO_EMPTY) ?: ['true'] as $item) {
$res[] = array_key_exists($tmp = strtolower($item), $tokens)
? $tokens[$tmp]
: $item;
}
}
$alt = match ($name) {
'persistent' => '#[Nette\Application\Attributes\Persistent]',
'deprecated' => '#[Nette\Application\Attributes\Deprecated]',
'crossOrigin' => '#[Nette\Application\Attributes\Request(sameOrigin: false)]',
default => 'alternative'
};
trigger_error("Annotation @$name is deprecated, use $alt (used in " . Nette\Utils\Reflection::toString($ref) . ')', E_USER_DEPRECATED);
return $res;
}
/**
* Has class specified annotation?
* @deprecated
*/
public function hasAnnotation(string $name): bool
{
return (bool) self::parseAnnotation($this, $name);
}
/**
* Returns an annotation value.
* @deprecated
*/
public function getAnnotation(string $name): mixed
{
$res = self::parseAnnotation($this, $name);
return $res ? end($res) : null;
}
public function getMethod($name): MethodReflection
{
return new MethodReflection($this->getName(), $name);
}
/**
* @return MethodReflection[]
*/
public function getMethods($filter = -1): array
{
foreach ($res = parent::getMethods($filter) as $key => $val) {
$res[$key] = new MethodReflection($this->getName(), $val->getName());
}
return $res;
}
/** @deprecated */
public static function combineArgs(\ReflectionFunctionAbstract $method, array $args): array
{
return ParameterConverter::toArguments($method, $args);
}
}