30
30
use Nette \PhpGenerator \Method ;
31
31
use Nette \PhpGenerator \PhpNamespace ;
32
32
use Nette \PhpGenerator \TraitType ;
33
- use RuntimeException ;
33
+ use ReflectionClass ;
34
34
use stdClass ;
35
- use Throwable ;
36
35
37
36
use function array_key_last ;
38
37
use function array_map ;
@@ -98,37 +97,37 @@ private function createFluentFactoryClass(GeneratorDefinition $definition): PhpN
98
97
99
98
$ staticFactory = ClassType::from (self ::FACTORY_CLASS );
100
99
assert ($ staticFactory instanceof ClassType);
100
+
101
+ // Import the methods customized in the factory class
101
102
foreach ($ staticFactory ->getMethods () as $ method ) {
102
103
if (! $ method ->isPublic ()) {
103
104
continue ;
104
105
}
105
106
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
- }
107
+ $ this ->addMethod ($ method , $ class );
111
108
}
112
109
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 );
110
+ // Import the other methods generated in the trait
111
+ foreach ($ staticFactory ->getTraits () as $ trait ) {
112
+ $ staticFactory = TraitType::from ($ trait ->getName ());
113
+ assert ($ staticFactory instanceof TraitType);
114
+ foreach ($ staticFactory ->getMethods () as $ method ) {
115
+ $ this ->addMethod ($ method , $ class );
124
116
}
125
117
}
126
118
127
119
return $ namespace ;
128
120
}
129
121
130
- private function addMethod (Method $ factoryMethod , PhpNamespace $ namespace , ClassType $ class ): void
122
+ private function addMethod (Method $ factoryMethod , ClassType $ class ): void
131
123
{
124
+ // Non-public methods are not part of the API
125
+ if (! $ factoryMethod ->isPublic ()) {
126
+ return ;
127
+ }
128
+
129
+ // Some methods can be overridden in the class, so we skip them
130
+ // when importing the methods provided by the trait.
132
131
if ($ class ->hasMethod ($ factoryMethod ->getName ())) {
133
132
return ;
134
133
}
@@ -155,9 +154,9 @@ private function addMethod(Method $factoryMethod, PhpNamespace $namespace, Class
155
154
156
155
return $this;
157
156
PHP,
158
- ' Stage ' ,
157
+ ( new ReflectionClass ( self :: FACTORY_CLASS ))-> getShortName () ,
159
158
$ factoryMethod ->getName (),
160
- implode (', ' , $ args ),
159
+ implode (', ' , $ args ),
161
160
));
162
161
}
163
162
}
0 commit comments