@@ -26,6 +26,13 @@ class MakeFactory extends Command
2626
2727 use CustomMySqlQueries;
2828
29+ public function writeSetter (string $ setterStub , string $ attributeName )
30+ {
31+ return str_replace (['{{ SetterName }} ' , '{{ AttributeName }} ' ],
32+ [ucfirst ($ attributeName ), $ attributeName ],
33+ $ setterStub );
34+ }
35+
2936 /**
3037 * Execute the console command.
3138 *
@@ -41,6 +48,7 @@ public function handle(): int
4148 $ factoryNamespace = config ('repository.path.namespace.factories ' );
4249 $ relativeFactoriesPath = config ('repository.path.relative.factories ' );
4350 $ factoryStubsPath = config ('repository.path.stub.factories ' );
51+ $ filenameWithPath = $ relativeFactoriesPath .$ factoryName .'.php ' ;
4452
4553 if ($ this ->option ('delete ' )) {
4654 unlink ("$ relativeFactoriesPath/ $ factoryName.php " );
@@ -65,26 +73,28 @@ public function handle(): int
6573 die;
6674 }
6775
76+ foreach ($ columns as $ _column ) {
77+ $ _column ->COLUMN_NAME = camel_case ($ _column ->COLUMN_NAME );
78+ }
79+
80+ $ baseContent = file_get_contents ($ factoryStubsPath .'class.stub ' );
81+ $ setterStub = file_get_contents ($ factoryStubsPath .'setter.stub ' );
82+
6883 // Initialize Class
69- $ factoryContent = "<?php \n\nnamespace $ factoryNamespace; \n\n" ;
70- $ factoryContent .= "use stdClass; " ;
71- $ factoryContent .= "\nuse App\Models\Entities \\$ entityName; " ;
72- $ factoryContent .= "\nuse Nanvaie\DatabaseRepository\Models\Factory\Factory; \n\n" ;
73- $ factoryContent .= "class $ factoryName extends Factory \n{ \n" ;
74-
75- // Create "makeEntityFromStdClass" Function
76- $ factoryContent .= "\t/** \n\t * @param stdClass \$entity \n\t * @return $ entityName \n\t */ \n" ;
77- $ factoryContent .= "\tpublic function makeEntityFromStdClass(stdClass \$entity): $ entityName \n\t{ \n" ;
78- $ factoryContent .= "\t\t\$$ entityVariableName = new $ entityName(); \n" ;
7984 foreach ($ columns as $ _column ) {
80- $ factoryContent .= "\n\t\t\$" . $ entityVariableName . "->set " . ucfirst (camel_case ($ _column ->COLUMN_NAME )) . "( \$entity-> " . snake_case ($ _column ->COLUMN_NAME ) . " ?? null); " ;
85+ $ baseContent = substr_replace ($ baseContent ,
86+ $ this ->writeSetter ($ setterStub , $ _column ->COLUMN_NAME ),
87+ -53 , 0 );
8188 }
82- $ factoryContent .= "\n\n\t\treturn \$$ entityVariableName; \n\t} \n} " ;
8389
84- file_put_contents ("$ relativeFactoriesPath/ $ factoryName.php " , $ factoryContent );
90+ $ baseContent = str_replace (['{{ EntityName }} ' , '{{ EntityNamespace }} ' , '{{ FactoryName }} ' , '{{ FactoryNamespace }} ' , '{{ EntityVariableName }} ' ],
91+ [$ entityName , $ entityNamespace , $ factoryName , $ factoryNamespace , $ entityVariableName ],
92+ $ baseContent );
93+
94+ file_put_contents ($ filenameWithPath , $ baseContent );
8595
8696 if ($ this ->option ('add-to-git ' )) {
87- shell_exec ("git add $ relativeFactoriesPath / $ factoryName .php " );
97+ shell_exec ("git add $ filenameWithPath " );
8898 }
8999
90100 $ this ->info ("Factory \"$ factoryName \" has been created. " );
0 commit comments