@@ -90,6 +90,18 @@ public function generate():CodeFiles
90
90
$ classFileGenerator ->generate ()
91
91
));
92
92
}
93
+
94
+ foreach ($ action ->modulesList as $ moduleId => $ moduleDetail ) {
95
+ // only generate Module.php file if they do not exist, do not override
96
+ if (!file_exists (Yii::getAlias ($ moduleDetail ['path ' ] . "/Module.php " ))) {
97
+ // $moduleNamespace = str_replace('\controllers', '', $action->prefixSettings['namespace']);
98
+ $ moduleFileGenerator = $ this ->makeModuleFile ('Module ' , $ moduleDetail ['namespace ' ], $ moduleId , $ action );
99
+ $ this ->files ->add (new CodeFile (
100
+ Yii::getAlias ($ moduleDetail ['path ' ] . "/Module.php " ),
101
+ $ moduleFileGenerator ->generate ()
102
+ ));
103
+ }
104
+ }
93
105
}
94
106
return $ this ->files ;
95
107
}
@@ -160,4 +172,38 @@ protected function makeCustomController(
160
172
$ classFileGenerator ->setClasses ([$ reflection ]);
161
173
return $ classFileGenerator ;
162
174
}
175
+
176
+ /**
177
+ * @param RestAction|FractalAction $action
178
+ */
179
+ public function makeModuleFile (string $ class , string $ namespace , $ moduleId , $ action ): FileGenerator
180
+ {
181
+ $ file = new FileGenerator ;
182
+ $ reflection = new ClassGenerator (
183
+ $ class ,
184
+ $ namespace ,
185
+ null ,
186
+ 'yii\base\Module '
187
+ );
188
+
189
+ $ moduleIds = array_keys ($ action ->modulesList );
190
+ $ position = array_search ($ moduleId , $ moduleIds );
191
+ $ childModuleId = $ childModuleCode = null ;
192
+ if (array_key_exists ($ position + 1 , $ moduleIds )) { # if `true`, child module exists
193
+ $ childModuleId = $ moduleIds [$ position + 1 ];
194
+ $ childModuleNamespace = $ action ->modulesList [$ childModuleId ]['namespace ' ];
195
+ $ childModuleCode = <<<PHP
196
+ \$this->modules = [
197
+ ' {$ childModuleId }' => [
198
+ // you should consider using a shorter namespace here!
199
+ 'class' => \\ {$ childModuleNamespace }\Module::class,
200
+ ],
201
+ ];
202
+ PHP ;
203
+ }
204
+
205
+ $ reflection ->addMethod ('init ' , [], AbstractMemberGenerator::FLAG_PUBLIC , 'parent::init(); ' . PHP_EOL . $ childModuleCode );
206
+ $ file ->setClasses ([$ reflection ]);
207
+ return $ file ;
208
+ }
163
209
}
0 commit comments