12
12
* This class is part of Ubiquity
13
13
*
14
14
* @author jcheron <[email protected] >
15
- * @version 1.0.8
15
+ * @version 1.0.9
16
16
* @category ubiquity.dev
17
17
*
18
18
*/
19
19
abstract class ModelsCreator {
20
20
21
+ private $ silent = false ;
22
+
21
23
protected $ config ;
22
24
23
25
protected $ tables = array ();
@@ -34,12 +36,13 @@ abstract protected function getPrimaryKeys($tableName);
34
36
35
37
abstract protected function getForeignKeys ($ tableName , $ pkName , $ dbName = null );
36
38
37
- protected function init ($ config , $ offset = 'default ' ) {
39
+ protected function init (array $ config , string $ offset = 'default ' ) {
38
40
$ this ->config = DAO ::getDbOffset ($ config , $ offset );
39
41
}
40
42
41
- public function create ($ config , $ initCache = true , $ singleTable = null , $ offset = 'default ' , $ memberAccess = 'private ' ) {
42
- $ engine =CacheManager::getAnnotationsEngineInstance ();
43
+ public function create (array $ config , bool $ initCache = true , ?string $ singleTable = null , string $ offset = 'default ' , string $ memberAccess = 'private ' ) {
44
+ \ob_start ();
45
+ $ engine = CacheManager::getAnnotationsEngineInstance ();
43
46
$ this ->init ($ config , $ offset );
44
47
$ this ->memberAccess = $ memberAccess ;
45
48
$ dirPostfix = '' ;
@@ -54,14 +57,14 @@ public function create($config, $initCache = true, $singleTable = null, $offset
54
57
CacheManager::checkCache ($ config );
55
58
56
59
foreach ($ this ->tables as $ table ) {
57
- $ class = new Model ($ engine ,$ table , $ config ['mvcNS ' ]['models ' ] . $ nsPostfix , $ memberAccess );
60
+ $ class = new Model ($ engine , $ table , $ config ['mvcNS ' ]['models ' ] . $ nsPostfix , $ memberAccess );
58
61
$ class ->setDatabase ($ offset );
59
62
60
63
$ fieldsInfos = $ this ->getFieldsInfos ($ table );
61
64
$ class ->setSimpleMembers (\array_keys ($ fieldsInfos ));
62
65
$ keys = $ this ->getPrimaryKeys ($ table );
63
66
foreach ($ fieldsInfos as $ field => $ info ) {
64
- $ member = new Member ($ class ,$ engine ,$ field , $ memberAccess );
67
+ $ member = new Member ($ class , $ engine , $ field , $ memberAccess );
65
68
if (\in_array ($ field , $ keys )) {
66
69
$ member ->setPrimary ();
67
70
}
@@ -74,28 +77,33 @@ public function create($config, $initCache = true, $singleTable = null, $offset
74
77
$ this ->classes [$ table ] = $ class ;
75
78
}
76
79
$ this ->createRelations ();
77
-
80
+
78
81
if (isset ($ singleTable )) {
79
82
$ this ->createOneClass ($ singleTable , $ modelsDir );
80
83
} else {
81
84
foreach ($ this ->classes as $ table => $ class ) {
82
85
$ name = $ class ->getSimpleName ();
83
86
echo "Creating the {$ name } class \n" ;
84
- $ classContent= $ class ->__toString ();
87
+ $ classContent = $ class ->__toString ();
85
88
$ this ->writeFile ($ modelsDir . \DS . $ name . '.php ' , $ classContent );
86
89
}
87
90
}
88
91
if ($ initCache === true ) {
89
- CacheManager::initCache ($ config , 'models ' );
92
+ CacheManager::initCache ($ config , 'models ' , $ this -> silent );
90
93
}
91
94
}
95
+ $ r = \ob_get_clean ();
96
+ if ($ this ->silent ) {
97
+ return $ r ;
98
+ }
99
+ echo $ r ;
92
100
}
93
101
94
- protected function createOneClass ($ singleTable , $ modelsDir ) {
102
+ protected function createOneClass (string $ singleTable , string $ modelsDir ) {
95
103
if (isset ($ this ->classes [$ singleTable ])) {
96
104
$ class = $ this ->classes [$ singleTable ];
97
105
echo "Creating the {$ class ->getName ()} class \n" ;
98
- $ classContent= $ class ->__toString ();
106
+ $ classContent = $ class ->__toString ();
99
107
$ this ->writeFile ($ modelsDir . \DS . $ class ->getSimpleName () . '.php ' , $ classContent );
100
108
} else {
101
109
echo "The {$ singleTable } table does not exist in the database \n" ;
@@ -110,30 +118,30 @@ protected function createRelations() {
110
118
foreach ($ fks as $ fk ) {
111
119
$ field = \lcfirst ($ table );
112
120
$ fkTable = $ fk ['TABLE_NAME ' ];
113
- $ this ->classes [$ table ]->addOneToMany (\lcfirst ($ fkTable ) . 's ' , \lcfirst ($ table ), $ this ->classes [$ fkTable ]->getName (), $ this ->getAlternateName ($ fk ['COLUMN_NAME ' ], $ fk ['REFERENCED_COLUMN_NAME ' ]?? $ field ) . 's ' );
114
- $ this ->classes [$ fkTable ]->addManyToOne ($ field , \lcfirst ($ fk ['COLUMN_NAME ' ]), $ class ->getName (), $ this ->getAlternateName ($ fk ['COLUMN_NAME ' ], $ fk ['REFERENCED_COLUMN_NAME ' ]?? $ field ));
121
+ $ this ->classes [$ table ]->addOneToMany (\lcfirst ($ fkTable ) . 's ' , \lcfirst ($ table ), $ this ->classes [$ fkTable ]->getName (), $ this ->getAlternateName ($ fk ['COLUMN_NAME ' ], $ fk ['REFERENCED_COLUMN_NAME ' ] ?? $ field ) . 's ' );
122
+ $ this ->classes [$ fkTable ]->addManyToOne ($ field , \lcfirst ($ fk ['COLUMN_NAME ' ]), $ class ->getName (), $ this ->getAlternateName ($ fk ['COLUMN_NAME ' ], $ fk ['REFERENCED_COLUMN_NAME ' ] ?? $ field ));
115
123
}
116
124
}
117
125
}
118
126
$ this ->createManyToMany ();
119
127
}
120
128
121
- protected function getAlternateName ($ fkName , $ pkName ) {
122
- $ alter= $ fkName ;
123
- $ pkName= \ucfirst ($ pkName );
129
+ protected function getAlternateName (string $ fkName , string $ pkName ): string {
130
+ $ alter = $ fkName ;
131
+ $ pkName = \ucfirst ($ pkName );
124
132
if (\substr ($ fkName , 0 , \strlen ($ pkName )) == $ pkName ) {
125
133
$ alter = \substr ($ fkName , \strlen ($ pkName ));
126
134
}
127
- $ needle_position = \strlen ($ pkName ) * -1 ;
128
-
135
+ $ needle_position = \strlen ($ pkName ) * - 1 ;
136
+
129
137
if (\substr ($ alter , $ needle_position ) == $ pkName ) {
130
138
$ alter = \substr ($ alter , 0 , $ needle_position );
131
139
}
132
140
$ alter = \trim ($ alter , '_ ' );
133
141
return \lcfirst ($ alter );
134
142
}
135
143
136
- protected function getTableName ($ classname ) {
144
+ protected function getTableName (string $ classname ): string {
137
145
foreach ($ this ->classes as $ table => $ class ) {
138
146
if ($ class ->getName () === $ classname ) {
139
147
return $ table ;
@@ -155,20 +163,20 @@ protected function createManyToMany() {
155
163
$ table2 = $ this ->getTableName ($ manyToOne2 ->className );
156
164
$ class1 = $ this ->classes [$ table1 ];
157
165
$ class2 = $ this ->classes [$ table2 ];
158
- $ reflexive= ($ class1 ===$ class2 );
159
- if ($ reflexive ){
160
- $ table1Member= $ table2Member= $ table. 's ' ;
161
- $ altName1= $ this ->getAlternateName ($ manyToOne2 ->name , \current ($ this ->getPrimaryKeys ($ table1 ))) . 's ' ;
162
- }else {
166
+ $ reflexive = ($ class1 === $ class2 );
167
+ if ($ reflexive ) {
168
+ $ table1Member = $ table2Member = $ table . 's ' ;
169
+ $ altName1 = $ this ->getAlternateName ($ manyToOne2 ->name , \current ($ this ->getPrimaryKeys ($ table1 ))) . 's ' ;
170
+ } else {
163
171
$ table1Member = \lcfirst ($ table1 ) . 's ' ;
164
172
$ table2Member = \lcfirst ($ table2 ) . 's ' ;
165
- $ altName1= $ altName2= $ table. 's ' ;
173
+ $ altName1 = $ altName2 = $ table . 's ' ;
166
174
}
167
175
$ joinTable1 = $ this ->getJoinTableArray ($ class1 , $ manyToOne1 );
168
176
$ joinTable2 = $ this ->getJoinTableArray ($ class2 , $ manyToOne2 );
169
177
$ class1 ->removeOneToManyMemberByClassAssociation ($ class ->getName ());
170
178
$ class1 ->addManyToMany ($ table2Member , $ manyToOne2 ->className , $ table1Member , $ table , $ joinTable1 , $ joinTable2 , $ altName1 );
171
- if (! $ reflexive ){
179
+ if (! $ reflexive ) {
172
180
$ class2 ->removeOneToManyMemberByClassAssociation ($ class ->getName ());
173
181
$ class2 ->addManyToMany ($ table1Member , $ manyToOne1 ->className , $ table2Member , $ table , $ joinTable2 , $ joinTable1 , $ altName2 );
174
182
}
@@ -192,7 +200,15 @@ protected function getJoinTableArray(Model $class, object $joinColumn) {
192
200
return [];
193
201
}
194
202
195
- protected function writeFile ($ filename , $ data ) {
203
+ protected function writeFile (string $ filename , string $ data ): int {
196
204
return \file_put_contents ($ filename , $ data );
197
205
}
206
+
207
+ /**
208
+ *
209
+ * @param boolean $silent
210
+ */
211
+ public function setSilent (bool $ silent ): void {
212
+ $ this ->silent = $ silent ;
213
+ }
198
214
}
0 commit comments