-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathAccessorDefinition.php
More file actions
140 lines (112 loc) · 3.38 KB
/
AccessorDefinition.php
File metadata and controls
140 lines (112 loc) · 3.38 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
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
<?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\Definitions;
use Nette;
use Nette\DI\Helpers;
use Nette\Utils\Type;
/**
* Accessor definition.
*/
final class AccessorDefinition extends Definition
{
private const METHOD_GET = 'get';
/** @var Reference|null */
private $reference;
/** @return static */
public function setImplement(string $interface)
{
if (!interface_exists($interface)) {
throw new Nette\InvalidArgumentException(sprintf(
"[%s]\nInterface '%s' not found.",
$this->getDescriptor(),
$interface
));
}
$rc = new \ReflectionClass($interface);
$method = $rc->getMethods()[0] ?? null;
if (
!$method
|| $method->isStatic()
|| $method->getName() !== self::METHOD_GET
|| count($rc->getMethods()) > 1
) {
throw new Nette\InvalidArgumentException(sprintf(
"[%s]\nInterface %s must have just one non-static method get().",
$this->getDescriptor(),
$interface
));
} elseif ($method->getNumberOfParameters()) {
throw new Nette\InvalidArgumentException(sprintf(
"[%s]\nMethod %s::get() must have no parameters.",
$this->getDescriptor(),
$interface
));
}
try {
Helpers::ensureClassType(Type::fromReflection($method), "return type of $interface::get()", $this->getDescriptor());
} catch (Nette\DI\ServiceCreationException $e) {
trigger_error($e->getMessage(), E_USER_DEPRECATED);
}
return parent::setType($interface);
}
public function getImplement(): ?string
{
return $this->getType();
}
/**
* @param string|Reference $reference
* @return static
*/
public function setReference($reference)
{
if ($reference instanceof Reference) {
$this->reference = $reference;
} else {
$this->reference = substr($reference, 0, 1) === '@'
? new Reference(substr($reference, 1))
: Reference::fromType($reference);
}
return $this;
}
public function getReference(): ?Reference
{
return $this->reference;
}
public function resolveType(Nette\DI\Resolver $resolver): void
{
}
public function complete(Nette\DI\Resolver $resolver): void
{
if (!$this->reference) {
$interface = $this->getType();
$method = new \ReflectionMethod($interface, self::METHOD_GET);
$type = Type::fromReflection($method) ?? Helpers::getReturnTypeAnnotation($method);
$this->setReference(Helpers::ensureClassType($type, "return type of $interface::get()"));
}
$this->reference = $resolver->normalizeReference($this->reference);
}
public function generateMethod(Nette\PhpGenerator\Method $method, Nette\DI\PhpGenerator $generator): void
{
$class = (new Nette\PhpGenerator\ClassType)
->addImplement($this->getType());
$containerType = $generator->getClassName();
$container = $class->addProperty('container')
->setPrivate();
if (PHP_VERSION_ID >= 74000) {
$container->setType($containerType);
}
$class->addMethod('__construct')
->addBody('$this->container = $container;')
->addParameter('container')
->setType($containerType);
$rm = new \ReflectionMethod($this->getType(), self::METHOD_GET);
$class->addMethod(self::METHOD_GET)
->setBody('return $this->container->getService(?);', [$this->reference->getValue()])
->setReturnType((string) Type::fromReflection($rm));
$method->setBody('return new class ($this) ' . $class . ';');
}
}