1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Zemit Framework. |
4
|
|
|
* |
5
|
|
|
* (c) Zemit Team <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Zemit\Modules\Cli\Tasks; |
12
|
|
|
|
13
|
|
|
use Phalcon\Db\Column; |
14
|
|
|
use Phalcon\Text; |
15
|
|
|
use Zemit\Modules\Cli\Task; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class ScaffoldTask |
19
|
|
|
* |
20
|
|
|
* @author Julien Turbide <[email protected]> |
21
|
|
|
* @copyright Zemit Team <[email protected]> |
22
|
|
|
* |
23
|
|
|
* @since 1.0 |
24
|
|
|
* @version 1.0 |
25
|
|
|
* |
26
|
|
|
* @package Zemit\Modules\Cli\Tasks |
27
|
|
|
*/ |
28
|
|
|
class ScaffoldTask extends Task |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $consoleDoc = <<<DOC |
34
|
|
|
Usage: |
35
|
|
|
php zemit cli scaffold <action> [<params> ...] |
36
|
|
|
|
37
|
|
|
Options: |
38
|
|
|
task: scaffold |
39
|
|
|
action: main |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
DOC; |
43
|
|
|
|
44
|
|
|
public $modelsPath = 'ts/Models/'; |
45
|
|
|
public $abstractPath = './Base/'; |
46
|
|
|
|
47
|
|
|
public function allModelsAction() |
48
|
|
|
{ |
49
|
|
|
$ret = []; |
50
|
|
|
|
51
|
|
|
$tables = $this->db->listTables(); |
52
|
|
|
foreach ($tables as $table) { |
53
|
|
|
|
54
|
|
|
$className = ucFirst(Text::camelize($table)); |
55
|
|
|
$fileName = $className . '.ts'; |
56
|
|
|
|
57
|
|
|
$abstractClassName = 'Abstract' . $className; |
58
|
|
|
$abstractFileName = $abstractClassName . '.ts'; |
59
|
|
|
|
60
|
|
|
// Create Abstract |
61
|
|
|
$output = []; |
62
|
|
|
$output [] = 'import { AbstractModel } from \'../AbstractModel\';' . PHP_EOL; |
63
|
|
|
$output [] = 'export class ' . $abstractClassName . ' extends AbstractModel {'; |
64
|
|
|
|
65
|
|
|
$columns = $this->db->describeColumns($table); |
66
|
|
|
foreach ($columns as $column) { |
67
|
|
|
|
68
|
|
|
switch($column->getType()) { |
|
|
|
|
69
|
|
|
case Column::TYPE_TIMESTAMP: |
70
|
|
|
case Column::TYPE_MEDIUMINTEGER: |
71
|
|
|
case Column::TYPE_SMALLINTEGER: |
72
|
|
|
case Column::TYPE_TINYINTEGER: |
73
|
|
|
case Column::TYPE_INTEGER: |
74
|
|
|
case Column::TYPE_DECIMAL: |
75
|
|
|
case Column::TYPE_DOUBLE: |
76
|
|
|
case Column::TYPE_FLOAT: |
77
|
|
|
case Column::TYPE_BIT: |
78
|
|
|
$tsType = 'number'; |
79
|
|
|
break; |
80
|
|
|
case Column::TYPE_BIGINTEGER: |
81
|
|
|
$tsType = 'bigint'; |
82
|
|
|
break; |
83
|
|
|
case Column::TYPE_ENUM: |
84
|
|
|
// $tsType = 'enum'; |
85
|
|
|
// break; |
86
|
|
|
case Column::TYPE_VARCHAR: |
87
|
|
|
case Column::TYPE_CHAR: |
88
|
|
|
case Column::TYPE_TEXT: |
89
|
|
|
case Column::TYPE_TINYTEXT: |
90
|
|
|
case Column::TYPE_MEDIUMTEXT: |
91
|
|
|
case Column::TYPE_BLOB: |
92
|
|
|
case Column::TYPE_TINYBLOB: |
93
|
|
|
case Column::TYPE_LONGBLOB: |
94
|
|
|
case Column::TYPE_DATETIME: |
95
|
|
|
case Column::TYPE_DATE: |
96
|
|
|
case Column::TYPE_TIME: |
97
|
|
|
$tsType = 'string'; |
98
|
|
|
break; |
99
|
|
|
case Column::TYPE_JSON: |
100
|
|
|
case Column::TYPE_JSONB: |
101
|
|
|
$tsType = 'object'; |
102
|
|
|
break; |
103
|
|
|
case Column::TYPE_BOOLEAN: |
104
|
|
|
$tsType = 'boolean'; |
105
|
|
|
break; |
106
|
|
|
default: |
107
|
|
|
$tsType = 'any'; |
108
|
|
|
break; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$columnDefault = $column->getDefault(); |
112
|
|
|
switch (getType(strtolower($columnDefault))) { |
113
|
|
|
// "boolean", "integer", "double", "string", "array", "object", "resource", "NULL", "unknown type", "resource (closed)" |
114
|
|
|
case "boolean": |
115
|
|
|
case "integer": |
116
|
|
|
case "double": |
117
|
|
|
case "null": |
118
|
|
|
$default = $columnDefault; |
119
|
|
|
break; |
120
|
|
|
case "string": |
121
|
|
|
if ($tsType === 'string') { |
122
|
|
|
$default = !empty($columnDefault)? '"' . addslashes($columnDefault) . '"' : null; |
123
|
|
|
} |
124
|
|
|
if ($tsType === 'number') { |
125
|
|
|
$default = !empty($columnDefault)? $columnDefault : null; |
126
|
|
|
} |
127
|
|
|
if ($tsType === 'array') { |
128
|
|
|
$default = '[]'; |
129
|
|
|
} |
130
|
|
|
if ($tsType === 'object') { |
131
|
|
|
$default = '{}'; |
132
|
|
|
} |
133
|
|
|
break; |
134
|
|
|
case "array": |
135
|
|
|
$default = '[]'; |
136
|
|
|
break; |
137
|
|
|
case "object": |
138
|
|
|
$default = '{}'; |
139
|
|
|
break; |
140
|
|
|
default: |
141
|
|
|
break; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$propertyName = lcfirst(Text::camelize($column->getName())); |
145
|
|
|
$output [] = ' public ' . $propertyName . ': ' . $tsType . (!empty($default)? ' = ' . $default : null) . ';'; |
146
|
|
|
} |
147
|
|
|
$output [] = '}'; |
148
|
|
|
|
149
|
|
|
// Save Abstract Model File |
150
|
|
|
$this->saveFile( |
151
|
|
|
$this->modelsPath . $this->abstractPath . $abstractFileName, |
152
|
|
|
implode(PHP_EOL, $output) |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$ret [] = 'Abstract Model ' . $abstractClassName . ' created'; |
156
|
|
|
|
157
|
|
|
// Create Model |
158
|
|
|
$output = []; |
159
|
|
|
$output [] = 'import { ' . $abstractClassName . ' } from \'' . $this->abstractPath . $abstractClassName . '\';' . PHP_EOL; |
160
|
|
|
$output [] = 'export class ' . $className . ' extends ' . $abstractClassName . ' {'; |
161
|
|
|
$output [] = ' '; |
162
|
|
|
$output [] = '}'; |
163
|
|
|
|
164
|
|
|
// Save Model File |
165
|
|
|
$this->saveFile( |
166
|
|
|
$this->modelsPath . $fileName, |
167
|
|
|
implode(PHP_EOL, $output) |
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$ret [] = 'Model ' . $className . ' created'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
dd($ret); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
protected function saveFile($file, $text) |
177
|
|
|
{ |
178
|
|
|
$file = fopen($file, 'w'); |
179
|
|
|
fwrite($file, $text); |
180
|
|
|
fclose($file); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|