forked from FacturaScripts/fsmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerCommand.php
More file actions
230 lines (198 loc) · 8.19 KB
/
ControllerCommand.php
File metadata and controls
230 lines (198 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
namespace fsmaker\Command\Controller;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use fsmaker\Console\BaseCommand;
use fsmaker\Column;
use fsmaker\FileGenerator;
use fsmaker\Utils;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
use function Laravel\Prompts\confirm;
#[AsCommand(
name: 'controller',
description: 'Crea un nuevo controlador (Controller, ListController o EditController)'
)]
class ControllerCommand extends BaseCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->requirePluginOrCore()) {
return Command::FAILURE;
}
$option = select(
label: 'Elija el tipo de controlador a crear',
options: [
'Controller' => 'Controller',
'ListController' => 'ListController',
'EditController' => 'EditController'
],
default: 'Controller',
scroll: 3,
required: true
);
$modelName = Utils::prompt(
label: "Nombre del $option",
placeholder: 'Ej: Producto',
hint: "El nombre del $option debe empezar por mayúscula y solo puede contener letras, números y guiones bajos, luego será colocado como 'List[Nombre elegido].php' por ejemplo.",
regex: '/^[A-Z][a-zA-Z0-9_]*$/',
errorMessage: 'Inválido, debe empezar por mayúscula y contener solo texto, números o guiones bajos.'
);
switch ($option) {
case 'Controller':
$this->createController($modelName);
return Command::SUCCESS;
case 'ListController':
$fields = Column::askMulti();
$this->createListController($modelName, $fields);
return Command::SUCCESS;
case 'EditController':
$fields = Column::askMulti();
$this->createEditController($modelName, $fields);
return Command::SUCCESS;
}
Utils::echo("Opción no válida.\n");
return Command::FAILURE;
}
private function createController(string $name): void
{
if (empty($name)) {
Utils::echo("* No introdujo el nombre del controlador.\n");
return;
}
$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . $name . '.php';
if (file_exists($fileName)) {
Utils::echo("* El controlador " . $name . " YA EXISTE.\n");
return;
}
$menu = text(
label: 'Nombre del menú',
placeholder: 'Ej: sales',
default: 'admin',
required: true,
validate: null,
hint: 'El nombre que se colocará en "$data[\'menu\'] = \'NOMBRE_ELEGIDO\';", por defecto es "admin".'
);
$createView = 'si' === Utils::promptYesOrNo(
label: '¿Desea añadir la vista twig?'
);
$samplePath = dirname(__DIR__, 3) . "/samples/Controller.php.sample";
$sample = file_get_contents($samplePath);
if (!$createView) {
$search = "\n\n \$this->view('[[NAME]].html.twig');";
$sample = str_replace($search, '', $sample);
}
$template = str_replace(['[[NAME_SPACE]]', '[[NAME]]', '[[MENU]]'], [Utils::getNamespace(), $name, $menu], $sample);
Utils::createFolder($filePath);
file_put_contents($fileName, $template);
Utils::echo('* ' . $fileName . " -> OK.\n");
if ($createView) {
$viewPath = Utils::isCoreFolder() ? 'Core/View/' : 'View/';
$viewFilename = $viewPath . $name . '.html.twig';
Utils::createFolder($viewPath);
if (file_exists($viewFilename)) {
Utils::echo('* ' . $viewFilename . " YA EXISTE.\n");
return;
}
$samplePath2 = dirname(__DIR__, 3) . "/samples/View.html.twig.sample";
$sample2 = file_get_contents($samplePath2);
$template2 = str_replace('[[NADA_A_REEMPLAZAR]]', $name, $sample2);
file_put_contents($viewFilename, $template2);
Utils::echo('* ' . $viewFilename . " -> OK.\n");
}
}
private function createEditController(string $modelName, array $fields): void
{
if (empty($modelName)) {
Utils::echo('* No introdujo el nombre del EditController');
return;
}
$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . 'Edit' . $modelName . '.php';
Utils::createFolder($filePath);
if (file_exists($fileName)) {
Utils::echo("El controlador " . $fileName . " YA EXISTE.\n");
return;
}
$menu = text(
label: 'Nombre del menú',
placeholder: 'Ej: sales',
default: 'admin',
required: true,
validate: null,
hint: 'El nombre que se colocará en "$data[\'menu\'] = \'NOMBRE_ELEGIDO\';", por defecto es "admin".'
);
$samplePath = dirname(__DIR__, 3) . "/samples/EditController.php.sample";
$sample = file_get_contents($samplePath);
$template = str_replace(
['[[NAME_SPACE]]', '[[MODEL_NAME]]', '[[MENU]]'],
[Utils::getNamespace(), $modelName, $menu],
$sample
);
file_put_contents($fileName, $template);
Utils::echo('* ' . $fileName . " -> OK.\n");
$xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/';
$xmlFilename = $xmlPath . 'Edit' . $modelName . '.xml';
Utils::createFolder($xmlPath);
if (file_exists($xmlFilename)) {
Utils::echo('* ' . $xmlFilename . " YA EXISTE\n");
return;
}
FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'edit');
Utils::echo('* ' . $xmlFilename . " -> OK.\n");
}
private function createListController(string $modelName, array $fields): void
{
if (empty($modelName)) {
Utils::echo('* No introdujo el nombre del ListController');
return;
}
$menu = text(
label: 'Nombre del menú',
placeholder: 'Ej: sales',
default: 'admin',
required: true,
validate: null,
hint: 'El nombre que se colocará en "$data[\'menu\'] = \'NOMBRE_ELEGIDO\';", por defecto es "admin".'
);
$title = text(
label: 'Nombre del submenú',
placeholder: 'Ej: Productos',
default: $modelName,
required: true,
validate: null,
hint: 'El nombre que se colocará en "$data[\'title\'] = \'NOMBRE_ELEGIDO\';", (Si tienes traducciones coloca la key de la traducción).'
);
$filePath = Utils::isCoreFolder() ? 'Core/Controller/' : 'Controller/';
$fileName = $filePath . 'List' . $modelName . '.php';
Utils::createFolder($filePath);
if (file_exists($fileName)) {
Utils::echo("El controlador " . $fileName . " YA EXISTE.\n");
return;
}
$samplePath = dirname(__DIR__, 3) . "/samples/ListController.php.sample";
$sample = file_get_contents($samplePath);
$template = str_replace(
['[[NAME_SPACE]]', '[[MODEL_NAME]]', '[[MENU]]', '[[TITLE]]'],
[Utils::getNamespace(), $modelName, $menu, $title],
$sample
);
file_put_contents($fileName, $template);
Utils::echo('* ' . $fileName . " -> OK.\n");
$xmlPath = Utils::isCoreFolder() ? 'Core/XMLView/' : 'XMLView/';
$xmlFilename = $xmlPath . 'List' . $modelName . '.xml';
Utils::createFolder($xmlPath);
if (file_exists($xmlFilename)) {
Utils::echo('* ' . $xmlFilename . " YA EXISTE\n");
return;
}
FileGenerator::createXMLViewByFields($xmlFilename, $fields, 'list');
Utils::echo('* ' . $xmlFilename . " -> OK.\n");
}
}