3
3
namespace Nanvaie \DatabaseRepository \Commands ;
4
4
5
5
use Illuminate \Support \Str ;
6
+ use Nanvaie \DatabaseRepository \CreateEntity ;
6
7
use Nanvaie \DatabaseRepository \CustomMySqlQueries ;
7
- use Illuminate \Console \Command ;
8
+ use Nanvaie \DatabaseRepository \Creators \CreatorEntity ;
9
+ use Nanvaie \DatabaseRepository \Creators \BaseCreator ;
10
+ use Illuminate \Support \Collection ;
8
11
9
- class MakeEntity extends Command
12
+ class MakeEntity extends BaseCommand
10
13
{
14
+ use CustomMySqlQueries;
11
15
/**
12
16
* The name and signature of the console command.
13
17
*
@@ -26,39 +30,10 @@ class MakeEntity extends Command
26
30
*/
27
31
protected $ description = 'Create a new entity. ' ;
28
32
29
- use CustomMySqlQueries;
30
33
31
- /**
32
- * @param string $attributeStub
33
- * @param string $attributeName
34
- * @param string $attributeType
35
- * @return string
36
- */
37
- private function writeAttribute (string $ attributeStub , string $ attributeName , string $ attributeType ): string
38
- {
39
- return str_replace (['{{ AttributeType }} ' , '{{ AttributeName }} ' ],
40
- [$ attributeType , $ attributeName ],
41
- $ attributeStub );
42
- }
43
-
44
- /**
45
- * Generate getter and setter for given attribute.
46
- * @param string $accessorStub
47
- * @param string $attributeName
48
- * @param string $attributeType
49
- * @return string
50
- */
51
- private function writeAccessors (string $ accessorStub , string $ attributeName , string $ attributeType ): string
52
- {
53
- return str_replace (['{{ AttributeType }} ' , '{{ AttributeName }} ' , '{{ GetterName }} ' , '{{ SetterName }} ' ],
54
- [$ attributeType , $ attributeName , ucfirst ($ attributeName ), ucfirst ($ attributeName )],
55
- $ accessorStub );
56
- }
57
34
58
35
/**
59
36
* Execute the console command.
60
- *
61
- * @return int
62
37
*/
63
38
public function handle (): int
64
39
{
@@ -70,28 +45,12 @@ public function handle(): int
70
45
$ entityStubsPath = __DIR__ . '/../../ ' . config ('repository.path.stub.entities ' );
71
46
$ filenameWithPath = $ relativeEntitiesPath . $ entityName .'.php ' ;
72
47
73
- if (file_exists ($ filenameWithPath ) && $ this ->option ('delete ' )) {
74
- unlink ($ filenameWithPath );
75
- $ this ->info ("Entity \"$ entityName \" has been deleted. " );
76
- return 0 ;
77
- }
78
-
79
- if ( ! file_exists ($ relativeEntitiesPath ) && ! mkdir ($ relativeEntitiesPath , 0775 , true ) && ! is_dir ($ relativeEntitiesPath )) {
80
- $ this ->alert ("Directory \"$ relativeEntitiesPath \" was not created " );
81
- return 0 ;
82
- }
83
-
84
- if (class_exists ($ relativeEntitiesPath .'\\' .$ entityName ) && ! $ this ->option ('force ' )) {
85
- $ this ->alert ("Entity \"$ entityName \" is already exist! " );
86
- return 0 ;
87
- }
48
+ $ this ->checkDelete ($ filenameWithPath ,$ entityName );
49
+ $ this ->checkDirectory ($ relativeEntitiesPath ,$ entityName );
50
+ $ this ->checkClassExist ($ relativeEntitiesPath ,$ entityName );
88
51
89
52
$ columns = $ this ->getAllColumnsInTable ($ tableName );
90
-
91
- if ($ columns ->isEmpty ()) {
92
- $ this ->alert ("Couldn't retrieve columns from table \"$ tableName \"! Perhaps table's name is misspelled. " );
93
- die;
94
- }
53
+ $ this ->checkEmpty ($ columns ,$ tableName );
95
54
96
55
foreach ($ columns as $ _column ) {
97
56
$ _column ->COLUMN_NAME = Str::camel ($ _column ->COLUMN_NAME );
@@ -101,61 +60,13 @@ public function handle(): int
101
60
$ attributeStub = file_get_contents ($ entityStubsPath .'attribute.stub ' );
102
61
$ accessorsStub = file_get_contents ($ entityStubsPath .'accessors.stub ' );
103
62
104
- // Create Attributes
105
- $ attributes = '' ;
106
- foreach ($ columns as $ _column ) {
107
- $ defaultValue = ($ _column ->COLUMN_DEFAULT ?? 'null ' ) ? ($ _column ->COLUMN_DEFAULT ?? 'null ' ) : "'' " ;
108
- $ attributes .= $ this ->writeAttribute (
109
- $ attributeStub ,
110
- $ _column ->COLUMN_NAME .($ _column ->IS_NULLABLE === 'YES ' ? ' = ' .$ defaultValue : '' ),
111
- ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ this ->dataTypes [$ _column ->DATA_TYPE ]
112
- );
113
- }
114
-
115
- // Create Setters and Getters
116
- $ settersAndGetters = '' ;
117
- foreach ($ columns as $ _column ) {
118
- $ settersAndGetters .= $ this ->writeAccessors (
119
- $ accessorsStub ,
120
- $ _column ->COLUMN_NAME ,
121
- ($ _column ->IS_NULLABLE === 'YES ' ? 'null| ' : '' ) . $ this ->dataTypes [$ _column ->DATA_TYPE ]
122
- );
123
- }
124
-
125
- if ($ detectForeignKeys ) {
126
- $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
127
-
128
- // Create Additional Attributes from Foreign Keys
129
- foreach ($ foreignKeys as $ _foreignKey ) {
130
- $ attributes .= $ this ->writeAttribute (
131
- $ attributeStub ,
132
- $ _foreignKey ->VARIABLE_NAME ,
133
- $ _foreignKey ->ENTITY_DATA_TYPE
134
- );
135
- }
136
-
137
- // Create Additional Setters and Getters from Foreign keys
138
- foreach ($ foreignKeys as $ _foreignKey ) {
139
- $ settersAndGetters .= $ this ->writeAccessors (
140
- $ accessorsStub ,
141
- $ _foreignKey ->VARIABLE_NAME ,
142
- $ _foreignKey ->ENTITY_DATA_TYPE
143
- );
144
- }
145
- }
146
-
147
- $ baseContent = str_replace (['{{ EntityNamespace }} ' , '{{ EntityName }} ' , '{{ Attributes }} ' , '{{ SettersAndGetters }} ' ],
148
- [$ entityNamespace , $ entityName , $ attributes , $ settersAndGetters ],
149
- $ baseContent );
150
-
151
- file_put_contents ($ filenameWithPath , $ baseContent );
152
-
153
- if ($ this ->option ('add-to-git ' )) {
154
- shell_exec ('git add ' .$ filenameWithPath );
155
- }
156
-
157
- $ this ->info ("Entity \"$ entityName \" has been created. " );
63
+ $ entityCreator = new CreatorEntity ($ columns ,$ attributeStub , $ detectForeignKeys ,$ tableName ,$ entityName ,$ entityNamespace ,$ accessorsStub ,$ baseContent );
64
+ $ creator = new BaseCreator ($ entityCreator );
65
+ $ baseContent = $ creator ->createClass ();
158
66
67
+ $ this ->finalized ($ filenameWithPath , $ entityName , $ baseContent );
159
68
return 0 ;
160
69
}
70
+
71
+
161
72
}
0 commit comments