5
5
namespace AsyncAws \CodeGenerator \Generator \PhpGenerator ;
6
6
7
7
use Nette \PhpGenerator \ClassType ;
8
- use Nette \PhpGenerator \Closure ;
9
- use Nette \PhpGenerator \GlobalFunction ;
10
- use Nette \PhpGenerator \Helpers ;
11
- use Nette \PhpGenerator \Literal ;
12
- use Nette \PhpGenerator \Method ;
13
- use Nette \PhpGenerator \Parameter ;
8
+ use Nette \PhpGenerator \Factory ;
14
9
use Nette \PhpGenerator \PhpNamespace ;
15
- use Nette \PhpGenerator \Property ;
16
10
17
11
/**
18
12
* Generate Nette PhpNamespace from existing source class.
25
19
*/
26
20
class ClassFactory
27
21
{
22
+ /**
23
+ * @var Factory
24
+ */
25
+ private $ factory ;
26
+
27
+ public function __construct ()
28
+ {
29
+ $ this ->factory = new Factory ();
30
+ }
31
+
28
32
/**
29
33
* @param \ReflectionClass<object> $from
30
34
*/
31
35
public function fromClassReflection (\ReflectionClass $ from ): PhpNamespace
32
36
{
37
+ /** @var ClassType $class */
38
+ $ class = $ this ->factory ->fromClassReflection ($ from , true );
39
+
33
40
$ namespace = new PhpNamespace ($ from ->getNamespaceName ());
34
41
$ filename = $ from ->getFileName ();
35
42
$ rows = file ($ filename );
@@ -46,136 +53,8 @@ public function fromClassReflection(\ReflectionClass $from): PhpNamespace
46
53
}
47
54
}
48
55
49
- $ class = $ from ->isAnonymous ()
50
- ? new ClassType ()
51
- : new ClassType ($ from ->getShortName (), $ namespace );
52
-
53
56
$ namespace ->add ($ class );
54
- $ class ->setType ($ from ->isInterface () ? $ class ::TYPE_INTERFACE : ($ from ->isTrait () ? $ class ::TYPE_TRAIT : $ class ::TYPE_CLASS ));
55
- $ class ->setFinal ($ from ->isFinal () && $ class ->isClass ());
56
- $ class ->setAbstract ($ from ->isAbstract () && $ class ->isClass ());
57
-
58
- $ ifaces = $ from ->getInterfaceNames ();
59
- foreach ($ ifaces as $ iface ) {
60
- $ ifaces = array_filter ($ ifaces , function (string $ item ) use ($ iface ): bool {
61
- return !is_subclass_of ($ iface , $ item );
62
- });
63
- }
64
- $ class ->setImplements ($ ifaces );
65
-
66
- $ class ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
67
- if ($ from ->getParentClass ()) {
68
- $ class ->setExtends ($ from ->getParentClass ()->getName ());
69
- $ class ->setImplements (array_diff ($ class ->getImplements (), $ from ->getParentClass ()->getInterfaceNames ()));
70
- }
71
- $ props = $ methods = [];
72
- foreach ($ from ->getProperties () as $ prop ) {
73
- if ($ prop ->isDefault () && $ prop ->getDeclaringClass ()->getName () === $ from ->getName ()) {
74
- $ props [] = $ this ->fromPropertyReflection ($ prop );
75
- }
76
- }
77
- $ class ->setProperties ($ props );
78
- foreach ($ from ->getMethods () as $ method ) {
79
- if ($ method ->getDeclaringClass ()->getName () === $ from ->getName ()) {
80
- $ methods [] = $ this ->fromMethodReflection ($ method );
81
- }
82
- }
83
- $ class ->setMethods ($ methods );
84
- $ class ->setConstants ($ from ->getConstants ());
85
57
86
58
return $ namespace ;
87
59
}
88
-
89
- public function fromMethodReflection (\ReflectionMethod $ from ): Method
90
- {
91
- $ method = new Method ($ from ->getName ());
92
- $ method ->setParameters (array_map ([$ this , 'fromParameterReflection ' ], $ from ->getParameters ()));
93
- $ method ->setStatic ($ from ->isStatic ());
94
- $ isInterface = $ from ->getDeclaringClass ()->isInterface ();
95
- $ method ->setVisibility (
96
- $ from ->isPrivate ()
97
- ? ClassType::VISIBILITY_PRIVATE
98
- : ($ from ->isProtected () ? ClassType::VISIBILITY_PROTECTED : ($ isInterface ? null : ClassType::VISIBILITY_PUBLIC ))
99
- );
100
- $ method ->setFinal ($ from ->isFinal ());
101
- $ method ->setAbstract ($ from ->isAbstract () && !$ isInterface );
102
-
103
- $ filename = $ from ->getFileName ();
104
- $ rows = file ($ filename );
105
- $ body = implode ('' , array_map (function ($ r ) {
106
- return trim ($ r , ' ' );
107
- }, \array_slice ($ rows , $ from ->getStartLine () + 1 , $ from ->getEndLine () - $ from ->getStartLine () - 2 )));
108
-
109
- $ method ->setBody ($ from ->isAbstract () ? null : print_r ($ body , true ));
110
- $ method ->setReturnReference ($ from ->returnsReference ());
111
- $ method ->setVariadic ($ from ->isVariadic ());
112
- $ method ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
113
- if ($ from ->hasReturnType () && ($ type = $ from ->getReturnType ()) instanceof \ReflectionNamedType) {
114
- $ method ->setReturnType ($ type ->getName ());
115
- $ method ->setReturnNullable ($ type ->allowsNull ());
116
- }
117
-
118
- return $ method ;
119
- }
120
-
121
- /**
122
- * @return GlobalFunction|Closure
123
- */
124
- public function fromFunctionReflection (\ReflectionFunction $ from )
125
- {
126
- $ function = $ from ->isClosure () ? new Closure () : new GlobalFunction ($ from ->getName ());
127
- $ function ->setParameters (array_map ([$ this , 'fromParameterReflection ' ], $ from ->getParameters ()));
128
- $ function ->setReturnReference ($ from ->returnsReference ());
129
- $ function ->setVariadic ($ from ->isVariadic ());
130
- if (!$ from ->isClosure ()) {
131
- $ function ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
132
- }
133
- if ($ from ->hasReturnType () && ($ type = $ from ->getReturnType ()) instanceof \ReflectionNamedType) {
134
- $ function ->setReturnType ($ type ->getName ());
135
- $ function ->setReturnNullable ($ type ->allowsNull ());
136
- }
137
-
138
- return $ function ;
139
- }
140
-
141
- public function fromParameterReflection (\ReflectionParameter $ from ): Parameter
142
- {
143
- $ param = new Parameter ($ from ->getName ());
144
- $ param ->setReference ($ from ->isPassedByReference ());
145
- if ($ from ->hasType () && ($ type = $ from ->getType ()) instanceof \ReflectionNamedType) {
146
- $ param ->setType ($ type ->getName ());
147
- $ param ->setNullable ($ type ->allowsNull ());
148
- }
149
- if ($ from ->isDefaultValueAvailable ()) {
150
- $ param ->setDefaultValue ($ from ->isDefaultValueConstant ()
151
- ? new Literal ($ from ->getDefaultValueConstantName ())
152
- : $ from ->getDefaultValue ());
153
- $ param ->setNullable ($ param ->isNullable () && null !== $ param ->getDefaultValue ());
154
- }
155
-
156
- return $ param ;
157
- }
158
-
159
- public function fromPropertyReflection (\ReflectionProperty $ from ): Property
160
- {
161
- $ defaults = $ from ->getDeclaringClass ()->getDefaultProperties ();
162
- $ prop = new Property ($ from ->getName ());
163
- if (isset ($ defaults [$ prop ->getName ()])) {
164
- $ prop ->setValue ($ defaults [$ prop ->getName ()]);
165
- }
166
- $ prop ->setStatic ($ from ->isStatic ());
167
- $ prop ->setVisibility (
168
- $ from ->isPrivate ()
169
- ? ClassType::VISIBILITY_PRIVATE
170
- : ($ from ->isProtected () ? ClassType::VISIBILITY_PROTECTED : ClassType::VISIBILITY_PUBLIC )
171
- );
172
- if (\PHP_VERSION_ID >= 70400 && ($ type = $ from ->getType ()) instanceof \ReflectionNamedType) {
173
- $ prop ->setType ($ type ->getName ());
174
- $ prop ->setNullable ($ type ->allowsNull ());
175
- $ prop ->setInitialized (\array_key_exists ($ prop ->getName (), $ defaults ));
176
- }
177
- $ prop ->setComment (Helpers::unformatDocComment ((string ) $ from ->getDocComment ()));
178
-
179
- return $ prop ;
180
- }
181
60
}
0 commit comments