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
|
|
|
$this->autoReply = isset($params[4]) ? (bool) $params[4] : false; |
58
|
|
|
$this->autoHide = isset($params[5]) ? (bool) $params[5] : false; |
59
|
|
|
|
60
|
|
|
if (!\DB_CONNECTION) { |
61
|
|
|
trigger_error('A database connection is needed. Do you set your config.php file?'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$status = $this->checkOptions($params); |
65
|
|
|
if ($status !== 0) { |
66
|
|
|
return $status; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$status = $this->checkParams($params); |
70
|
|
|
if ($status !== 0) { |
71
|
|
|
return $status; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->showMessage('Generating XML table file' . \PHP_EOL . \PHP_EOL); |
75
|
|
|
$this->showMessage(' Options setted:' . \PHP_EOL); |
76
|
|
|
$this->showMessage(' Table name: ' . $this->getTableName() . \PHP_EOL); |
77
|
|
|
$this->showMessage(' Destiny path: ' . $this->getDstFolder() . \PHP_EOL); |
78
|
|
|
|
79
|
|
|
if (!$this->areYouSure($this->autoReply)) { |
80
|
|
|
$this->showMessage(' Options [TABLE NAME] [DST]' . \PHP_EOL); |
81
|
|
|
return self::RETURN_SUCCESS; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$status = $this->check(); |
85
|
|
|
if ($status !== 0) { |
86
|
|
|
return $status; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $this->generateModel(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Return description about this class. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function getDescription(): string |
98
|
|
|
{ |
99
|
|
|
return 'Generate a XML table definition for model from database table.'; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Print help information to the user. |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getHelpMsg(): string |
108
|
|
|
{ |
109
|
|
|
$array = \explode('\\', __CLASS__); |
110
|
|
|
$class = array_pop($array); |
111
|
|
|
return 'Use as: php vendor/bin/console ' . $class . ' [OPTIONS]' . \PHP_EOL |
112
|
|
|
. 'Available options:' . \PHP_EOL |
113
|
|
|
. ' -h, --help Show this help.' . \PHP_EOL |
114
|
|
|
. ' -t, --tables Show tables.' . \PHP_EOL |
115
|
|
|
. ' -g, --gen Generate model.' . \PHP_EOL |
116
|
|
|
. \PHP_EOL |
117
|
|
|
. ' OPTION1 Table name' . \PHP_EOL |
118
|
|
|
. ' OPTION2 Destiny path' . \PHP_EOL |
119
|
|
|
. \PHP_EOL; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns an associative array of available methods for the user. |
124
|
|
|
* Add more options if you want to add support for custom methods. |
125
|
|
|
* [ |
126
|
|
|
* '-h' => 'getHelpMsg', |
127
|
|
|
* '--help' => 'getHelpMsg', |
128
|
|
|
* ] |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getUserMethods(): array |
133
|
|
|
{ |
134
|
|
|
// Adding extra method |
135
|
|
|
$methods = parent::getUserMethods(); |
136
|
|
|
$methods['-t'] = 'getTablesMsg'; |
137
|
|
|
$methods['--tables'] = 'getTablesMsg'; |
138
|
|
|
$methods['-g'] = 'generateModel'; |
139
|
|
|
$methods['--gen'] = 'generateModel'; |
140
|
|
|
return $methods; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Check if options are looking for help. |
145
|
|
|
* |
146
|
|
|
* @param array $params |
147
|
|
|
* |
148
|
|
|
* @return int |
149
|
|
|
*/ |
150
|
|
|
private function checkOptions(array $params = []): int |
151
|
|
|
{ |
152
|
|
|
if (isset($params[0])) { |
153
|
|
|
switch ($params[0]) { |
154
|
|
|
case '-h': |
155
|
|
|
case '--help': |
156
|
|
|
$this->showMessage($this->getHelpMsg()); |
157
|
|
|
return -1; |
158
|
|
|
case '-t': |
159
|
|
|
case '--tables': |
160
|
|
|
$this->setDataBase($params[1]); |
161
|
|
|
$this->showMessage($this->getTablesMsg()); |
162
|
|
|
return -1; |
163
|
|
|
case '-g': |
164
|
|
|
case '--gen': |
165
|
|
|
$this->setTableName(isset($params[1]) ? $params[1] : ''); |
166
|
|
|
$this->setDstFolder(\FS_FOLDER . (isset($params[2]) ? $params[2] : 'Core/Table')); |
167
|
|
|
$this->setDataBase(isset($params[3]) ? $params[3] : null); |
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
|
|
|
trigger_error('No table name setted.' . \PHP_EOL); |
184
|
|
|
return -1; |
185
|
|
|
} |
186
|
|
|
if (!isset($params[1])) { |
187
|
|
|
trigger_error('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() === '') { |
201
|
|
|
trigger_error('ERROR: Table name not setted.' . \PHP_EOL . \PHP_EOL); |
202
|
|
|
return self::RETURN_TABLE_NAME_NOT_SET; |
203
|
|
|
} |
204
|
|
|
if ($this->getDstFolder() === '') { |
205
|
|
|
trigger_error('ERROR: Destiny folder not setted.' . \PHP_EOL . \PHP_EOL); |
206
|
|
|
return self::RETURN_DST_FOLDER_NOT_SET; |
207
|
|
|
} |
208
|
|
|
if (!is_file($this->getDstFolder()) && !@mkdir($this->getDstFolder()) && !is_dir($this->getDstFolder())) { |
209
|
|
|
trigger_error("ERROR: Can't create folder " . $this->getDstFolder() . '.' . \PHP_EOL . \PHP_EOL); |
210
|
|
|
return self::RETURN_CANT_CREATE_FOLDER; |
211
|
|
|
} |
212
|
|
|
if (!\in_array($this->getTableName(), $this->dataBase->getTables(), false)) { |
213
|
|
|
trigger_error('ERROR: Table not exists.' . \PHP_EOL . \PHP_EOL); |
214
|
|
|
return self::RETURN_TABLE_NOT_EXISTS; |
215
|
|
|
} |
216
|
|
|
return self::RETURN_SUCCESS; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Generate model file. |
221
|
|
|
* |
222
|
|
|
* @return int |
223
|
|
|
*/ |
224
|
|
|
private function generateModel(): int |
225
|
|
|
{ |
226
|
|
|
$loader = new \Twig_Loader_Filesystem([__DIR__ . '/../Template']); |
|
|
|
|
227
|
|
|
$twig = new \Twig_Environment($loader, ['debug' => \FS_DEBUG,]); |
|
|
|
|
228
|
|
|
$twig->addExtension(new \Twig_Extension_Debug()); |
|
|
|
|
229
|
|
|
$txt = $twig->render( |
230
|
|
|
'XmlTable.xml.twig', |
231
|
|
|
['fsc' => $this] |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
$status = $this->saveFile($this->getDstFolder() . $this->getTableName() . '.xml', $txt); |
235
|
|
|
if (\is_bool($status)) { |
|
|
|
|
236
|
|
|
trigger_error("Can't save " . $this->getDstFolder() . $this->getTableName() . '.xml"' . \PHP_EOL); |
237
|
|
|
return $status; |
238
|
|
|
} |
239
|
|
|
$this->showMessage('Finished! Look at "' . $this->getDstFolder() . '"' . \PHP_EOL); |
240
|
|
|
return self::RETURN_SUCCESS; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|