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,61 +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- $ 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 ();
15866
67+ $ this ->finalized ($ filenameWithPath , $ entityName , $ baseContent );
15968 return 0 ;
16069 }
70+
71+
16172}
0 commit comments