@@ -34,29 +34,17 @@ public function __construct()
34
34
use CustomMySqlQueries;
35
35
36
36
/**
37
- * Generate a getter for given attribute.
37
+ * Generate getter and setter for given attribute.
38
+ * @param string $accessorStub
38
39
* @param string $attributeName
39
40
* @param string $attributeType
40
41
* @return string
41
42
*/
42
- private function writeGetter ( string $ attributeName , string $ attributeType ): string
43
+ private function writeAccessors ( string $ accessorStub , string $ attributeName , string $ attributeType ): string
43
44
{
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" ;
45
+ return str_replace (['AttributeType ' , 'AttributeName ' , 'GetterName ' , 'SetterName ' ],
46
+ [$ attributeType , $ attributeName , ucfirst ($ attributeName ), ucfirst ($ attributeName )],
47
+ $ accessorStub );
60
48
}
61
49
62
50
/**
@@ -69,27 +57,31 @@ public function handle(): int
69
57
$ tableName = $ this ->argument ('table_name ' );
70
58
$ detectForeignKeys = $ this ->option ('foreign-keys ' );
71
59
$ entityName = str_singular (ucfirst (camel_case ($ tableName )));
60
+ $ entityNamespace = config ('repository.path.namespace.entities ' );
72
61
$ relativeEntitiesPath = config ('repository.path.relative.entities ' );
62
+ $ entityStubsPath = config ('repository.path.stubs.entity ' );
63
+ $ filenameWithPath = $ relativeEntitiesPath .$ entityName .'.php ' ;
73
64
74
65
if ($ this ->option ('delete ' )) {
75
- unlink (" $ relativeEntitiesPath / $ entityName .php " );
66
+ unlink ($ filenameWithPath );
76
67
$ this ->info ("Entity \"$ entityName \" has been deleted. " );
77
68
return 0 ;
78
69
}
79
70
80
- if (!file_exists ($ relativeEntitiesPath )) {
81
- mkdir ($ relativeEntitiesPath , 775 , true );
71
+ if ( ! file_exists ($ relativeEntitiesPath ) && ! mkdir ($ relativeEntitiesPath , 775 , true ) && ! is_dir ($ relativeEntitiesPath )) {
72
+ $ this ->alert ("Directory \"$ relativeEntitiesPath \" was not created " );
73
+ return 0 ;
82
74
}
83
75
84
- if (class_exists (" $ relativeEntitiesPath \\ $ entityName" ) && !$ this ->option ('force ' )) {
85
- $ this ->alert ("Entity $ entityName is already exist! " );
86
- die ;
76
+ if (class_exists ($ entityNamespace . '\\' . $ entityName ) && ! $ this ->option ('force ' )) {
77
+ $ this ->alert ("Entity \" $ entityName\" is already exist! " );
78
+ return 0 ;
87
79
}
88
80
89
81
$ columns = $ this ->getAllColumnsInTable ($ tableName );
90
82
91
83
if ($ columns ->isEmpty ()) {
92
- $ this ->alert ("Couldn't retrieve columns from table " . $ tableName . "! Perhaps table's name is misspelled. " );
84
+ $ this ->alert ("Couldn't retrieve columns from table \" $ tableName\ "! Perhaps table's name is misspelled. " );
93
85
die;
94
86
}
95
87
@@ -101,40 +93,43 @@ public function handle(): int
101
93
$ _column ->COLUMN_NAME = camel_case ($ _column ->COLUMN_NAME );
102
94
}
103
95
96
+ $ baseContent = file_get_contents ($ entityStubsPath .'Class.stub ' );
97
+ $ accessorsStub = file_get_contents ($ entityStubsPath .'Accessors.stub ' );
98
+
104
99
// Initialize Class
105
- $ entityContent = "<?php \n\nnamespace $ relativeEntitiesPath; \n\n" ;
106
- $ entityContent .= "class $ entityName extends Entity \n{ \n" ;
100
+ $ baseContent = str_replace (['EntityNameSpace ' , 'EntityName ' ], [$ entityNamespace , $ entityName ], $ baseContent );
107
101
108
102
// Create Attributes
109
103
foreach ($ columns as $ _column ) {
110
- $ entityContent .= "\tprotected \$$ _column ->COLUMN_NAME ; \n" ;
104
+ $ baseContent = substr_replace ($ baseContent ,
105
+ "\tprotected " . $ this ->dataTypes [$ _column ->DATA_TYPE ] . " \$$ _column ->COLUMN_NAME ; \n" ,
106
+ -1 , 0 );
111
107
}
112
108
// Create Additional Attributes from Foreign Keys
113
109
if ($ detectForeignKeys ) {
114
110
foreach ($ foreignKeys as $ _foreignKey ) {
115
- $ entityContent .= "\n\tprotected \$" . $ _foreignKey ->VARIABLE_NAME . "; " ;
111
+ $ baseContent = substr_replace ($ baseContent ,
112
+ "\tprotected " . $ _foreignKey ->ENTITY_DATA_TYPE . " \$$ _foreignKey ->VARIABLE_NAME ; \n" ,
113
+ -1 , 0 );
116
114
}
117
- $ entityContent .= "\n" ;
118
115
}
119
116
120
117
// Create Setters and Getters
121
118
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 );
119
+ $ baseContent = substr_replace ( $ baseContent ,
120
+ $ this ->writeAccessors ( $ accessorsStub , $ _column ->COLUMN_NAME , $ this -> dataTypes [ $ _column -> DATA_TYPE ]),
121
+ - 1 , 0 );
125
122
}
126
123
// Create Additional Setters and Getters from Foreign keys
127
124
if ($ detectForeignKeys ) {
128
125
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 );
126
+ $ baseContent = substr_replace ($ baseContent , $ this ->writeAccessors ($ accessorsStub , $ _foreignKey ->VARIABLE_NAME , $ _foreignKey ->ENTITY_DATA_TYPE ), -1 , 0 );
131
127
}
132
128
}
133
- $ entityContent .= "} " ;
134
129
135
- file_put_contents (" $ relativeEntitiesPath / $ entityName .php " , $ entityContent );
130
+ file_put_contents ($ filenameWithPath , $ baseContent );
136
131
137
- shell_exec (" git add $ relativeEntitiesPath / $ entityName .php " );
132
+ shell_exec (' git add ' . $ filenameWithPath );
138
133
139
134
$ this ->info ("Entity \"$ entityName \" has been created. " );
140
135
0 commit comments