4
4
5
5
namespace GeekCell \DddBundle \Maker ;
6
6
7
- use Doctrine \Bundle \DoctrineBundle \Repository \ServiceEntityRepository ;
8
7
use Doctrine \ORM \QueryBuilder ;
9
8
use Doctrine \Persistence \ManagerRegistry ;
10
9
use GeekCell \Ddd \Contracts \Domain \Repository ;
31
30
use Symfony \Component \Console \Input \InputInterface ;
32
31
use Symfony \Component \Console \Input \InputOption ;
33
32
use GeekCell \DddBundle \Infrastructure \Doctrine \Repository as OrmRepository ;
34
-
35
33
use function Symfony \Component \String \u ;
36
34
37
35
const DOCTRINE_CONFIG_PATH = 'config/packages/doctrine.yaml ' ;
@@ -112,6 +110,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
112
110
'Adds the suffix "Model" to the model class name ' ,
113
111
null
114
112
)
113
+ ->addOption (
114
+ 'base-path ' ,
115
+ null ,
116
+ InputOption::VALUE_REQUIRED ,
117
+ 'Base path from which to generate model & config. ' ,
118
+ null
119
+ )
115
120
;
116
121
}
117
122
@@ -199,6 +204,14 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
199
204
);
200
205
$ input ->setOption ('entity ' , $ asEntity );
201
206
}
207
+
208
+ if (null === $ input ->getOption ('base-path ' )) {
209
+ $ basePath = $ io ->ask (
210
+ 'Which base path should be used? Default is " ' . PathGenerator::DEFAULT_BASE_PATH . '" ' ,
211
+ PathGenerator::DEFAULT_BASE_PATH ,
212
+ );
213
+ $ input ->setOption ('base-path ' , $ basePath );
214
+ }
202
215
}
203
216
204
217
/**
@@ -209,19 +222,20 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
209
222
/** @var string $modelName */
210
223
$ modelName = $ input ->getArgument ('name ' );
211
224
$ suffix = $ input ->getOption ('with-suffix ' ) ? 'Model ' : '' ;
225
+ $ pathGenerator = new PathGenerator ($ input ->getOption ('base-path ' ));
212
226
213
227
$ modelClassNameDetails = $ generator ->createClassNameDetails (
214
228
$ modelName ,
215
- 'Domain \\Model \\' ,
229
+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\' ) ,
216
230
$ suffix ,
217
231
);
218
232
219
233
$ this ->templateVariables ['class_name ' ] = $ modelClassNameDetails ->getShortName ();
220
234
221
- $ identityClassNameDetails = $ this ->generateIdentity ($ modelName , $ input , $ io , $ generator );
222
- $ this ->generateEntityMappings ($ modelClassNameDetails , $ input , $ io , $ generator );
235
+ $ identityClassNameDetails = $ this ->generateIdentity ($ modelName , $ input , $ io , $ generator, $ pathGenerator );
236
+ $ this ->generateEntityMappings ($ modelClassNameDetails , $ input , $ io , $ generator, $ pathGenerator );
223
237
$ this ->generateEntity ($ modelClassNameDetails , $ input , $ generator );
224
- $ this ->generateRepository ($ generator , $ input , $ modelClassNameDetails , $ identityClassNameDetails );
238
+ $ this ->generateRepository ($ generator , $ input , $ pathGenerator , $ modelClassNameDetails , $ identityClassNameDetails );
225
239
226
240
$ this ->writeSuccessMessage ($ io );
227
241
}
@@ -239,7 +253,8 @@ private function generateIdentity(
239
253
string $ modelName ,
240
254
InputInterface $ input ,
241
255
ConsoleStyle $ io ,
242
- Generator $ generator
256
+ Generator $ generator ,
257
+ PathGenerator $ pathGenerator
243
258
): ?ClassNameDetails {
244
259
if (!$ this ->shouldGenerateIdentity ($ input )) {
245
260
return null ;
@@ -251,7 +266,7 @@ private function generateIdentity(
251
266
$ identityType = $ input ->getOption ('with-identity ' );
252
267
$ identityClassNameDetails = $ generator ->createClassNameDetails (
253
268
$ modelName ,
254
- 'Domain \\Model \\ValueObject \\Identity \\' ,
269
+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\ValueObject \\Identity \\' ) ,
255
270
ucfirst ($ identityType ),
256
271
);
257
272
@@ -296,7 +311,7 @@ private function generateIdentity(
296
311
297
312
$ mappingTypeClassNameDetails = $ generator ->createClassNameDetails (
298
313
$ modelName .ucfirst ($ identityType ),
299
- 'Infrastructure \\Doctrine \\DBAL \\Type \\' ,
314
+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\DBAL \\Type \\' ) ,
300
315
'Type ' ,
301
316
);
302
317
@@ -360,12 +375,14 @@ private function generateIdentity(
360
375
* @param InputInterface $input
361
376
* @param ConsoleStyle $io
362
377
* @param Generator $generator
378
+ * @param PathGenerator $pathGenerator
363
379
*/
364
380
private function generateEntityMappings (
365
381
ClassNameDetails $ modelClassNameDetails ,
366
382
InputInterface $ input ,
367
383
ConsoleStyle $ io ,
368
- Generator $ generator
384
+ Generator $ generator ,
385
+ PathGenerator $ pathGenerator
369
386
): void {
370
387
if (!$ this ->shouldGenerateEntity ($ input )) {
371
388
return ;
@@ -378,7 +395,7 @@ private function generateEntityMappings(
378
395
$ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
379
396
$ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
380
397
'attribute ' ,
381
- '%kernel.project_dir%/src/ Domain/Model ' ,
398
+ $ pathGenerator -> path ( '%kernel.project_dir%/src ' , ' Domain/Model ') ,
382
399
);
383
400
$ generator ->dumpFile (DOCTRINE_CONFIG_PATH , $ newYaml );
384
401
$ this ->classesToImport [] = ['Doctrine \\ORM \\Mapping ' => 'ORM ' ];
@@ -403,7 +420,7 @@ private function generateEntityMappings(
403
420
$ this ->templateVariables ['as_entity ' ] = false ;
404
421
405
422
try {
406
- $ mappingsDirectory = '/src/ Infrastructure/Doctrine/ORM/Mapping ' ;
423
+ $ mappingsDirectory = $ pathGenerator -> path ( '/src ' , ' Infrastructure/Doctrine/ORM/Mapping ') ;
407
424
$ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
408
425
$ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
409
426
'xml ' ,
@@ -417,6 +434,7 @@ private function generateEntityMappings(
417
434
$ mappingsDirectory ,
418
435
$ modelName
419
436
);
437
+
420
438
$ generator ->generateFile (
421
439
$ targetPath ,
422
440
__DIR__ .'/../Resources/skeleton/doctrine/Mapping.tpl.xml.php ' ,
@@ -443,6 +461,7 @@ private function generateEntityMappings(
443
461
* @param ClassNameDetails $modelClassNameDetails
444
462
* @param InputInterface $input
445
463
* @param Generator $generator
464
+ * @throws \Exception
446
465
*/
447
466
private function generateEntity (
448
467
ClassNameDetails $ modelClassNameDetails ,
@@ -474,16 +493,18 @@ private function generateEntity(
474
493
* @param InputInterface $input
475
494
* @param ClassNameDetails $modelClassNameDetails
476
495
* @param ?ClassNameDetails $identityClassNameDetails
496
+ * @throws \Exception
477
497
*/
478
498
private function generateRepository (
479
499
Generator $ generator ,
480
500
InputInterface $ input ,
501
+ PathGenerator $ pathGenerator ,
481
502
ClassNameDetails $ modelClassNameDetails ,
482
503
?ClassNameDetails $ identityClassNameDetails ,
483
504
): void {
484
505
$ interfaceNameDetails = $ generator ->createClassNameDetails (
485
506
$ input ->getArgument ('name ' ),
486
- 'Domain \\Repository \\' ,
507
+ $ pathGenerator -> namespacePrefix ( 'Domain \\Repository \\' ) ,
487
508
'Repository ' ,
488
509
);
489
510
@@ -496,7 +517,7 @@ private function generateRepository(
496
517
497
518
$ implementationNameDetails = $ generator ->createClassNameDetails (
498
519
$ input ->getArgument ('name ' ),
499
- 'Infrastructure \\Doctrine \\ORM \\Repository \\' ,
520
+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\ORM \\Repository \\' ) ,
500
521
'Repository ' ,
501
522
);
502
523
0 commit comments