forked from FacturaScripts/fsmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModelCommand.php
More file actions
182 lines (157 loc) · 6.71 KB
/
ModelCommand.php
File metadata and controls
182 lines (157 loc) · 6.71 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
<?php
/**
* @author Carlos García Gómez <carlos@facturascripts.com>
*/
namespace fsmaker\Command\Model;
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\text;
#[AsCommand(
name: 'model',
description: 'Crea un nuevo modelo con su tabla XML'
)]
class ModelCommand extends BaseCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (!$this->requirePluginOrCore()) {
return Command::FAILURE;
}
$name = Utils::prompt(
label: 'Nombre del modelo (singular)',
placeholder: 'Ej: Cliente',
hint: 'El nombre debe empezar por mayúscula y solo puede contener letras, números y guiones bajos.',
regex: '/^[A-Z][a-zA-Z0-9_]*$/',
errorMessage: 'Inválido, debe empezar por mayúscula y solo puede contener letras, números y guiones bajos.'
);
$tableName = Utils::prompt(
label: 'Nombre de la tabla (plural)',
placeholder: 'Ej: facturascli',
hint: 'El nombre debe empezar por minuscula y solo puede contener minusculas, números y guiones bajos.',
regex: '/^[a-z][a-z0-9_]*$/',
errorMessage: 'Inválido, debe empezar por minuscula y solo puede contener minusculas, números y guiones bajos.'
);
$filePath = Utils::isCoreFolder() ? 'Core/Model/' : 'Model/';
$fileName = $filePath . $name . '.php';
Utils::createFolder($filePath);
if (file_exists($fileName)) {
Utils::echo("* El modelo " . $name . " YA EXISTE.\n");
return Command::FAILURE;
}
$fields = Column::askMulti();
FileGenerator::createModelByFields($fileName, $tableName, $fields, $name, Utils::getNamespace());
Utils::echo('* ' . $fileName . " -> OK.\n");
$tablePath = Utils::isCoreFolder() ? 'Core/Table/' : 'Table/';
$tableFilename = $tablePath . $tableName . '.xml';
Utils::createFolder($tablePath);
if (false === file_exists($tableFilename)) {
FileGenerator::createTableXmlByFields($tableFilename, $tableName, $fields);
Utils::echo('* ' . $tableFilename . " -> OK.\n");
} else {
Utils::echo("\n" . '* ' . $tableFilename . " YA EXISTE");
}
Utils::echo("\n");
if (Utils::promptYesOrNo('¿Crear EditController? (No - predeterminado)') === 'si') {
$this->createEditController($name, $fields);
}
Utils::echo("\n");
if (Utils::promptYesOrNo('¿Crear ListController? (No - predeterminado)') === 'si') {
$this->createListController($name, $fields);
}
return Command::SUCCESS;
}
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 = \Laravel\Prompts\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");
}
}