77use Illuminate \Support \Facades \Config ;
88use Illuminate \Support \Facades \Event ;
99use Illuminate \Support \Str ;
10+ use InvalidArgumentException ;
1011use RonasIT \Support \DTO \RelationsDTO ;
1112use RonasIT \Support \Events \SuccessCreateMessage ;
1213use RonasIT \Support \Events \WarningEvent ;
3031
3132class MakeEntityCommand extends Command
3233{
34+ private string $ entityName ;
35+ private string $ entityNamespace ;
36+ private RelationsDTO $ relations ;
37+
3338 const CRUD_OPTIONS = [
3439 'C ' , 'R ' , 'U ' , 'D '
3540 ];
@@ -118,6 +123,7 @@ public function handle(): void
118123 $ this ->validateInput ();
119124 $ this ->checkConfigs ();
120125 $ this ->listenEvents ();
126+ $ this ->parseRelations ();
121127
122128 try {
123129 $ this ->generate ();
@@ -126,7 +132,7 @@ public function handle(): void
126132 }
127133 }
128134
129- protected function checkConfigs ()
135+ protected function checkConfigs (): void
130136 {
131137 $ packageConfigPath = __DIR__ . '/../../config/entity-generator.php ' ;
132138 $ packageConfigs = require $ packageConfigPath ;
@@ -142,7 +148,7 @@ protected function checkConfigs()
142148 }
143149 }
144150
145- protected function outputNewConfig ($ packageConfigs , $ projectConfigs )
151+ protected function outputNewConfig (array $ packageConfigs , array $ projectConfigs ): array
146152 {
147153 $ flattenedPackageConfigs = Arr::dot ($ packageConfigs );
148154 $ flattenedProjectConfigs = Arr::dot ($ projectConfigs );
@@ -158,7 +164,7 @@ protected function outputNewConfig($packageConfigs, $projectConfigs)
158164 return array_undot ($ newConfig );
159165 }
160166
161- protected function customVarExport ($ expression )
167+ protected function customVarExport (array $ expression ): string
162168 {
163169 $ defaultExpression = var_export ($ expression , true );
164170
@@ -177,7 +183,7 @@ protected function customVarExport($expression)
177183 return preg_replace (array_keys ($ patterns ), array_values ($ patterns ), $ defaultExpression );
178184 }
179185
180- protected function classExists ($ path , $ name )
186+ protected function classExists (string $ path , string $ name ): bool
181187 {
182188 $ paths = config ('entity-generator.paths ' );
183189
@@ -188,13 +194,15 @@ protected function classExists($path, $name)
188194 return file_exists ($ classPath );
189195 }
190196
191- protected function validateInput ()
197+ protected function validateInput (): void
192198 {
199+ $ this ->validateEntityName ();
200+ $ this ->extractEntityNameAndPath ();
193201 $ this ->validateOnlyApiOption ();
194202 $ this ->validateCrudOptions ();
195203 }
196204
197- protected function generate ()
205+ protected function generate (): void
198206 {
199207 foreach ($ this ->rules ['only ' ] as $ option => $ generators ) {
200208 if ($ this ->option ($ option )) {
@@ -211,37 +219,60 @@ protected function generate()
211219 }
212220 }
213221
214- protected function runGeneration ($ generator )
222+ protected function runGeneration (string $ generator ): void
215223 {
216224 app ($ generator )
217- ->setModel ($ this ->argument ('name ' ))
225+ ->setModel ($ this ->entityName )
226+ ->setModelSubFolder ($ this ->entityNamespace )
218227 ->setFields ($ this ->getFields ())
219- ->setRelations ($ this ->getRelations () )
228+ ->setRelations ($ this ->relations )
220229 ->setCrudOptions ($ this ->getCrudOptions ())
221230 ->generate ();
222231 }
223232
224- protected function getCrudOptions ()
233+ protected function getCrudOptions (): array
225234 {
226235 return str_split ($ this ->option ('methods ' ));
227236 }
228237
229- protected function getRelations ()
238+ protected function parseRelations (): void
230239 {
231- return new RelationsDTO (
232- hasOne: $ this ->option ('has-one ' ),
233- hasMany: $ this ->option ('has-many ' ),
234- belongsTo: $ this ->option ('belongs-to ' ),
235- belongsToMany: $ this ->option ('belongs-to-many ' ),
240+ $ this -> relations = new RelationsDTO (
241+ hasOne: $ this ->trimRelations ( $ this -> option ('has-one ' ) ),
242+ hasMany: $ this ->trimRelations ( $ this -> option ('has-many ' ) ),
243+ belongsTo: $ this ->trimRelations ( $ this -> option ('belongs-to ' ) ),
244+ belongsToMany: $ this ->trimRelations ( $ this -> option ('belongs-to-many ' ) ),
236245 );
237246 }
238247
239- protected function getFields ()
248+ protected function trimRelations (array $ relations ): array
249+ {
250+ return array_map (
251+ callback: fn ($ relation ) => Str::trim ($ relation , '/ ' ),
252+ array: $ relations ,
253+ );
254+ }
255+
256+ protected function getFields (): array
240257 {
241258 return Arr::only ($ this ->options (), EntityGenerator::AVAILABLE_FIELDS );
242259 }
243260
244- protected function validateCrudOptions ()
261+ protected function validateEntityName (): void
262+ {
263+ if (!preg_match ('/^[A-Za-z0-9\/]+$/ ' , $ this ->argument ('name ' ))) {
264+ throw new InvalidArgumentException ("Invalid entity name {$ this ->argument ('name ' )}" );
265+ }
266+ }
267+
268+ protected function extractEntityNameAndPath (): void
269+ {
270+ list ($ this ->entityName , $ entityPath ) = extract_last_part ($ this ->argument ('name ' ), '/ ' );
271+
272+ $ this ->entityNamespace = Str::trim ($ entityPath , '/ ' );
273+ }
274+
275+ protected function validateCrudOptions (): void
245276 {
246277 $ crudOptions = $ this ->getCrudOptions ();
247278
@@ -252,7 +283,7 @@ protected function validateCrudOptions()
252283 }
253284 }
254285
255- protected function validateOnlyApiOption ()
286+ protected function validateOnlyApiOption (): void
256287 {
257288 if ($ this ->option ('only-api ' )) {
258289 $ modelName = Str::studly ($ this ->argument ('name ' ));
0 commit comments