-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExtensionDefinition.php
106 lines (92 loc) · 3.5 KB
/
ExtensionDefinition.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
<?php
namespace TheCodingMachine\Funky;
use ReflectionMethod;
use TheCodingMachine\Funky\Annotations\Extension;
use TheCodingMachine\Funky\Injections\Injection;
class ExtensionDefinition extends AbstractDefinition
{
public function __construct(ReflectionMethod $reflectionMethod, Extension $annotation)
{
if (!$reflectionMethod->isPublic()) {
throw BadModifierException::mustBePublic($reflectionMethod, '@Extension');
}
if (!$reflectionMethod->isStatic()) {
throw BadModifierException::mustBeStatic($reflectionMethod, '@Extension');
}
if ($annotation->isFromMethodName()) {
$this->name = $reflectionMethod->getName();
} elseif ($annotation->isFromType()) {
$returnType = $reflectionMethod->getReturnType();
if ($returnType === null) {
throw UnknownTypeException::create($reflectionMethod);
}
$this->name = (string) $returnType;
} else {
$this->name = (string) $annotation->getName();
}
$this->tags = $annotation->getTags();
parent::__construct($reflectionMethod);
}
public function buildExtensionCode(string $functionName) : string
{
$returnTypeCode = '';
$returnType = $this->reflectionMethod->getReturnType();
if ($returnType) {
if ($returnType->isBuiltin()) {
$returnTypeCode = ': '.$this->reflectionMethod->getReturnType();
} else {
$returnTypeCode = ': \\'.$this->reflectionMethod->getReturnType();
}
}
$parameters = $this->reflectionMethod->getParameters();
$previousParameterCode = '';
$previousTypeCode = '';
if (count($parameters) >= 1) {
$previousParameter = $this->reflectionMethod->getParameters()[0];
$previousType = $previousParameter->getType();
if ($previousType) {
if ($previousType->isBuiltin()) {
$previousTypeCode = (string) $previousType.' ';
} else {
$previousTypeCode = '\\'.(string) $previousType.' ';
}
if ($previousParameter->allowsNull()) {
$previousTypeCode = '?'.$previousTypeCode;
}
}
$previousParameterCode = ', '.$previousTypeCode.'$previous';
if ($previousParameter->isDefaultValueAvailable()) {
$previousParameterCode .= ' = '.var_export($previousParameter->getDefaultValue(), true);
}
}
return sprintf(
<<<EOF
public static function %s(ContainerInterface \$container%s)%s
{
return %s::%s(%s%s);
}
EOF
,
$functionName,
$previousParameterCode,
$returnTypeCode,
'\\'.$this->reflectionMethod->getDeclaringClass()->getName(),
$this->reflectionMethod->getName(),
$previousParameterCode ? '$previous': '',
implode('', array_map(function (Injection $injection) {
return ', '.$injection->getCode();
}, $this->getInjections()))
);
}
/**
* Returns a list of services to be injected.
*
* @return Injection[]
*/
private function getInjections(): array
{
$parameters = $this->getReflectionMethod()->getParameters();
array_shift($parameters);
return array_map([$this, 'mapParameterToInjection'], $parameters);
}
}