1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Sonata Project package. |
7
|
|
|
* |
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Sonata\AdminBundle\Maker; |
15
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Command\Validators; |
17
|
|
|
use Sonata\AdminBundle\Manipulator\ServicesManipulator; |
18
|
|
|
use Sonata\AdminBundle\Model\ModelManagerInterface; |
19
|
|
|
use Symfony\Bundle\MakerBundle\ConsoleStyle; |
20
|
|
|
use Symfony\Bundle\MakerBundle\DependencyBuilder; |
21
|
|
|
use Symfony\Bundle\MakerBundle\Generator; |
22
|
|
|
use Symfony\Bundle\MakerBundle\InputConfiguration; |
23
|
|
|
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
24
|
|
|
use Symfony\Bundle\MakerBundle\Util\ClassNameDetails; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
27
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
28
|
|
|
use Symfony\Component\Console\Input\InputOption; |
29
|
|
|
use Symfony\Component\DependencyInjection\Container; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @author Gaurav Singh Faujdar <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
final class AdminMaker extends AbstractMaker |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $projectDirectory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string[] |
43
|
|
|
*/ |
44
|
|
|
private $availableModelManagers; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $skeletonDirectory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private $modelClass; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $modelClassBasename; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $adminClassBasename; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
private $controllerClassBasename; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var string |
73
|
|
|
*/ |
74
|
|
|
private $managerType; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var ModelManagerInterface |
78
|
|
|
*/ |
79
|
|
|
private $modelManager; |
80
|
|
|
|
81
|
|
|
public function __construct($projectDirectory, array $modelManagers = []) |
82
|
|
|
{ |
83
|
|
|
$this->projectDirectory = $projectDirectory; |
84
|
|
|
$this->availableModelManagers = $modelManagers; |
85
|
|
|
$this->skeletonDirectory = sprintf('%s/../Resources/skeleton', __DIR__); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public static function getCommandName(): string |
89
|
|
|
{ |
90
|
|
|
return 'make:sonata:admin'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
94
|
|
|
{ |
95
|
|
|
$command |
96
|
|
|
->setDescription('Generates an admin class based on the given model class') |
97
|
|
|
->addArgument('model', InputArgument::REQUIRED, 'The fully qualified model class') |
98
|
|
|
->addOption('admin', 'a', InputOption::VALUE_OPTIONAL, 'The admin class basename') |
99
|
|
|
->addOption('controller', 'c', InputOption::VALUE_OPTIONAL, 'The controller class basename') |
100
|
|
|
->addOption('manager', 'm', InputOption::VALUE_OPTIONAL, 'The model manager type') |
101
|
|
|
->addOption('services', 's', InputOption::VALUE_OPTIONAL, 'The services YAML file', 'services.yaml') |
102
|
|
|
->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'The admin service ID'); |
103
|
|
|
|
104
|
|
|
$inputConfig->setArgumentAsNonInteractive('model'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
108
|
|
|
{ |
109
|
|
|
$io->section('Welcome to the Sonata Admin'); |
110
|
|
|
$this->modelClass = $io->ask( |
111
|
|
|
'The fully qualified model class', |
112
|
|
|
$input->getArgument('model'), |
|
|
|
|
113
|
|
|
[Validators::class, 'validateClass'] |
114
|
|
|
); |
115
|
|
|
$this->modelClassBasename = current(\array_slice(explode('\\', $this->modelClass), -1)); |
116
|
|
|
|
117
|
|
|
$this->adminClassBasename = $io->ask( |
118
|
|
|
'The admin class basename', |
119
|
|
|
$input->getOption('admin') ?: sprintf('%sAdmin', $this->modelClassBasename), |
120
|
|
|
[Validators::class, 'validateAdminClassBasename'] |
121
|
|
|
); |
122
|
|
|
if (\count($this->availableModelManagers) > 1) { |
123
|
|
|
$managerTypes = array_keys($this->availableModelManagers); |
124
|
|
|
$this->managerType = $io->choice('The manager type', $managerTypes, $managerTypes[0]); |
125
|
|
|
|
126
|
|
|
$input->setOption('manager', $this->managerType); |
127
|
|
|
} |
128
|
|
|
if ($io->confirm('Do you want to generate a controller?', false)) { |
129
|
|
|
$this->controllerClassBasename = $io->ask( |
130
|
|
|
'The controller class basename', |
131
|
|
|
$input->getOption('controller') ?: sprintf('%sAdminController', $this->modelClassBasename), |
132
|
|
|
[Validators::class, 'validateControllerClassBasename'] |
133
|
|
|
); |
134
|
|
|
$input->setOption('controller', $this->controllerClassBasename); |
135
|
|
|
} |
136
|
|
|
$input->setOption('services', false); |
137
|
|
|
if ($io->confirm('Do you want to update the services YAML configuration file?', true)) { |
138
|
|
|
$path = sprintf('%s/config/', $this->projectDirectory); |
139
|
|
|
$servicesFile = $io->ask( |
140
|
|
|
'The services YAML configuration file', |
141
|
|
|
is_file($path.'admin.yaml') ? 'admin.yaml' : 'services.yaml', |
142
|
|
|
[Validators::class, 'validateServicesFile'] |
143
|
|
|
); |
144
|
|
|
$id = $io->ask( |
145
|
|
|
'The admin service ID', |
146
|
|
|
$this->getAdminServiceId($this->adminClassBasename), |
147
|
|
|
[Validators::class, 'validateServiceId'] |
148
|
|
|
); |
149
|
|
|
$input->setOption('services', $servicesFile); |
150
|
|
|
$input->setOption('id', $id); |
151
|
|
|
} |
152
|
|
|
$input->setArgument('model', $this->modelClass); |
153
|
|
|
$input->setOption('admin', $this->adminClassBasename); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Configure any library dependencies that your maker requires. |
158
|
|
|
*/ |
159
|
|
|
public function configureDependencies(DependencyBuilder $dependencies): void |
160
|
|
|
{ |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Called after normal code generation: allows you to do anything. |
165
|
|
|
*/ |
166
|
|
|
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
167
|
|
|
{ |
168
|
|
|
$this->configure($input); |
169
|
|
|
|
170
|
|
|
$adminClassNameDetails = $generator->createClassNameDetails( |
171
|
|
|
$this->adminClassBasename, |
172
|
|
|
'Admin\\', |
173
|
|
|
'Admin' |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
$adminClassFullName = $adminClassNameDetails->getFullName(); |
177
|
|
|
$this->generateAdmin($io, $generator, $adminClassNameDetails); |
178
|
|
|
|
179
|
|
|
$controllerClassFullName = ''; |
180
|
|
|
if ($this->controllerClassBasename) { |
181
|
|
|
$controllerClassNameDetails = $generator->createClassNameDetails( |
182
|
|
|
$this->controllerClassBasename, |
183
|
|
|
'Controller\\', |
184
|
|
|
'Controller' |
185
|
|
|
); |
186
|
|
|
|
187
|
|
|
$this->generateController($io, $generator, $controllerClassNameDetails); |
188
|
|
|
|
189
|
|
|
$controllerClassFullName = $controllerClassNameDetails->getFullName(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
$this->generateService($input, $io, $adminClassFullName, $controllerClassFullName); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
private function getAdminServiceId(string $adminClassBasename): string |
196
|
|
|
{ |
197
|
|
|
return Container::underscore(sprintf( |
198
|
|
|
'admin.%s', |
199
|
|
|
str_replace('\\', '.', 'Admin' === substr($adminClassBasename, -5) ? |
200
|
|
|
substr($adminClassBasename, 0, -5) : $adminClassBasename) |
201
|
|
|
)); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function generateService( |
205
|
|
|
InputInterface $input, |
206
|
|
|
ConsoleStyle $io, |
207
|
|
|
string $adminClassFullName, |
208
|
|
|
string $controllerClassFullName |
209
|
|
|
): void { |
210
|
|
|
if ($servicesFile = $input->getOption('services')) { |
211
|
|
|
$file = sprintf('%s/config/%s', $this->projectDirectory, $servicesFile); |
212
|
|
|
$servicesManipulator = new ServicesManipulator($file); |
213
|
|
|
$controllerName = $this->controllerClassBasename ? $controllerClassFullName : '~'; |
214
|
|
|
|
215
|
|
|
$id = $input->getOption('id') ?: |
216
|
|
|
$this->getAdminServiceId($this->adminClassBasename); |
217
|
|
|
|
218
|
|
|
$servicesManipulator->addResource( |
219
|
|
|
$id, |
220
|
|
|
$this->modelClass, |
221
|
|
|
$adminClassFullName, |
222
|
|
|
$controllerName, |
223
|
|
|
substr($this->managerType, \strlen('sonata.admin.manager.')) |
224
|
|
|
); |
225
|
|
|
|
226
|
|
|
$io->writeln(sprintf( |
227
|
|
|
'%sThe service "<info>%s</info>" has been appended to the file <info>"%s</info>".', |
228
|
|
|
PHP_EOL, |
229
|
|
|
$id, |
230
|
|
|
realpath($file) |
231
|
|
|
)); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
private function generateController( |
236
|
|
|
ConsoleStyle $io, |
237
|
|
|
Generator $generator, |
238
|
|
|
ClassNameDetails $controllerClassNameDetails |
239
|
|
|
): void { |
240
|
|
|
$controllerClassFullName = $controllerClassNameDetails->getFullName(); |
241
|
|
|
$generator->generateClass( |
242
|
|
|
$controllerClassFullName, |
243
|
|
|
sprintf('%s/AdminController.tpl.php', $this->skeletonDirectory), |
244
|
|
|
[] |
245
|
|
|
); |
246
|
|
|
$generator->writeChanges(); |
247
|
|
|
$io->writeln(sprintf( |
248
|
|
|
'%sThe controller class "<info>%s</info>" has been generated under the file "<info>%s</info>".', |
249
|
|
|
PHP_EOL, |
250
|
|
|
$controllerClassNameDetails->getShortName(), |
251
|
|
|
$controllerClassFullName |
252
|
|
|
)); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
private function generateAdmin( |
256
|
|
|
ConsoleStyle $io, |
257
|
|
|
Generator $generator, |
258
|
|
|
ClassNameDetails $adminClassNameDetails |
259
|
|
|
): void { |
260
|
|
|
$adminClassFullName = $adminClassNameDetails->getFullName(); |
261
|
|
|
|
262
|
|
|
$fields = $this->modelManager->getExportFields($this->modelClass); |
263
|
|
|
$fieldString = ''; |
264
|
|
|
foreach ($fields as $field) { |
265
|
|
|
$fieldString = $fieldString.sprintf('%12s', '')."->add('".$field."')".PHP_EOL; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$fieldString .= sprintf('%12s', ''); |
269
|
|
|
|
270
|
|
|
$generator->generateClass( |
271
|
|
|
$adminClassFullName, |
272
|
|
|
sprintf('%s/Admin.tpl.php', $this->skeletonDirectory), |
273
|
|
|
['fields' => $fieldString] |
274
|
|
|
); |
275
|
|
|
|
276
|
|
|
$generator->writeChanges(); |
277
|
|
|
|
278
|
|
|
$io->writeln(sprintf( |
279
|
|
|
'%sThe admin class "<info>%s</info>" has been generated under the file "<info>%s</info>".', |
280
|
|
|
PHP_EOL, |
281
|
|
|
$adminClassNameDetails->getShortName(), |
282
|
|
|
$adminClassFullName |
283
|
|
|
)); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
private function configure(InputInterface $input): void |
287
|
|
|
{ |
288
|
|
|
$this->modelClass = Validators::validateClass($input->getArgument('model')); |
|
|
|
|
289
|
|
|
$this->modelClassBasename = (new \ReflectionClass($this->modelClass))->getShortName(); |
290
|
|
|
$this->adminClassBasename = Validators::validateAdminClassBasename( |
291
|
|
|
$input->getOption('admin') ?: sprintf('%sAdmin', $this->modelClassBasename) |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
if ($this->controllerClassBasename = $input->getOption('controller')) { |
|
|
|
|
295
|
|
|
$this->controllerClassBasename = Validators::validateControllerClassBasename($this->controllerClassBasename); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
if (0 === \count($this->availableModelManagers)) { |
299
|
|
|
throw new \InvalidArgumentException('There are no model managers registered.'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$this->managerType = $input->getOption('manager') ?: array_keys($this->availableModelManagers)[0]; |
303
|
|
|
$this->modelManager = $this->availableModelManagers[$this->managerType] ?: current($this->availableModelManagers); |
|
|
|
|
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.