|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Sprint |
|
4
|
|
|
* |
|
5
|
|
|
* A set of power tools to enhance the CodeIgniter framework and provide consistent workflow. |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
15
|
|
|
* all copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
23
|
|
|
* THE SOFTWARE. |
|
24
|
|
|
* |
|
25
|
|
|
* @package Sprint |
|
26
|
|
|
* @author Lonnie Ezell |
|
27
|
|
|
* @copyright Copyright 2014-2015, New Myth Media, LLC (http://newmythmedia.com) |
|
28
|
|
|
* @license http://opensource.org/licenses/MIT (MIT) |
|
29
|
|
|
* @link http://sprintphp.com |
|
30
|
|
|
* @since Version 1.0 |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
use Myth\CLI; |
|
34
|
|
|
|
|
35
|
|
|
class ModelGenerator extends \Myth\Forge\BaseGenerator { |
|
36
|
|
|
|
|
37
|
|
|
protected $options = [ |
|
38
|
|
|
'table_name' => '', |
|
39
|
|
|
'primary_key' => '', |
|
40
|
|
|
'set_created' => true, |
|
41
|
|
|
'set_modified' => true, |
|
42
|
|
|
'created_field' => 'created_on', |
|
43
|
|
|
'modified_field' => 'modified_on', |
|
44
|
|
|
'date_format' => 'datetime', |
|
45
|
|
|
'log_user' => false, |
|
46
|
|
|
'created_by_field' => 'created_by', |
|
47
|
|
|
'modified_by_field' => 'modified_by', |
|
48
|
|
|
'deleted_by_field' => 'deleted_by', |
|
49
|
|
|
'use_soft_deletes' => true, |
|
50
|
|
|
'soft_delete_key' => 'deleted', |
|
51
|
|
|
'protected' => '', |
|
52
|
|
|
'return_type' => 'array', |
|
53
|
|
|
'return_insert_id' => true, |
|
54
|
|
|
'rules' => '[]' |
|
55
|
|
|
]; |
|
56
|
|
|
|
|
57
|
|
|
//-------------------------------------------------------------------- |
|
58
|
|
|
|
|
59
|
|
|
public function run( $segments = [ ], $quiet = false ) |
|
60
|
|
|
{ |
|
61
|
|
|
$name = array_shift( $segments ); |
|
62
|
|
|
|
|
63
|
|
|
$options = CLI::getOptions(); |
|
64
|
|
|
|
|
65
|
|
|
$this->options['table_name'] = array_shift( $segments ); |
|
66
|
|
|
|
|
67
|
|
|
if ( empty( $name ) ) |
|
68
|
|
|
{ |
|
69
|
|
|
$name = CLI::prompt( 'Model name' ); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
// Format to CI Standards |
|
73
|
|
|
if ( substr( $name, - 6 ) !== '_model' ) |
|
74
|
|
|
{ |
|
75
|
|
|
$name .= '_model'; |
|
76
|
|
|
} |
|
77
|
|
|
$name = ucfirst( $name ); |
|
78
|
|
|
|
|
79
|
|
|
if ( $quiet === false ) |
|
80
|
|
|
{ |
|
81
|
|
|
$this->collectOptions( $name, $options ); |
|
82
|
|
|
} |
|
83
|
|
|
else |
|
84
|
|
|
{ |
|
85
|
|
|
$this->quietSetOptions( $name, $options ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
$data = [ |
|
89
|
|
|
'model_name' => $name, |
|
90
|
|
|
'today' => date( 'Y-m-d H:ia' ) |
|
91
|
|
|
]; |
|
92
|
|
|
|
|
93
|
|
|
$data = array_merge( $data, $this->options ); |
|
94
|
|
|
|
|
95
|
|
|
$destination = $this->determineOutputPath( 'models' ) . $name . '.php'; |
|
96
|
|
|
|
|
97
|
|
|
if ( ! $this->copyTemplate( 'model', $destination, $data, $this->overwrite ) ) |
|
98
|
|
|
{ |
|
99
|
|
|
CLI::error( 'Error creating new files' ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return TRUE; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
//-------------------------------------------------------------------- |
|
106
|
|
|
|
|
107
|
|
|
/* |
|
108
|
|
|
* Customizes our settings |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function collectOptions( $model_name, $options=[] ) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->load->helper( 'inflector' ); |
|
113
|
|
|
|
|
114
|
|
|
// Table Name? |
|
115
|
|
|
if ( empty( $this->options['table_name'] ) ) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->options['table_name'] = empty( $options['table'] ) ? |
|
118
|
|
|
CLI::prompt( 'Table name', plural( strtolower( str_replace( '_model', '', $model_name ) ) ) ) : |
|
119
|
|
|
$options['table']; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
$this->options['fields'] = $this->table_info( $this->options['table_name'], $options ); |
|
123
|
|
|
|
|
124
|
|
|
// Primary Key |
|
125
|
|
|
if (empty($this->options['primary_key'])) |
|
126
|
|
|
{ |
|
127
|
|
|
$this->options['primary_key'] = empty( $options['primary_key'] ) ? |
|
128
|
|
|
CLI::prompt( 'Primary Key', 'id' ) : |
|
129
|
|
|
$options['primary_key']; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$this->options['protected'] = [ $this->options['primary_key'] ]; |
|
133
|
|
|
|
|
134
|
|
|
// Set Created? |
|
135
|
|
View Code Duplication |
if ( empty( $options['set_created'] ) ) |
|
136
|
|
|
{ |
|
137
|
|
|
$ans = CLI::prompt( 'Set Created date?', [ 'y', 'n' ] ); |
|
138
|
|
|
if ( $ans == 'n' ) |
|
139
|
|
|
{ |
|
140
|
|
|
$this->options['set_created'] = FALSE; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
// Set Modified? |
|
145
|
|
View Code Duplication |
if ( empty( $options['set_modified'] ) ) |
|
146
|
|
|
{ |
|
147
|
|
|
$ans = CLI::prompt( 'Set Modified date?', [ 'y', 'n' ] ); |
|
148
|
|
|
if ( $ans == 'n' ) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->options['set_modified'] = FALSE; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// Date Format |
|
155
|
|
|
$this->options['date_format'] = empty( $options['date_format'] ) ? |
|
156
|
|
|
CLI::prompt( 'Date Format?', [ 'datetime', 'date', 'int' ] ) : |
|
157
|
|
|
$options['date_format']; |
|
158
|
|
|
|
|
159
|
|
|
// Log User? |
|
160
|
|
View Code Duplication |
if ( empty( $options['log_user'] ) ) |
|
161
|
|
|
{ |
|
162
|
|
|
$ans = CLI::prompt( 'Log User actions?', [ 'y', 'n' ] ); |
|
163
|
|
|
if ( $ans == 'y' ) |
|
164
|
|
|
{ |
|
165
|
|
|
$this->options['log_user'] = TRUE; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
// Soft Deletes |
|
170
|
|
View Code Duplication |
if ( empty( $options['soft_delete'] ) ) |
|
171
|
|
|
{ |
|
172
|
|
|
$ans = CLI::prompt( 'Use Soft Deletes?', [ 'y', 'n' ] ); |
|
173
|
|
|
if ( $ans == 'n' ) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->options['soft_delete'] = false; |
|
176
|
|
|
} |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
//-------------------------------------------------------------------- |
|
182
|
|
|
|
|
183
|
|
|
protected function quietSetOptions( $model_name, $options=[] ) |
|
184
|
|
|
{ |
|
185
|
|
|
$this->load->helper( 'inflector' ); |
|
186
|
|
|
|
|
187
|
|
|
if (empty($this->options['table_name'])) |
|
188
|
|
|
{ |
|
189
|
|
|
$this->options['table_name'] = plural( strtolower( str_replace( '_model', '', $model_name ) ) ); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// Try to set it from the database first, |
|
193
|
|
|
// otherwise, try to pull from fields |
|
194
|
|
|
$this->options['fields'] = $this->table_info( $this->options['table_name'], $options ); |
|
195
|
|
|
|
|
196
|
|
|
$this->options['primary_key'] = ! empty( $this->options['primary_key'] ) ? $this->options['primary_key'] : 'id'; |
|
197
|
|
|
$this->options['protected'] = [ $this->options['primary_key'] ]; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
//-------------------------------------------------------------------- |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get the structure and details for the fields in the specified DB table |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $table_name Name of the table to check |
|
206
|
|
|
* |
|
207
|
|
|
* @return mixed An array of fields or false if the table does not exist |
|
208
|
|
|
*/ |
|
209
|
|
|
protected function table_info( $table_name, $options=[] ) |
|
210
|
|
|
{ |
|
211
|
|
|
$this->load->database(); |
|
212
|
|
|
|
|
213
|
|
|
// Check whether the table exists in this database |
|
214
|
|
|
if ( ! $this->db->table_exists( $table_name ) ) |
|
215
|
|
|
{ |
|
216
|
|
|
if (empty($options['fields'])) |
|
217
|
|
|
{ |
|
218
|
|
|
return FALSE; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
$fields = $this->parseFieldString($options['fields']); |
|
222
|
|
|
} |
|
223
|
|
|
else |
|
224
|
|
|
{ |
|
225
|
|
|
$fields = $this->db->field_data( $table_name ); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
// There may be something wrong or the database driver may not return |
|
229
|
|
|
// field data |
|
230
|
|
|
if ( empty( $fields ) ) |
|
231
|
|
|
{ |
|
232
|
|
|
return FALSE; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
$this->options['set_created'] = false; |
|
236
|
|
|
$this->options['set_modified'] = false; |
|
237
|
|
|
$this->options['use_soft_deletes'] = false; |
|
238
|
|
|
|
|
239
|
|
|
// Use the primary key if the table has one already set. |
|
240
|
|
|
foreach ( $fields as $field ) |
|
241
|
|
|
{ |
|
242
|
|
|
if ( ! empty( $field->primary_key ) && $field->primary_key == 1 ) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->options['primary_key'] = $field->name; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
if ($field->name == $this->options['created_field']) |
|
248
|
|
|
{ |
|
249
|
|
|
$this->options['set_created'] = true; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if ($field->name == $this->options['modified_field']) |
|
253
|
|
|
{ |
|
254
|
|
|
$this->options['set_modified'] = true; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
if ($field->name == $this->options['soft_delete_key']) |
|
258
|
|
|
{ |
|
259
|
|
|
$this->options['use_soft_deletes'] = true; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
// Set our validation rules based on these fields |
|
264
|
|
|
$this->options['rules'] = $this->buildValidationRules( $fields ); |
|
265
|
|
|
|
|
266
|
|
|
return $fields; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
//-------------------------------------------------------------------- |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Grabs the fields from the CLI options and gets them ready for |
|
273
|
|
|
* use within the views. |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function parseFieldString($fields) |
|
276
|
|
|
{ |
|
277
|
|
|
if ( empty( $fields ) ) |
|
278
|
|
|
{ |
|
279
|
|
|
return NULL; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
$fields = explode( ' ', $fields ); |
|
283
|
|
|
|
|
284
|
|
|
$new_fields = [ ]; |
|
285
|
|
|
|
|
286
|
|
|
foreach ( $fields as $field ) |
|
287
|
|
|
{ |
|
288
|
|
|
$pop = [ NULL, NULL, NULL ]; |
|
289
|
|
|
list( $field, $type, $size ) = array_merge( explode( ':', $field ), $pop ); |
|
290
|
|
|
$type = strtolower( $type ); |
|
291
|
|
|
|
|
292
|
|
|
// Strings |
|
293
|
|
View Code Duplication |
if (in_array($type, ['char', 'varchar', 'string'])) |
|
294
|
|
|
{ |
|
295
|
|
|
$new_fields[] = [ |
|
296
|
|
|
'name' => $field, |
|
297
|
|
|
'type' => 'text' |
|
298
|
|
|
]; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
// Textarea |
|
302
|
|
|
else if ($type == 'text') |
|
303
|
|
|
{ |
|
304
|
|
|
$new_fields[] = [ |
|
305
|
|
|
'name' => $field, |
|
306
|
|
|
'type' => 'textarea' |
|
307
|
|
|
]; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
// Number |
|
311
|
|
|
else if (in_array($type, ['tinyint', 'int', 'bigint', 'mediumint', 'float', 'double', 'number'])) |
|
312
|
|
|
{ |
|
313
|
|
|
$new_fields[] = [ |
|
314
|
|
|
'name' => $field, |
|
315
|
|
|
'type' => 'number' |
|
316
|
|
|
]; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
// Date |
|
320
|
|
|
else if (in_array($type, ['date', 'datetime', 'time'])) |
|
321
|
|
|
{ |
|
322
|
|
|
$new_fields[] = [ |
|
323
|
|
|
'name' => $field, |
|
324
|
|
|
'type' => $type |
|
325
|
|
|
]; |
|
326
|
|
|
} |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
// Convert to objects |
|
330
|
|
|
array_walk($new_fields, function(&$item, $key) { |
|
331
|
|
|
$item = (object)$item; |
|
332
|
|
|
}); |
|
333
|
|
|
|
|
334
|
|
|
return $new_fields; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
//-------------------------------------------------------------------- |
|
338
|
|
|
|
|
339
|
|
|
|
|
340
|
|
|
/** |
|
341
|
|
|
* Takes the fields from field_data() and creates the basic validation |
|
342
|
|
|
* rules for those fields. |
|
343
|
|
|
* |
|
344
|
|
|
* @param array $fields |
|
345
|
|
|
* |
|
346
|
|
|
* @return array |
|
347
|
|
|
*/ |
|
348
|
|
|
public function buildValidationrules($fields=[]) |
|
349
|
|
|
{ |
|
350
|
|
|
if (empty($fields) || ! is_array($fields) || ! count($fields)) |
|
351
|
|
|
{ |
|
352
|
|
|
return null; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
$rules = []; |
|
356
|
|
|
|
|
357
|
|
|
foreach ($fields as $field) |
|
358
|
|
|
{ |
|
359
|
|
|
$rule = []; |
|
360
|
|
|
|
|
361
|
|
|
switch ($field->type) |
|
362
|
|
|
{ |
|
363
|
|
|
// Numeric Types |
|
364
|
|
|
case 'tinyint': |
|
365
|
|
|
case 'smallint': |
|
366
|
|
|
case 'mediumint': |
|
367
|
|
|
case 'int': |
|
368
|
|
|
case 'integer': |
|
369
|
|
|
case 'bigint': |
|
370
|
|
|
$rule[] = 'integer'; |
|
371
|
|
|
break; |
|
372
|
|
|
case 'decimal': |
|
373
|
|
|
case 'dec': |
|
374
|
|
|
case 'numeric': |
|
375
|
|
|
case 'fixed': |
|
376
|
|
|
$rule[] = 'decimal'; |
|
377
|
|
|
break; |
|
378
|
|
|
case 'float': |
|
379
|
|
|
case 'double': |
|
380
|
|
|
$rule[] = 'numeric'; |
|
381
|
|
|
break; |
|
382
|
|
|
|
|
383
|
|
|
// Date types don't have many defaults we can go off of... |
|
384
|
|
|
|
|
385
|
|
|
// Text Types |
|
386
|
|
|
case 'char': |
|
387
|
|
|
case 'varchar': |
|
388
|
|
|
case 'text': |
|
389
|
|
|
$rule[] = 'alpha_numeric_spaces'; |
|
390
|
|
|
break; |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
if (! empty($field->max_length)) |
|
394
|
|
|
{ |
|
395
|
|
|
$rule[] = "max_length[{$field->max_length}]"; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
$rules[] = [ |
|
399
|
|
|
'field' => $field->name, |
|
400
|
|
|
'label' => ucwords(str_replace('_', ' ', $field->name)), |
|
401
|
|
|
'rules' => implode('|', $rule) |
|
402
|
|
|
]; |
|
403
|
|
|
} |
|
404
|
|
|
|
|
405
|
|
|
$str = $this->stringify($rules); |
|
406
|
|
|
|
|
407
|
|
|
// Clean up the resulting array a bit. |
|
408
|
|
|
$str = substr_replace($str, "\n]", -3); |
|
409
|
|
|
|
|
410
|
|
|
return $str; |
|
411
|
|
|
} |
|
412
|
|
|
|
|
413
|
|
|
//-------------------------------------------------------------------- |
|
414
|
|
|
|
|
415
|
|
|
} |