1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of FSConsoleTools |
4
|
|
|
* Copyright (C) 2018 Francesc Pineda Segarra <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, either version 3 of the |
9
|
|
|
* License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This program is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14
|
|
|
* GNU Lesser General Public License for more details. |
15
|
|
|
* |
16
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
17
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace FacturaScriptsUtils\Console\Command; |
21
|
|
|
|
22
|
|
|
use FacturaScriptsUtils\Console\ConsoleAbstract; |
23
|
|
|
|
24
|
|
|
if (!\defined('DB_CONNECTION')) { |
25
|
|
|
\define('DB_CONNECTION', false); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class GeneratePhpModel. |
30
|
|
|
* |
31
|
|
|
* @author Francesc Pineda Segarra <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class GenerateXmlTable extends ConsoleAbstract |
34
|
|
|
{ |
35
|
|
|
use Common\TableInformation; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constant values for return, to easy know how execution ends. |
39
|
|
|
*/ |
40
|
|
|
const RETURN_SUCCESS = 0; |
41
|
|
|
const RETURN_TABLE_NAME_NOT_SET = 1; |
42
|
|
|
const RETURN_MODEL_NAME_NOT_SET = 2; |
43
|
|
|
const RETURN_DST_FOLDER_NOT_SET = 3; |
44
|
|
|
const RETURN_CANT_CREATE_FOLDER = 4; |
45
|
|
|
const RETURN_TABLE_NOT_EXISTS = 5; |
46
|
|
|
const RETURN_FAIL_SAVING_FILE = 6; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Start point to run the command. |
50
|
|
|
* |
51
|
|
|
* @param array $params |
52
|
|
|
* |
53
|
|
|
* @return int |
54
|
|
|
*/ |
55
|
|
|
public function run(...$params): int |
56
|
|
|
{ |
57
|
|
|
if (!\DB_CONNECTION) { |
58
|
|
|
echo 'A database connection is needed. Do you set your config.php file?'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$status = $this->checkOptions($params); |
62
|
|
|
if ($status !== 0) { |
63
|
|
|
return $status; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$status = $this->checkParams($params); |
67
|
|
|
if ($status !== 0) { |
68
|
|
|
return $status; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
echo 'Generating XML table file' . \PHP_EOL . \PHP_EOL; |
72
|
|
|
echo ' Options setted:' . \PHP_EOL; |
73
|
|
|
echo ' Table name: ' . $this->getTableName() . \PHP_EOL; |
74
|
|
|
echo ' Destiny path: ' . $this->getDstFolder() . \PHP_EOL; |
75
|
|
|
|
76
|
|
|
if (!$this->areYouSure()) { |
77
|
|
|
echo ' Options [TABLE NAME] [DST]' . \PHP_EOL; |
78
|
|
|
return self::RETURN_SUCCESS; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$status = $this->check(); |
82
|
|
|
if ($status !== 0) { |
83
|
|
|
return $status; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->generateModel(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Return description about this class. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
public function getDescription(): string |
95
|
|
|
{ |
96
|
|
|
return 'Generate a XML table definition for model from database table.'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Print help information to the user. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
public function getHelpMsg(): string |
105
|
|
|
{ |
106
|
|
|
$array = \explode('\\', __CLASS__); |
107
|
|
|
$class = array_pop($array); |
108
|
|
|
return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL |
109
|
|
|
. 'Available options:' . \PHP_EOL |
110
|
|
|
. ' -h, --help Show this help.' . \PHP_EOL |
111
|
|
|
. ' -t, --tables Show tables.' . \PHP_EOL |
112
|
|
|
. ' -g, --gen Generate model.' . \PHP_EOL |
113
|
|
|
. \PHP_EOL |
114
|
|
|
. ' OPTION1 Table name' . \PHP_EOL |
115
|
|
|
. ' OPTION2 Destiny path' . \PHP_EOL |
116
|
|
|
. \PHP_EOL; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns an associative array of available methods for the user. |
121
|
|
|
* Add more options if you want to add support for custom methods. |
122
|
|
|
* [ |
123
|
|
|
* '-h' => 'getHelpMsg', |
124
|
|
|
* '--help' => 'getHelpMsg', |
125
|
|
|
* ] |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function getUserMethods(): array |
130
|
|
|
{ |
131
|
|
|
// Adding extra method |
132
|
|
|
$methods = parent::getUserMethods(); |
133
|
|
|
$methods['-t'] = 'getTablesMsg'; |
134
|
|
|
$methods['--tables'] = 'getTablesMsg'; |
135
|
|
|
$methods['-g'] = 'generateModel'; |
136
|
|
|
$methods['--gen'] = 'generateModel'; |
137
|
|
|
return $methods; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Check if options are looking for help. |
142
|
|
|
* |
143
|
|
|
* @param array $params |
144
|
|
|
* |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
|
|
private function checkOptions(array $params = []): int |
148
|
|
|
{ |
149
|
|
|
if (isset($params[0])) { |
150
|
|
|
switch ($params[0]) { |
151
|
|
|
case '-h': |
152
|
|
|
case '--help': |
153
|
|
|
echo $this->getHelpMsg(); |
154
|
|
|
return -1; |
155
|
|
|
case '-t': |
156
|
|
|
case '--tables': |
157
|
|
|
$this->setDataBase($params[1]); |
158
|
|
|
echo $this->getTablesMsg(); |
159
|
|
|
return -1; |
160
|
|
|
case '-g': |
161
|
|
|
case '--gen': |
162
|
|
|
$this->setTableName($params[1] ?? ''); |
163
|
|
|
$this->setDstFolder(\FS_FOLDER . ($params[2] ?? 'Core/Table')); |
164
|
|
|
$this->setDataBase($params[3]); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
return 0; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Check if options are looking for help. |
172
|
|
|
* |
173
|
|
|
* @param array $params |
174
|
|
|
* |
175
|
|
|
* @return int |
176
|
|
|
*/ |
177
|
|
|
private function checkParams(array $params = []): int |
178
|
|
|
{ |
179
|
|
|
if (!isset($params[0])) { |
180
|
|
|
echo 'No table name setted.' . \PHP_EOL; |
181
|
|
|
return -1; |
182
|
|
|
} |
183
|
|
|
if (!isset($params[1])) { |
184
|
|
|
echo 'No model name setted.' . \PHP_EOL; |
185
|
|
|
return -1; |
186
|
|
|
} |
187
|
|
|
return 0; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Launch basic checks. |
192
|
|
|
* |
193
|
|
|
* @return int |
194
|
|
|
*/ |
195
|
|
|
private function check(): int |
196
|
|
|
{ |
197
|
|
|
if ($this->getTableName() === null) { |
|
|
|
|
198
|
|
|
echo 'ERROR: Table name not setted.' . \PHP_EOL . \PHP_EOL; |
199
|
|
|
return self::RETURN_TABLE_NAME_NOT_SET; |
200
|
|
|
} |
201
|
|
|
if ($this->getDstFolder() === null) { |
|
|
|
|
202
|
|
|
echo 'ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL; |
203
|
|
|
return self::RETURN_DST_FOLDER_NOT_SET; |
204
|
|
|
} |
205
|
|
|
if (!is_file($this->getDstFolder()) && !@mkdir($this->getDstFolder()) && !is_dir($this->getDstFolder())) { |
206
|
|
|
echo "ERROR: Can't create folder " . $this->getDstFolder() . '.' . \PHP_EOL . \PHP_EOL; |
207
|
|
|
return self::RETURN_CANT_CREATE_FOLDER; |
208
|
|
|
} |
209
|
|
|
if (!\in_array($this->getTableName(), $this->dataBase->getTables(), false)) { |
210
|
|
|
echo 'ERROR: Table not exists.' . \PHP_EOL . \PHP_EOL; |
211
|
|
|
return self::RETURN_TABLE_NOT_EXISTS; |
212
|
|
|
} |
213
|
|
|
return self::RETURN_SUCCESS; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Generate model file. |
218
|
|
|
* |
219
|
|
|
* @return int |
220
|
|
|
*/ |
221
|
|
|
private function generateModel(): int |
222
|
|
|
{ |
223
|
|
|
$loader = new \Twig_Loader_Filesystem([__DIR__ . '/../Template']); |
224
|
|
|
$twig = new \Twig_Environment($loader, ['debug' => \FS_DEBUG,]); |
225
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
226
|
|
|
$txt = $twig->render( |
227
|
|
|
'XmlTable.xml.twig', |
228
|
|
|
['fsc' => $this] |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
$status = $this->saveFile($this->getDstFolder() . $this->getTableName() . '.xml', $txt); |
232
|
|
|
if (\is_bool($status)) { |
|
|
|
|
233
|
|
|
echo "Can't save " . $this->getDstFolder() . $this->getTableName() . '.xml"' . \PHP_EOL; |
234
|
|
|
return $status; |
235
|
|
|
} |
236
|
|
|
echo 'Finished! Look at "' . $this->getDstFolder() . '"' . \PHP_EOL; |
237
|
|
|
return self::RETURN_SUCCESS; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|