2
2
3
3
namespace Eghamat24 \DatabaseRepository \Commands ;
4
4
5
+ use Illuminate \Support \Collection ;
5
6
use Illuminate \Support \Str ;
6
7
use Eghamat24 \DatabaseRepository \Creators \BaseCreator ;
7
8
use Eghamat24 \DatabaseRepository \Creators \CreatorFactory ;
8
9
use Eghamat24 \DatabaseRepository \CustomMySqlQueries ;
9
10
10
11
class MakeFactory extends BaseCommand
11
12
{
13
+ use CustomMySqlQueries;
14
+
15
+ private const OBJECT_NAME = 'Factory ' ;
16
+
12
17
/**
13
18
* The name and signature of the console command.
14
19
*
@@ -26,51 +31,85 @@ class MakeFactory extends BaseCommand
26
31
*/
27
32
protected $ description = 'Create a new factory. ' ;
28
33
29
- use CustomMySqlQueries;
30
-
31
34
public function writeSetter (string $ setterStub , string $ columnName ): string
32
35
{
33
36
return str_replace (['{{ SetterName }} ' , '{{ AttributeName }} ' ],
34
37
[ucfirst ($ columnName ), Str::snake ($ columnName )],
35
38
$ setterStub );
36
39
}
37
40
38
- /**
39
- * Execute the console command.
40
- *
41
- * @return int
42
- */
43
41
public function handle (): void
44
42
{
45
43
$ this ->setArguments ();
46
44
47
45
$ filenameWithPath = $ this ->relativeFactoriesPath . $ this ->factoryName . '.php ' ;
48
46
49
- $ this ->checkDelete ($ filenameWithPath , $ this ->entityName , "Factory " );
50
- $ this ->checkDirectory ($ this ->relativeFactoriesPath );
51
- $ this ->checkClassExist ($ this ->factoryNamespace , $ this ->entityName , "Factory " );
47
+ $ this ->checkAndPrepare ($ filenameWithPath );
52
48
53
- $ columns = $ this ->getAllColumnsInTable ($ this ->tableName );
54
- $ this ->checkEmpty ($ columns , $ this ->tableName );
49
+ $ columns = $ this ->getColumnsOf ($ this ->tableName );
55
50
56
51
foreach ($ columns as $ _column ) {
57
52
$ _column ->COLUMN_NAME = Str::camel ($ _column ->COLUMN_NAME );
58
53
}
59
54
60
55
$ baseContent = file_get_contents ($ this ->factoryStubsPath . 'class.stub ' );
61
56
62
- $ factoryCreator = new CreatorFactory (
57
+ $ factoryCreator = $ this ->getCreatorFactory ($ columns , $ baseContent );
58
+ $ baseContent = $ this ->generateBaseContent ($ factoryCreator , $ filenameWithPath );
59
+
60
+ $ this ->finalized ($ filenameWithPath , $ this ->factoryName , $ baseContent );
61
+ }
62
+
63
+ /**
64
+ * @param string $filenameWithPath
65
+ * @return void
66
+ */
67
+ public function checkAndPrepare (string $ filenameWithPath ): void
68
+ {
69
+ $ this ->checkDelete ($ filenameWithPath , $ this ->entityName , self ::OBJECT_NAME );
70
+ $ this ->checkDirectory ($ this ->relativeFactoriesPath );
71
+ $ this ->checkClassExist ($ this ->factoryNamespace , $ this ->entityName , self ::OBJECT_NAME );
72
+ }
73
+
74
+ /**
75
+ * @param string $tableName
76
+ * @return Collection
77
+ */
78
+ public function getColumnsOf (string $ tableName ): Collection
79
+ {
80
+ $ columns = $ this ->getAllColumnsInTable ($ tableName );
81
+ $ this ->checkEmpty ($ columns , $ tableName );
82
+
83
+ return $ columns ;
84
+ }
85
+
86
+ /**
87
+ * @param Collection $columns
88
+ * @param bool|string $baseContent
89
+ * @return CreatorFactory
90
+ */
91
+ public function getCreatorFactory (Collection $ columns , bool |string $ baseContent ): CreatorFactory
92
+ {
93
+ return new CreatorFactory (
63
94
$ columns ,
64
95
$ this ->entityName ,
65
96
$ this ->entityNamespace ,
66
97
$ this ->factoryStubsPath ,
67
98
$ this ->factoryNamespace ,
68
99
$ this ->entityVariableName ,
69
100
$ this ->factoryName ,
70
- $ baseContent);
71
- $ creator = new BaseCreator ( $ factoryCreator );
72
- $ baseContent = $ creator -> createClass ( $ filenameWithPath , $ this );
101
+ $ baseContent
102
+ );
103
+ }
73
104
74
- $ this ->finalized ($ filenameWithPath , $ this ->factoryName , $ baseContent );
105
+ /**
106
+ * @param CreatorFactory $factoryCreator
107
+ * @param string $filenameWithPath
108
+ * @return string
109
+ */
110
+ public function generateBaseContent (CreatorFactory $ factoryCreator , string $ filenameWithPath ): string
111
+ {
112
+ $ creator = new BaseCreator ($ factoryCreator );
113
+ return $ creator ->createClass ($ filenameWithPath , $ this );
75
114
}
76
115
}
0 commit comments