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 GenerateXmlListModel. |
30
|
|
|
* |
31
|
|
|
* @author Francesc Pineda Segarra <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class GenerateXmlListModel 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 XMLView List file' . \PHP_EOL . \PHP_EOL; |
72
|
|
|
echo ' Options setted:' . \PHP_EOL; |
73
|
|
|
echo ' Table name: ' . $this->getTableName() . \PHP_EOL; |
74
|
|
|
echo ' Model name: ' . $this->getModelName() . \PHP_EOL; |
75
|
|
|
echo ' Destiny path: ' . $this->getDstFolder() . \PHP_EOL; |
76
|
|
|
|
77
|
|
|
if (!$this->areYouSure()) { |
78
|
|
|
echo ' Options [TABLE NAME] [MODEL NAME] [DST]' . \PHP_EOL; |
79
|
|
|
return self::RETURN_SUCCESS; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$status = $this->check(); |
83
|
|
|
if ($status !== 0) { |
84
|
|
|
return $status; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->generateModel(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return description about this class. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getDescription(): string |
96
|
|
|
{ |
97
|
|
|
return 'Generate a XMLView List for model class from database table.'; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Print help information to the user. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
|
|
public function getHelpMsg(): string |
106
|
|
|
{ |
107
|
|
|
$array = \explode('\\', __CLASS__); |
108
|
|
|
$class = array_pop($array); |
109
|
|
|
return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL |
110
|
|
|
. 'Available options:' . \PHP_EOL |
111
|
|
|
. ' -h, --help Show this help.' . \PHP_EOL |
112
|
|
|
. ' -t, --tables Show tables.' . \PHP_EOL |
113
|
|
|
. ' -g, --gen Generate model.' . \PHP_EOL |
114
|
|
|
. \PHP_EOL |
115
|
|
|
. ' OPTION1 Table name' . \PHP_EOL |
116
|
|
|
. ' OPTION2 Model name' . \PHP_EOL |
117
|
|
|
. ' OPTION3 Destiny path' . \PHP_EOL |
118
|
|
|
. \PHP_EOL; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns an associative array of available methods for the user. |
123
|
|
|
* Add more options if you want to add support for custom methods. |
124
|
|
|
* [ |
125
|
|
|
* '-h' => 'getHelpMsg', |
126
|
|
|
* '--help' => 'getHelpMsg', |
127
|
|
|
* ] |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
|
|
public function getUserMethods(): array |
132
|
|
|
{ |
133
|
|
|
// Adding extra method |
134
|
|
|
$methods = parent::getUserMethods(); |
135
|
|
|
$methods['-t'] = 'getTablesMsg'; |
136
|
|
|
$methods['--tables'] = 'getTablesMsg'; |
137
|
|
|
$methods['-g'] = 'generateModel'; |
138
|
|
|
$methods['--gen'] = 'generateModel'; |
139
|
|
|
return $methods; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Check if options are looking for help. |
144
|
|
|
* |
145
|
|
|
* @param array $params |
146
|
|
|
* |
147
|
|
|
* @return int |
148
|
|
|
*/ |
149
|
|
|
private function checkOptions(array $params = []): int |
150
|
|
|
{ |
151
|
|
|
if (isset($params[0])) { |
152
|
|
|
switch ($params[0]) { |
153
|
|
|
case '-h': |
154
|
|
|
case '--help': |
155
|
|
|
echo $this->getHelpMsg(); |
156
|
|
|
return -1; |
157
|
|
|
case '-t': |
158
|
|
|
case '--tables': |
159
|
|
|
$this->setDataBase($params[1]); |
160
|
|
|
echo $this->getTablesMsg(); |
161
|
|
|
return -1; |
162
|
|
|
case '-g': |
163
|
|
|
case '--gen': |
164
|
|
|
$this->setTableName($params[1] ?? ''); |
165
|
|
|
$this->setModelName($params[2] ?? ''); |
166
|
|
|
$this->setDstFolder(\FS_FOLDER . ($params[3] ?? 'Core/XMLView')); |
167
|
|
|
$this->setDataBase($params[4]); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
return 0; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Check if options are looking for help. |
175
|
|
|
* |
176
|
|
|
* @param array $params |
177
|
|
|
* |
178
|
|
|
* @return int |
179
|
|
|
*/ |
180
|
|
|
private function checkParams(array $params = []): int |
181
|
|
|
{ |
182
|
|
|
if (!isset($params[0])) { |
183
|
|
|
echo 'No table name setted.' . \PHP_EOL; |
184
|
|
|
return -1; |
185
|
|
|
} |
186
|
|
|
if (!isset($params[1])) { |
187
|
|
|
echo 'No model name setted.' . \PHP_EOL; |
188
|
|
|
return -1; |
189
|
|
|
} |
190
|
|
|
return 0; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Launch basic checks. |
195
|
|
|
* |
196
|
|
|
* @return int |
197
|
|
|
*/ |
198
|
|
|
private function check(): int |
199
|
|
|
{ |
200
|
|
|
if ($this->getTableName() === null) { |
|
|
|
|
201
|
|
|
echo 'ERROR: Table name not setted.' . \PHP_EOL . \PHP_EOL; |
202
|
|
|
return self::RETURN_TABLE_NAME_NOT_SET; |
203
|
|
|
} |
204
|
|
|
if ($this->getModelName() === null) { |
|
|
|
|
205
|
|
|
echo 'ERROR: Model name not setted.' . \PHP_EOL . \PHP_EOL; |
206
|
|
|
return self::RETURN_MODEL_NAME_NOT_SET; |
207
|
|
|
} |
208
|
|
|
if ($this->getDstFolder() === null) { |
|
|
|
|
209
|
|
|
echo 'ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL; |
210
|
|
|
return self::RETURN_DST_FOLDER_NOT_SET; |
211
|
|
|
} |
212
|
|
|
if (!is_file($this->getDstFolder()) && !@mkdir($this->getDstFolder()) && !is_dir($this->getDstFolder())) { |
213
|
|
|
echo "ERROR: Can't create folder " . $this->getDstFolder() . '.' . \PHP_EOL . \PHP_EOL; |
214
|
|
|
return self::RETURN_CANT_CREATE_FOLDER; |
215
|
|
|
} |
216
|
|
|
if (!\in_array($this->getTableName(), $this->dataBase->getTables(), false)) { |
217
|
|
|
echo 'ERROR: Table not exists.' . \PHP_EOL . \PHP_EOL; |
218
|
|
|
return self::RETURN_TABLE_NOT_EXISTS; |
219
|
|
|
} |
220
|
|
|
return self::RETURN_SUCCESS; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Generate model file. |
225
|
|
|
* |
226
|
|
|
* @return int |
227
|
|
|
*/ |
228
|
|
|
private function generateModel(): int |
229
|
|
|
{ |
230
|
|
|
$loader = new \Twig_Loader_Filesystem([__DIR__ . '/../Template']); |
231
|
|
|
$twig = new \Twig_Environment($loader, ['debug' => \FS_DEBUG,]); |
232
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
233
|
|
|
$txt = $twig->render( |
234
|
|
|
'ViewListModel.xml.twig', |
235
|
|
|
['fsc' => $this] |
236
|
|
|
); |
237
|
|
|
|
238
|
|
|
$status = $this->saveFile($this->getDstFolder() . 'List' . $this->getModelName() . '.xml', $txt); |
239
|
|
|
if (\is_bool($status)) { |
|
|
|
|
240
|
|
|
echo "Can't save " . $this->getDstFolder() . 'List' . $this->getModelName() . '.xml"' . \PHP_EOL; |
241
|
|
|
return $status; |
242
|
|
|
} |
243
|
|
|
echo 'Finished! Look at "' . $this->getDstFolder() . '"' . \PHP_EOL; |
244
|
|
|
return self::RETURN_SUCCESS; |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|