1
+ <?php
2
+
3
+ namespace App \Console \Commands ;
4
+
5
+ use App \Traits \CustomMySqlQueries ;
6
+ use Illuminate \Console \Command ;
7
+
8
+ class MakeEntity extends Command
9
+ {
10
+ /**
11
+ * The name and signature of the console command.
12
+ *
13
+ * @var string
14
+ */
15
+ protected $ signature = 'command:make-entity {table_name} {--k|foreign-keys : Detect foreign keys} {--d|delete : Delete resource} {--f|force : Override/Delete existing mysql repository} ' ;
16
+
17
+ /**
18
+ * The console command description.
19
+ *
20
+ * @var string
21
+ */
22
+ protected $ description = 'Create a new entity. ' ;
23
+
24
+ /**
25
+ * Create a new command instance.
26
+ *
27
+ * @return void
28
+ */
29
+ public function __construct ()
30
+ {
31
+ parent ::__construct ();
32
+ }
33
+
34
+ use CustomMySqlQueries;
35
+
36
+ /**
37
+ * Generate a getter for given attribute.
38
+ * @param string $attributeName
39
+ * @param string $attributeType
40
+ * @return string
41
+ */
42
+ private function writeGetter (string $ attributeName , string $ attributeType ): string
43
+ {
44
+ return "\n\t/** \n\t * @return $ attributeType \n\t */ \n\t" .
45
+ "public function get " . ucfirst ($ attributeName ) . "(): $ attributeType \n\t" .
46
+ "{ \n\t\treturn \$this-> $ attributeName; \n\t} \n" ;
47
+ }
48
+
49
+ /**
50
+ * Generate a setter for given attribute.
51
+ * @param string $attributeName
52
+ * @param string $attributeType
53
+ * @return string
54
+ */
55
+ private function writeSetter (string $ attributeName , string $ attributeType ): string
56
+ {
57
+ return "\n\t/** \n\t * @param $ attributeType \$$ attributeName \n\t */ \n\t" .
58
+ "public function set " . ucfirst ($ attributeName ) . "( $ attributeType \$$ attributeName): void \n\t" .
59
+ "{ \n\t\t\$this-> $ attributeName = \$$ attributeName; \n\t} \n" ;
60
+ }
61
+
62
+ /**
63
+ * Execute the console command.
64
+ *
65
+ * @return int
66
+ */
67
+ public function handle (): int
68
+ {
69
+ $ tableName = $ this ->argument ('table_name ' );
70
+ $ detectForeignKeys = $ this ->option ('foreign-keys ' );
71
+ $ entityName = str_singular (ucfirst (camel_case ($ tableName )));
72
+ $ relativeEntitiesPath = config ('repository.path.relative.entities ' );
73
+
74
+ if ($ this ->option ('delete ' )) {
75
+ unlink ("$ relativeEntitiesPath/ $ entityName.php " );
76
+ $ this ->info ("Entity \"$ entityName \" has been deleted. " );
77
+ return 0 ;
78
+ }
79
+
80
+ if (!file_exists ($ relativeEntitiesPath )) {
81
+ mkdir ($ relativeEntitiesPath , 775 , true );
82
+ }
83
+
84
+ if (class_exists ("$ relativeEntitiesPath \\$ entityName " ) && !$ this ->option ('force ' )) {
85
+ $ this ->alert ("Entity $ entityName is already exist! " );
86
+ die;
87
+ }
88
+
89
+ $ 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
+ }
95
+
96
+ if ($ detectForeignKeys ) {
97
+ $ foreignKeys = $ this ->extractForeignKeys ($ tableName );
98
+ }
99
+
100
+ foreach ($ columns as $ _column ) {
101
+ $ _column ->COLUMN_NAME = camel_case ($ _column ->COLUMN_NAME );
102
+ }
103
+
104
+ // Initialize Class
105
+ $ entityContent = "<?php \n\nnamespace $ relativeEntitiesPath; \n\n" ;
106
+ $ entityContent .= "class $ entityName extends Entity \n{ \n" ;
107
+
108
+ // Create Attributes
109
+ foreach ($ columns as $ _column ) {
110
+ $ entityContent .= "\tprotected \$$ _column ->COLUMN_NAME ; \n" ;
111
+ }
112
+ // Create Additional Attributes from Foreign Keys
113
+ if ($ detectForeignKeys ) {
114
+ foreach ($ foreignKeys as $ _foreignKey ) {
115
+ $ entityContent .= "\n\tprotected \$" . $ _foreignKey ->VARIABLE_NAME . "; " ;
116
+ }
117
+ $ entityContent .= "\n" ;
118
+ }
119
+
120
+ // Create Setters and Getters
121
+ foreach ($ columns as $ _column ) {
122
+ $ dataType = $ this ->dataTypes [$ _column ->DATA_TYPE ];
123
+ $ entityContent .= $ this ->writeGetter ($ _column ->COLUMN_NAME , $ dataType );
124
+ $ entityContent .= $ this ->writeSetter ($ _column ->COLUMN_NAME , $ dataType );
125
+ }
126
+ // Create Additional Setters and Getters from Foreign keys
127
+ if ($ detectForeignKeys ) {
128
+ foreach ($ foreignKeys as $ _foreignKey ) {
129
+ $ entityContent .= $ this ->writeGetter ($ _foreignKey ->VARIABLE_NAME , $ _foreignKey ->ENTITY_DATA_TYPE );
130
+ $ entityContent .= $ this ->writeSetter ($ _foreignKey ->VARIABLE_NAME , $ _foreignKey ->ENTITY_DATA_TYPE );
131
+ }
132
+ }
133
+ $ entityContent .= "} " ;
134
+
135
+ file_put_contents ("$ relativeEntitiesPath/ $ entityName.php " , $ entityContent );
136
+
137
+ shell_exec ("git add $ relativeEntitiesPath/ $ entityName.php " );
138
+
139
+ $ this ->info ("Entity \"$ entityName \" has been created. " );
140
+
141
+ return 0 ;
142
+ }
143
+ }
0 commit comments