-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathMakeAdmin.php
339 lines (304 loc) · 10.3 KB
/
MakeAdmin.php
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace Admingenerator\GeneratorBundle\Maker;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\InputConfiguration;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Question\Question;
class MakeAdmin extends AbstractMaker
{
const ACTIONS = [
'New' => ['views' => [
'index',
'form',
]],
'List' => ['views' => [
'index',
'results',
'filters',
'row',
]],
'Excel' => ['views' => []],
'Edit' => ['views' => [
'index',
'form',
]],
'Show' => ['views' => ['index']],
'Actions' => ['views' => ['index']],
];
const FORMS = ['New', 'Filters', 'Edit'];
const ORMS = ['doctrine', 'doctrine_odm', 'propel'];
public static function getCommandName(): string
{
return 'admin:generate-admin';
}
public static function getCommandDescription(): string
{
return 'Generate new admin pages given a model';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->addArgument('namespace', InputArgument::OPTIONAL, 'The namespace to place the admin', 'App')
->addArgument('sf4', InputArgument::OPTIONAL, 'Whether to use the Symfony 4 directory structure or the old bundle structure', 'Yes')
->addArgument('orm', InputArgument::OPTIONAL, 'The orm to use (propel, doctrine, doctrine_odm)', 'doctrine')
->addArgument('model-name', InputArgument::OPTIONAL, 'Base model name for admin module, without namespace.', 'YourModel')
->addArgument('prefix', InputArgument::OPTIONAL, 'The generator prefix ([prefix]-generator.yml)', 'AdminYourModel')
->setHelp(<<<EOT
The <info>admin:generate-admin</info> command helps you to generate new admin pages for a given model.
By default, the command interacts with the developer to tweak the generation.
Any passed option will be used as a default value for the interaction.
If you want to disable any user interaction, use <comment>--no-interaction</comment> but don't forget to pass all needed options.
EOT
);
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
$io->section('Welcome to the Admingenerator');
$question = new Question(
$command->getDefinition()->getArgument('namespace')->getDescription(),
$input->getArgument('namespace')
);
$question->setValidator(function ($namespace) {
return self::validateNamespace($namespace);
});
$input->setArgument('namespace', $io->askQuestion($question));
$question = new Question(
$command->getDefinition()->getArgument('orm')->getDescription(),
$input->getArgument('orm')
);
$question->setValidator(function ($orm) {
if (!in_array($orm, self::ORMS)) {
throw new \InvalidArgumentException('Use a valid generator.');
}
return $orm;
});
$question->setAutocompleterValues(self::ORMS);
$input->setArgument('orm', $io->askQuestion($question));
$question = new Question(
$command->getDefinition()->getArgument('model-name')->getDescription(),
$input->getArgument('model-name')
);
$question->setValidator(function ($modelName) {
if (empty($modelName) || preg_match('#[^a-zA-Z0-9]#', $modelName)) {
throw new \InvalidArgumentException('Model name should not contain any special characters nor spaces.');
}
return $modelName;
});
$input->setArgument('model-name', $io->askQuestion($question));
$question = new Question(
$command->getDefinition()->getArgument('prefix')->getDescription(),
$input->getArgument('prefix')
);
$question->setValidator(function ($prefix) {
if (!preg_match('/([a-z]+)/i', $prefix)) {
throw new \RuntimeException('Prefix has to be a single word');
}
return $prefix;
});
$input->setArgument('prefix', $io->askQuestion($question));
$question = new ConfirmationQuestion(
$command->getDefinition()->getArgument('sf4')->getDescription(),
$input->getArgument('sf4')
);
$input->setArgument('sf4', $io->askQuestion($question));
}
public function configureDependencies(DependencyBuilder $dependencies): void
{
}
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$orm = $input->getArgument('orm');
// Retrieve model folder depending on chosen Model Manager
$modelFolder = '';
switch ($orm) {
case 'propel':
$modelFolder = 'Model';
break;
case 'doctrine':
$modelFolder = 'Entity';
break;
case 'doctrine_odm':
$modelFolder = 'Document';
break;
}
$namespace = $input->getArgument('namespace');
$namespaceParts = explode('\\', $namespace);
$prefix = $input->getArgument('prefix');
$bundledNamespace = count($namespaceParts) > 1;
$bundleName = strtr($namespace, array('\\' => ''));
$parameters = [
'bundle' => $bundleName,
'bundleName' => $bundledNamespace ? end($namespaceParts) : $namespace,
'generator' => sprintf('admingenerator.generator.%s', $orm),
'modelFolder' => $modelFolder,
'modelName' => $input->getArgument('model-name'),
'namespace' => $namespace,
'namespacePrefix' => $bundledNamespace ? $namespaceParts[0] : '',
'prefix' => ucfirst($prefix),
];
$sf4 = boolval($input->getArgument('sf4'));
$dir = trim(strtr($namespace, '\\', '/'));
$generator->generateFile(
sprintf(
'%s/%s-generator.yml',
$sf4 ? 'config/admin' : sprintf('src/%s/Resources/config', $dir),
$prefix
),
__DIR__ . '/../Resources/skeleton/config/yml_generator.tpl.php',
$parameters
);
$dirPrefix = $prefix ? sprintf('%s/', ucfirst($prefix)) : '';
foreach (self::ACTIONS as $action => $actionProperties) {
$parameters['action'] = $action;
$generator->generateFile(
sprintf(
'src/%sController/%s%sController.php',
$sf4 ? '' : sprintf('%s/', $dir),
$dirPrefix,
$action
),
__DIR__ . '/../Resources/skeleton/controller/controller_generator.tpl.php',
$parameters
);
foreach ($actionProperties['views'] as $view) {
$parameters['view'] = $view;
$generator->generateFile(
sprintf(
'%s/%s%s/%s.html.twig',
$sf4 ? 'templates' : sprintf('src/%s/Resources/views', $dir),
$prefix,
$action,
$view
),
__DIR__ . '/../Resources/skeleton/template/twig_generator.tpl.php',
$parameters
);
}
}
foreach (self::FORMS as $form) {
$parameters['form'] = $form;
$generator->generateFile(
sprintf(
'src/%sForm/Type/%s%sType.php',
$sf4 ? '' : sprintf('%s/', $dir),
$dirPrefix,
$form
),
__DIR__ . '/../Resources/skeleton/form/type_generator.tpl.php',
$parameters
);
}
$generator->generateFile(
sprintf(
'src/%sForm/Type/%sOptions.php',
$sf4 ? '' : sprintf('%s/', $dir),
$dirPrefix,
),
__DIR__ . '/../Resources/skeleton/form/options_generator.tpl.php',
$parameters
);
$generator->writeChanges();
$io->section('Add the following to your routing file to access the admin');
$io->writeln(sprintf(" resource: \"@%s/Controller/%s/\"\n type: admingenerator\n prefix: /\n", $bundleName, ucfirst($prefix)));
}
/**
* Validates that the given namespace (e.g. Acme\Foo) is a valid format.
*/
public static function validateNamespace(string $namespace): string
{
$namespace = strtr($namespace, '/', '\\');
if (!preg_match('/^(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\?)+$/', $namespace)) {
throw new \InvalidArgumentException('The namespace contains invalid characters.');
}
// validate reserved keywords
foreach (explode('\\', $namespace) as $word) {
if (in_array(strtolower($word), self::RESERVED_KEYWORDS)) {
throw new \InvalidArgumentException(sprintf('The namespace cannot contain PHP reserved words ("%s").', $word));
}
}
return $namespace;
}
const RESERVED_KEYWORDS = [
'abstract',
'and',
'array',
'as',
'break',
'callable',
'case',
'catch',
'class',
'clone',
'const',
'continue',
'declare',
'default',
'do',
'else',
'elseif',
'enddeclare',
'endfor',
'endforeach',
'endif',
'endswitch',
'endwhile',
'extends',
'final',
'finally',
'for',
'foreach',
'function',
'global',
'goto',
'if',
'implements',
'interface',
'instanceof',
'insteadof',
'namespace',
'new',
'or',
'private',
'protected',
'public',
'static',
'switch',
'throw',
'trait',
'try',
'use',
'var',
'while',
'xor',
'yield',
'__CLASS__',
'__DIR__',
'__FILE__',
'__LINE__',
'__FUNCTION__',
'__METHOD__',
'__NAMESPACE__',
'__TRAIT__',
'__halt_compiler',
'die',
'echo',
'empty',
'exit',
'eval',
'include',
'include_once',
'isset',
'list',
'require',
'require_once',
'return',
'print',
'unset',
];
}