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