1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Composite Utils package. |
4
|
|
|
* |
5
|
|
|
* (c) Emily Shepherd <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the |
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @package spaark/composite-utils |
11
|
|
|
* @author Emily Shepherd <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Spaark\CompositeUtils\Factory\Reflection; |
16
|
|
|
|
17
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionComposite; |
18
|
|
|
use Spaark\CompositeUtils\Model\Reflection\ReflectionMethod; |
19
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\BooleanType; |
20
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\CollectionType; |
21
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\IntegerType; |
22
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\MixedType; |
23
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\ObjectType; |
24
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\StringType; |
25
|
|
|
use Spaark\CompositeUtils\Model\Reflection\Type\GenericType; |
26
|
|
|
use Spaark\CompositeUtils\Model\Generic\GenericContext; |
27
|
|
|
use Spaark\CompositeUtils\Model\ClassName; |
28
|
|
|
use Spaark\CompositeUtils\Service\RawPropertyAccessor; |
29
|
|
|
use Spaark\CompositeUtils\Service\GenericNameProvider; |
30
|
|
|
use Spaark\CompositeUtils\Traits\AutoConstructPropertyAccessTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Generates the code for a generic class |
34
|
|
|
*/ |
35
|
|
|
class GenericCompositeGenerator |
36
|
|
|
{ |
37
|
|
|
use AutoConstructPropertyAccessTrait; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ReflectionComposite |
41
|
|
|
* @construct required |
42
|
|
|
*/ |
43
|
|
|
protected $reflect; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var GenericNameProvider |
47
|
|
|
*/ |
48
|
|
|
protected $nameProvider; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var ?ClassName |
52
|
|
|
* @readable |
53
|
|
|
*/ |
54
|
|
|
protected $generatedClassName; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Creates an ObjectType from the given list of generics |
58
|
|
|
* |
59
|
|
|
* @param AbstractType[] $generics |
60
|
|
|
*/ |
61
|
6 |
|
private function createObject(...$generics) : ObjectType |
62
|
|
|
{ |
63
|
6 |
|
$object = new ObjectType($this->reflect->classname); |
64
|
6 |
|
$i = 0; |
65
|
|
|
|
66
|
6 |
|
foreach ($this->reflect->generics as $name => $value) |
|
|
|
|
67
|
|
|
{ |
68
|
6 |
|
$object->generics[] = $generics[$i++] ?? $value; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
return $object; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Generate class code for the given generics |
76
|
|
|
* |
77
|
|
|
* @param AbstractType[] $generics |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
6 |
|
public function generateClassCode(...$generics) : string |
81
|
|
|
{ |
82
|
6 |
|
$object = $this->createObject(...$generics); |
83
|
6 |
|
$this->nameProvider = new GenericNameProvider |
84
|
|
|
( |
85
|
6 |
|
new GenericContext($object, $this->reflect) |
86
|
|
|
); |
87
|
6 |
|
$class = $this->nameProvider->inferName($object); |
88
|
6 |
|
$this->generatedClassName = $class; |
89
|
6 |
|
$originalClass = $this->reflect->classname; |
90
|
6 |
|
$i = 0; |
|
|
|
|
91
|
|
|
|
92
|
|
|
$code = |
93
|
6 |
|
'namespace ' . $class->namespace . ';' |
94
|
6 |
|
. 'class ' . $class->classname . ' ' |
95
|
6 |
|
. 'extends \\' . $originalClass |
96
|
6 |
|
. '{'; |
97
|
|
|
|
98
|
6 |
|
foreach ($this->reflect->methods as $method) |
99
|
|
|
{ |
100
|
6 |
|
$code .= $this->generateMethodCode($method); |
101
|
|
|
} |
102
|
|
|
|
103
|
6 |
|
$code .= '}'; |
104
|
|
|
|
105
|
6 |
|
return $code; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generates the method code for the current class being generated |
110
|
|
|
* |
111
|
|
|
* @param ReflectionMethod $method |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
6 |
|
public function generateMethodCode(ReflectionMethod $method) |
115
|
|
|
: string |
116
|
|
|
{ |
117
|
6 |
|
$params = []; |
118
|
6 |
|
$newParams = []; |
119
|
6 |
|
$paramNames = []; |
120
|
6 |
|
foreach ($method->parameters as $i => $param) |
121
|
|
|
{ |
122
|
6 |
|
$paramNames[] = $name = ' $' . $param->name; |
123
|
6 |
|
$params[] = $method->nativeParameters[$i] . $name; |
|
|
|
|
124
|
6 |
|
$newParams[] = |
125
|
6 |
|
$this->nameProvider->inferName($param->type) . $name; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return |
129
|
6 |
|
($method->scope === 'static' ? 'static ' : '') |
130
|
6 |
|
. 'function ' . $method->name |
131
|
6 |
|
. '(' . implode(',', $params) . '){' |
132
|
6 |
|
. '__generic_' . $method->name |
133
|
6 |
|
. '(' . implode(',', $paramNames) . ');}' |
134
|
|
|
. "\n" |
135
|
6 |
|
. 'function __generic_' . $method->name |
136
|
6 |
|
. '(' . implode(',', $newParams) . '){' |
137
|
6 |
|
. 'parent::' . $method->name |
138
|
6 |
|
. '(' . implode(',', $paramNames) . ');}' |
139
|
6 |
|
. "\n"; |
140
|
|
|
} |
141
|
|
|
|
142
|
4 |
|
public function createClass(...$generics) |
143
|
|
|
{ |
144
|
4 |
|
eval($this->generateClassCode(...$generics)); |
145
|
4 |
|
} |
146
|
|
|
} |
147
|
|
|
|
Since your code implements the magic setter
_set
, this function will be called for any write access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.