forked from sonata-project/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdminMaker.php
301 lines (265 loc) · 10.7 KB
/
AdminMaker.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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Maker;
use Sonata\AdminBundle\Command\Validators;
use Sonata\AdminBundle\Manipulator\ServicesManipulator;
use Sonata\AdminBundle\Model\ModelManagerInterface;
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\Bundle\MakerBundle\Util\ClassNameDetails;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\DependencyInjection\Container;
/**
* @author Gaurav Singh Faujdar <[email protected]>
*/
final class AdminMaker extends AbstractMaker
{
/**
* @var string
*/
private $projectDirectory;
/**
* @var string[]
*/
private $availableModelManagers;
/**
* @var string
*/
private $skeletonDirectory;
/**
* @var string
*/
private $modelClass;
/**
* @var string
*/
private $modelClassBasename;
/**
* @var string
*/
private $adminClassBasename;
/**
* @var string
*/
private $controllerClassBasename;
/**
* @var string
*/
private $managerType;
/**
* @var ModelManagerInterface
*/
private $modelManager;
public function __construct($projectDirectory, array $modelManagers = [])
{
$this->projectDirectory = $projectDirectory;
$this->availableModelManagers = $modelManagers;
$this->skeletonDirectory = __DIR__.'/../Resources/skeleton';
}
public static function getCommandName(): string
{
return 'make:sonata:admin';
}
public function configureCommand(Command $command, InputConfiguration $inputConfig): void
{
$command
->setDescription('Generates an admin class based on the given model class')
->addArgument('model', InputArgument::REQUIRED, 'The fully qualified model class')
->addOption('admin', 'a', InputOption::VALUE_OPTIONAL, 'The admin class basename')
->addOption('controller', 'c', InputOption::VALUE_OPTIONAL, 'The controller class basename')
->addOption('manager', 'm', InputOption::VALUE_OPTIONAL, 'The model manager type')
->addOption('services', 's', InputOption::VALUE_OPTIONAL, 'The services YAML file', 'services.yaml')
->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'The admin service ID');
$inputConfig->setArgumentAsNonInteractive('model');
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
{
$io->section('Welcome to the Sonata Admin');
$this->modelClass = $io->ask(
'The fully qualified model class',
$input->getArgument('model'),
[Validators::class, 'validateClass']
);
$this->modelClassBasename = current(\array_slice(explode('\\', $this->modelClass), -1));
$this->adminClassBasename = $io->ask(
'The admin class basename',
$input->getOption('admin') ?: $this->modelClassBasename.'Admin',
[Validators::class, 'validateAdminClassBasename']
);
if (\count($this->availableModelManagers) > 1) {
$managerTypes = array_keys($this->availableModelManagers);
$this->managerType = $io->choice('The manager type', $managerTypes, $managerTypes[0]);
$input->setOption('manager', $this->managerType);
}
if ($io->confirm('Do you want to generate a controller?', false)) {
$this->controllerClassBasename = $io->ask(
'The controller class basename',
$input->getOption('controller') ?: $this->modelClassBasename.'AdminController',
[Validators::class, 'validateControllerClassBasename']
);
$input->setOption('controller', $this->controllerClassBasename);
}
$input->setOption('services', false);
if ($io->confirm('Do you want to update the services YAML configuration file?', true)) {
$path = $this->projectDirectory.'/config/';
$servicesFile = $io->ask(
'The services YAML configuration file',
is_file($path.'admin.yaml') ? 'admin.yaml' : 'services.yaml',
[Validators::class, 'validateServicesFile']
);
$id = $io->ask(
'The admin service ID',
$this->getAdminServiceId($this->adminClassBasename),
[Validators::class, 'validateServiceId']
);
$input->setOption('services', $servicesFile);
$input->setOption('id', $id);
}
$input->setArgument('model', $this->modelClass);
$input->setOption('admin', $this->adminClassBasename);
}
/**
* Configure any library dependencies that your maker requires.
*/
public function configureDependencies(DependencyBuilder $dependencies): void
{
}
/**
* Called after normal code generation: allows you to do anything.
*/
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
{
$this->configure($input);
$adminClassNameDetails = $generator->createClassNameDetails(
$this->adminClassBasename,
'Admin\\',
'Admin'
);
$adminClassFullName = $adminClassNameDetails->getFullName();
$this->generateAdmin($io, $generator, $adminClassNameDetails);
$controllerClassFullName = '';
if ($this->controllerClassBasename) {
$controllerClassNameDetails = $generator->createClassNameDetails(
$this->controllerClassBasename,
'Controller\\',
'Controller'
);
$this->generateController($input, $io, $generator, $controllerClassNameDetails);
$controllerClassFullName = $controllerClassNameDetails->getFullName();
}
$this->generateService($input, $io, $adminClassFullName, $controllerClassFullName);
}
private function getAdminServiceId(string $adminClassBasename): string
{
return Container::underscore(sprintf(
'admin.%s',
str_replace('\\', '.', 'Admin' === substr($adminClassBasename, -5) ?
substr($adminClassBasename, 0, -5) : $adminClassBasename)
));
}
private function generateService(
InputInterface $input,
ConsoleStyle $io,
string $adminClassFullName,
string $controllerClassFullName
): void {
if ($servicesFile = $input->getOption('services')) {
$file = sprintf('%s/config/%s', $this->projectDirectory, $servicesFile);
$servicesManipulator = new ServicesManipulator($file);
$controllerName = $this->controllerClassBasename ? $controllerClassFullName : '~';
$id = $input->getOption('id') ?:
$this->getAdminServiceId('App', $this->adminClassBasename);
$servicesManipulator->addResource(
$id,
$this->modelClass,
$adminClassFullName,
$controllerName,
substr($this->managerType, \strlen('sonata.admin.manager.'))
);
$io->writeln(sprintf(
'%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".',
PHP_EOL,
$id,
realpath($file)
));
}
}
private function generateController(
InputInterface $input,
ConsoleStyle $io,
Generator $generator,
ClassNameDetails $controllerClassNameDetails
): void {
$controllerClassFullName = null;
if ($controllerClassNameDetails) {
$controllerClassFullName = $controllerClassNameDetails->getFullName();
$generator->generateClass(
$controllerClassFullName,
$this->skeletonDirectory.'/AdminController.tpl.php',
[]
);
$generator->writeChanges();
$io->writeln(sprintf(
'%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
PHP_EOL,
$controllerClassNameDetails->getShortName(),
$controllerClassFullName
));
}
}
private function generateAdmin(
ConsoleStyle $io,
Generator $generator,
ClassNameDetails $adminClassNameDetails
): void {
$adminClassFullName = $adminClassNameDetails->getFullName();
$fields = $this->modelManager->getExportFields($this->modelClass);
$fieldString = '';
foreach ($fields as $field) {
$fieldString = $fieldString.sprintf('%12s', '')."->add('".$field."')".PHP_EOL;
}
$fieldString .= sprintf('%12s', '');
$generator->generateClass(
$adminClassFullName,
$this->skeletonDirectory.'/Admin.tpl.php',
['fields' => $fieldString]
);
$generator->writeChanges();
$io->writeln(sprintf(
'%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".',
PHP_EOL,
$adminClassNameDetails->getShortName(),
$adminClassFullName
));
}
private function configure(InputInterface $input): void
{
$this->modelClass = Validators::validateClass($input->getArgument('model'));
$this->modelClassBasename = (new \ReflectionClass($this->modelClass))->getShortName();
$this->adminClassBasename = Validators::validateAdminClassBasename(
$input->getOption('admin') ?: $this->modelClassBasename.'Admin'
);
if ($this->controllerClassBasename = $input->getOption('controller')) {
$this->controllerClassBasename = Validators::validateControllerClassBasename($this->controllerClassBasename);
}
if (0 === \count($this->availableModelManagers)) {
throw new \InvalidArgumentException('There are no model managers registered.');
}
$this->managerType = $input->getOption('manager') ?: array_keys($this->availableModelManagers)[0];
$this->modelManager = $this->availableModelManagers[$this->managerType] ?: current($this->availableModelManagers);
}
}