Repository navigation
Expand file tree
/
Copy pathMethodDefinitionCollector.php
More file actions
90 lines (83 loc) · 3.06 KB
/
Copy pathMethodDefinitionCollector.php
File metadata and controls
90 lines (83 loc) · 3.06 KB
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
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Di;
class MethodDefinitionCollector extends AbstractCallableDefinitionCollector implements MethodDefinitionCollectorInterface
{
protected static array $container = [];
/**
* Get the method definition from metadata container,
* If the metadata not exist in container, then will
* parse it and save into container, and then return it.
*/
public static function getOrParse(string $class, string $method): array
{
$key = $class . '::' . $method;
if (static::has($key)) {
return static::get($key);
}
$parameters = ReflectionManager::reflectMethod($class, $method)->getParameters();
$definitions = [];
foreach ($parameters as $parameter) {
$type = $parameter->getType()->getName();
switch ($type) {
case 'int':
case 'float':
case 'string':
case 'array':
case 'bool':
$definition = [
'type' => $type,
'name' => $parameter->getName(),
'ref' => '',
'allowsNull' => $parameter->allowsNull(),
];
if ($parameter->isDefaultValueAvailable()) {
$definition['defaultValue'] = $parameter->getDefaultValue();
}
$definitions[] = $definition;
break;
default:
// Object
$definitions[] = [
'type' => 'object',
'name' => $parameter->getName(),
'ref' => $type ?? null,
'allowsNull' => $parameter->allowsNull(),
];
break;
}
}
static::set($key, $definitions);
return $definitions;
}
public function getParameters(string $class, string $method): array
{
$key = $class . '::' . $method . '@params';
if (static::has($key)) {
return static::get($key);
}
$parameters = ReflectionManager::reflectClass($class)->getMethod($method)->getParameters();
$definitions = $this->getDefinitionsFromParameters($parameters);
static::set($key, $definitions);
return $definitions;
}
public function getReturnType(string $class, string $method): ReflectionType
{
$key = $class . '::' . $method . '@return';
if (static::has($key)) {
return static::get($key);
}
$returnType = ReflectionManager::reflectClass($class)->getMethod($method)->getReturnType();
$type = $this->createType('', $returnType, $returnType ? $returnType->allowsNull() : true);
static::set($key, $type);
return $type;
}
}