-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathHelpers.php
246 lines (214 loc) · 7.3 KB
/
Helpers.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
<?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\DI;
use Nette;
use Nette\PhpGenerator\PhpLiteral;
use Nette\Utils\Reflection;
/**
* The DI helpers.
* @internal
*/
final class Helpers
{
use Nette\StaticClass;
/**
* Expands %placeholders%.
* @param mixed
* @param bool|array $recursive
* @return mixed
* @throws Nette\InvalidArgumentException
*/
public static function expand($var, array $params, $recursive = false)
{
if (is_array($var)) {
$res = [];
foreach ($var as $key => $val) {
if (is_int($key) && is_string($val) && preg_match('#^\.\.\.(%[\w.-]*%)\z#i', $val, $matches)) {
$res = array_merge($res, self::expand($matches[1], $params, $recursive));
} else {
$res[$key] = self::expand($val, $params, $recursive);
}
}
return $res;
} elseif ($var instanceof Statement) {
return new Statement(self::expand($var->getEntity(), $params, $recursive), self::expand($var->arguments, $params, $recursive));
} elseif (!is_string($var)) {
return $var;
}
$parts = preg_split('#%([\w.-]*)%#i', $var, -1, PREG_SPLIT_DELIM_CAPTURE);
$res = [];
$php = false;
foreach ($parts as $n => $part) {
if ($n % 2 === 0) {
$res[] = $part;
} elseif ($part === '') {
$res[] = '%';
} elseif (isset($recursive[$part])) {
throw new Nette\InvalidArgumentException(sprintf('Circular reference detected for variables: %s.', implode(', ', array_keys($recursive))));
} else {
try {
$val = Nette\Utils\Arrays::get($params, explode('.', $part));
} catch (Nette\InvalidArgumentException $e) {
throw new Nette\InvalidArgumentException("Missing parameter '$part'.", 0, $e);
}
if ($recursive) {
$val = self::expand($val, $params, (is_array($recursive) ? $recursive : []) + [$part => 1]);
}
if (strlen($part) + 2 === strlen($var)) {
return $val;
}
if ($val instanceof PhpLiteral) {
$php = true;
} elseif (!is_scalar($val)) {
throw new Nette\InvalidArgumentException("Unable to concatenate non-scalar parameter '$part' into '$var'.");
}
$res[] = $val;
}
}
if ($php) {
$res = array_filter($res, function ($val) { return $val !== ''; });
$res = array_map(function ($val) { return $val instanceof PhpLiteral ? "($val)" : var_export((string) $val, true); }, $res);
return new PhpLiteral(implode(' . ', $res));
}
return implode('', $res);
}
/**
* Generates list of arguments using autowiring.
*/
public static function autowireArguments(\ReflectionFunctionAbstract $method, array $arguments, $container): array
{
$optCount = 0;
$num = -1;
$res = [];
$methodName = Reflection::toString($method) . '()';
foreach ($method->getParameters() as $num => $parameter) {
if (!$parameter->isVariadic() && array_key_exists($parameter->getName(), $arguments)) {
$res[$num] = $arguments[$parameter->getName()];
unset($arguments[$parameter->getName()], $arguments[$num]);
$optCount = 0;
} elseif (array_key_exists($num, $arguments)) {
$res[$num] = $arguments[$num];
unset($arguments[$num]);
$optCount = 0;
} elseif (($class = Reflection::getParameterType($parameter)) && !Reflection::isBuiltinType($class)) {
$res[$num] = $container->getByType($class, false);
if ($res[$num] === null) {
if ($parameter->allowsNull()) {
$optCount++;
} elseif (class_exists($class) || interface_exists($class)) {
throw new ServiceCreationException("Service of type $class needed by $methodName not found. Did you register it in configuration file?");
} else {
throw new ServiceCreationException("Class $class needed by $methodName not found. Check type hint and 'use' statements.");
}
} else {
if ($container instanceof ContainerBuilder) {
$res[$num] = '@' . $res[$num];
}
$optCount = 0;
}
} elseif (($class && $parameter->allowsNull()) || $parameter->isOptional() || $parameter->isDefaultValueAvailable()) {
// !optional + defaultAvailable = func($a = null, $b) since 5.4.7
// optional + !defaultAvailable = i.e. Exception::__construct, mysqli::mysqli, ...
$res[$num] = $parameter->isDefaultValueAvailable() ? Reflection::getParameterDefaultValue($parameter) : null;
$optCount++;
} else {
throw new ServiceCreationException("Parameter \${$parameter->getName()} in $methodName has no class type hint or default value, so its value must be specified.");
}
}
// extra parameters
while (array_key_exists(++$num, $arguments)) {
$res[$num] = $arguments[$num];
unset($arguments[$num]);
$optCount = 0;
}
if ($arguments) {
throw new ServiceCreationException("Unable to pass specified arguments to $methodName.");
}
return $optCount ? array_slice($res, 0, -$optCount) : $res;
}
/**
* Removes ... and process constants recursively.
*/
public static function filterArguments(array $args): array
{
foreach ($args as $k => $v) {
if ($v === '...') {
unset($args[$k]);
} elseif (is_string($v) && preg_match('#^[\w\\\\]*::[A-Z][A-Z0-9_]*\z#', $v, $m)) {
$args[$k] = constant(ltrim($v, ':'));
} elseif (is_array($v)) {
$args[$k] = self::filterArguments($v);
} elseif ($v instanceof Statement) {
$tmp = self::filterArguments([$v->getEntity()]);
$args[$k] = new Statement($tmp[0], self::filterArguments($v->arguments));
}
}
return $args;
}
/**
* Replaces @extension with real extension name in service definition.
* @param mixed
* @return mixed
*/
public static function prefixServiceName($config, string $namespace)
{
if (is_string($config)) {
if (strncmp($config, '@extension.', 10) === 0) {
$config = '@' . $namespace . '.' . substr($config, 11);
}
} elseif ($config instanceof Statement) {
return new Statement(
self::prefixServiceName($config->getEntity(), $namespace),
self::prefixServiceName($config->arguments, $namespace)
);
} elseif (is_array($config)) {
foreach ($config as &$val) {
$val = self::prefixServiceName($val, $namespace);
}
}
return $config;
}
/**
* Returns an annotation value.
* @return string|null
*/
public static function parseAnnotation(\Reflector $ref, $name)
{
if (!Reflection::areCommentsAvailable()) {
throw new Nette\InvalidStateException('You have to enable phpDoc comments in opcode cache.');
}
$name = preg_quote($name, '#');
if ($ref->getDocComment() && preg_match("#[\\s*]@$name(?:\\s++([^@]\\S*)?|$)#", trim($ref->getDocComment(), '/*'), $m)) {
return $m[1] ?? '';
}
}
/**
* @return string|null
*/
public static function getReturnType(\ReflectionFunctionAbstract $func)
{
if ($type = Reflection::getReturnType($func)) {
return $type;
} elseif ($type = preg_replace('#[|\s].*#', '', (string) self::parseAnnotation($func, 'return'))) {
if ($type === 'object' || $type === 'mixed') {
return null;
} elseif ($func instanceof \ReflectionMethod) {
return $type === 'static' || $type === '$this'
? $func->getDeclaringClass()->getName()
: Reflection::expandClassName($type, $func->getDeclaringClass());
} else {
return $type;
}
}
}
public static function normalizeClass(string $class): string
{
return class_exists($class) || interface_exists($class)
? (new \ReflectionClass($class))->getName()
: $class;
}
}