28
28
use MongoDB \Model \BSONArray ;
29
29
use Nette \PhpGenerator \ClassType ;
30
30
use Nette \PhpGenerator \Method ;
31
+ use Nette \PhpGenerator \Parameter ;
31
32
use Nette \PhpGenerator \PhpNamespace ;
32
33
use Nette \PhpGenerator \TraitType ;
33
- use RuntimeException ;
34
+ use ReflectionClass ;
34
35
use stdClass ;
35
- use Throwable ;
36
36
37
37
use function array_key_last ;
38
38
use function array_map ;
@@ -98,37 +98,37 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
98
98
99
99
$ staticFactory = ClassType::from (self ::FACTORY_CLASS );
100
100
assert ($ staticFactory instanceof ClassType);
101
+
102
+ // Import the methods customized in the factory class
101
103
foreach ($ staticFactory ->getMethods () as $ method ) {
102
104
if (! $ method ->isPublic ()) {
103
105
continue ;
104
106
}
105
107
106
- try {
107
- $ this ->addMethod ($ method , $ namespace , $ class );
108
- } catch (Throwable $ e ) {
109
- throw new RuntimeException (sprintf ('Failed to generate class for operator "%s" ' , $ operator ->name ), 0 , $ e );
110
- }
108
+ $ this ->addMethod ($ method , $ class );
111
109
}
112
110
113
- $ staticFactory = TraitType::from (Stage \FactoryTrait::class);
114
- assert ($ staticFactory instanceof TraitType);
115
- foreach ($ staticFactory ->getMethods () as $ method ) {
116
- if (! $ method ->isPublic ()) {
117
- continue ;
118
- }
119
-
120
- try {
121
- $ this ->addMethod ($ method , $ namespace , $ class );
122
- } catch (Throwable $ e ) {
123
- throw new RuntimeException (sprintf ('Failed to generate class for operator "%s" ' , $ operator ->name ), 0 , $ e );
111
+ // Import the other methods generated in the trait
112
+ foreach ($ staticFactory ->getTraits () as $ trait ) {
113
+ $ staticFactory = TraitType::from ($ trait ->getName ());
114
+ assert ($ staticFactory instanceof TraitType);
115
+ foreach ($ staticFactory ->getMethods () as $ method ) {
116
+ $ this ->addMethod ($ method , $ class );
124
117
}
125
118
}
126
119
127
120
return $ namespace ;
128
121
}
129
122
130
- private function addMethod (Method $ factoryMethod , PhpNamespace $ namespace , ClassType $ class ): void
123
+ private function addMethod (Method $ factoryMethod , ClassType $ class ): void
131
124
{
125
+ // Non-public methods are not part of the API
126
+ if (! $ factoryMethod ->isPublic ()) {
127
+ return ;
128
+ }
129
+
130
+ // Some methods can be overridden in the class, so we skip them
131
+ // when importing the methods provided by the trait.
132
132
if ($ class ->hasMethod ($ factoryMethod ->getName ())) {
133
133
return ;
134
134
}
@@ -139,7 +139,7 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
139
139
$ method ->setParameters ($ factoryMethod ->getParameters ());
140
140
141
141
$ args = array_map (
142
- fn ($ param ) => '$ ' . $ param ->getName (),
142
+ fn (Parameter $ param ): string => '$ ' . $ param ->getName (),
143
143
$ factoryMethod ->getParameters (),
144
144
);
145
145
@@ -155,9 +155,9 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
155
155
156
156
return $this;
157
157
PHP,
158
- ' Stage ' ,
158
+ ( new ReflectionClass ( self :: FACTORY_CLASS ))-> getShortName () ,
159
159
$ factoryMethod ->getName (),
160
- implode (', ' , $ args ),
160
+ implode (', ' , $ args ),
161
161
));
162
162
}
163
163
}
0 commit comments