33namespace Nanvaie \DatabaseRepository \Commands ;
44
55use Illuminate \Support \Str ;
6+ use Nanvaie \DatabaseRepository \CreateEntity ;
67use 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 ;
811
9- class MakeEntity extends Command
12+ class MakeEntity extends BaseCommand
1013{
14+ use CustomMySqlQueries;
1115 /**
1216 * The name and signature of the console command.
1317 *
@@ -26,39 +30,10 @@ class MakeEntity extends Command
2630 */
2731 protected $ description = 'Create a new entity. ' ;
2832
29- use CustomMySqlQueries;
3033
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- }
5734
5835 /**
5936 * Execute the console command.
60- *
61- * @return int
6237 */
6338 public function handle (): int
6439 {
@@ -70,28 +45,12 @@ public function handle(): int
7045 $ entityStubsPath = __DIR__ . '/../../ ' . config ('repository.path.stub.entities ' );
7146 $ filenameWithPath = $ relativeEntitiesPath . $ entityName .'.php ' ;
7247
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 );
8851
8952 $ 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 );
9554
9655 foreach ($ columns as $ _column ) {
9756 $ _column ->COLUMN_NAME = Str::camel ($ _column ->COLUMN_NAME );
@@ -101,60 +60,13 @@ public function handle(): int
10160 $ attributeStub = file_get_contents ($ entityStubsPath .'attribute.stub ' );
10261 $ accessorsStub = file_get_contents ($ entityStubsPath .'accessors.stub ' );
10362
104- // Create Attributes
105- $ attributes = '' ;
106- foreach ($ columns as $ _column ) {
107- $ attributes .= $ this ->writeAttribute (
108- $ attributeStub ,
109- $ _column ->COLUMN_NAME ,
110- ($ _column ->IS_NULLABLE === 'YES ' ? '? ' : '' ) . $ this ->dataTypes [$ _column ->DATA_TYPE ]
111- );
112- }
113-
114- // Create Setters and Getters
115- $ settersAndGetters = '' ;
116- foreach ($ columns as $ _column ) {
117- $ settersAndGetters .= $ this ->writeAccessors (
118- $ accessorsStub ,
119- $ _column ->COLUMN_NAME ,
120- ($ _column ->IS_NULLABLE === 'YES ' ? '? ' : '' ) . $ this ->dataTypes [$ _column ->DATA_TYPE ]
121- );
122- }
123-
124- if ($ detectForeignKeys ) {
125- $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
126-
127- // Create Additional Attributes from Foreign Keys
128- foreach ($ foreignKeys as $ _foreignKey ) {
129- $ attributes .= $ this ->writeAttribute (
130- $ attributeStub ,
131- $ _foreignKey ->VARIABLE_NAME ,
132- $ _foreignKey ->ENTITY_DATA_TYPE
133- );
134- }
135-
136- // Create Additional Setters and Getters from Foreign keys
137- foreach ($ foreignKeys as $ _foreignKey ) {
138- $ settersAndGetters .= $ this ->writeAccessors (
139- $ accessorsStub ,
140- $ _foreignKey ->VARIABLE_NAME ,
141- $ _foreignKey ->ENTITY_DATA_TYPE
142- );
143- }
144- }
145-
146- $ baseContent = str_replace (['{{ EntityNamespace }} ' , '{{ EntityName }} ' , '{{ Attributes }} ' , '{{ SettersAndGetters }} ' ],
147- [$ entityNamespace , $ entityName , $ attributes , $ settersAndGetters ],
148- $ baseContent );
149-
150- file_put_contents ($ filenameWithPath , $ baseContent );
151-
152- if ($ this ->option ('add-to-git ' )) {
153- shell_exec ('git add ' .$ filenameWithPath );
154- }
155-
156- $ 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 ();
15766
67+ $ this ->finalized ($ filenameWithPath , $ entityName , $ baseContent );
15868 return 0 ;
15969 }
70+
71+
16072}
0 commit comments