44
55namespace GeekCell \DddBundle \Maker ;
66
7- use Doctrine \Bundle \DoctrineBundle \Repository \ServiceEntityRepository ;
87use Doctrine \ORM \QueryBuilder ;
98use Doctrine \Persistence \ManagerRegistry ;
109use GeekCell \Ddd \Contracts \Domain \Repository ;
3231use Symfony \Component \Console \Input \InputOption ;
3332use GeekCell \DddBundle \Infrastructure \Doctrine \Repository as OrmRepository ;
3433
34+ use Symfony \Component \Filesystem \Path ;
3535use function Symfony \Component \String \u ;
3636
3737const DOCTRINE_CONFIG_PATH = 'config/packages/doctrine.yaml ' ;
@@ -112,6 +112,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
112112 'Adds the suffix "Model" to the model class name ' ,
113113 null
114114 )
115+ ->addOption (
116+ 'base-path ' ,
117+ null ,
118+ InputOption::VALUE_REQUIRED ,
119+ 'Base path from which to generate model & config. ' ,
120+ null
121+ )
115122 ;
116123 }
117124
@@ -199,6 +206,14 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
199206 );
200207 $ input ->setOption ('entity ' , $ asEntity );
201208 }
209+
210+ if (null === $ input ->getOption ('base-path ' )) {
211+ $ basePath = $ io ->ask (
212+ 'Which base path should be used? Default is " ' . PathGenerator::DEFAULT_BASE_PATH . '" ' ,
213+ PathGenerator::DEFAULT_BASE_PATH ,
214+ );
215+ $ input ->setOption ('base-path ' , $ basePath );
216+ }
202217 }
203218
204219 /**
@@ -209,19 +224,20 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
209224 /** @var string $modelName */
210225 $ modelName = $ input ->getArgument ('name ' );
211226 $ suffix = $ input ->getOption ('with-suffix ' ) ? 'Model ' : '' ;
227+ $ pathGenerator = new PathGenerator ($ input ->getOption ('base-path ' ));
212228
213229 $ modelClassNameDetails = $ generator ->createClassNameDetails (
214230 $ modelName ,
215- 'Domain \\Model \\' ,
231+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\' ) ,
216232 $ suffix ,
217233 );
218234
219235 $ this ->templateVariables ['class_name ' ] = $ modelClassNameDetails ->getShortName ();
220236
221- $ identityClassNameDetails = $ this ->generateIdentity ($ modelName , $ input , $ io , $ generator );
222- $ this ->generateEntityMappings ($ modelClassNameDetails , $ input , $ io , $ generator );
237+ $ identityClassNameDetails = $ this ->generateIdentity ($ modelName , $ input , $ io , $ generator, $ pathGenerator );
238+ $ this ->generateEntityMappings ($ modelClassNameDetails , $ input , $ io , $ generator, $ pathGenerator );
223239 $ this ->generateEntity ($ modelClassNameDetails , $ input , $ generator );
224- $ this ->generateRepository ($ generator , $ input , $ modelClassNameDetails , $ identityClassNameDetails );
240+ $ this ->generateRepository ($ generator , $ input , $ pathGenerator , $ modelClassNameDetails , $ identityClassNameDetails );
225241
226242 $ this ->writeSuccessMessage ($ io );
227243 }
@@ -239,7 +255,8 @@ private function generateIdentity(
239255 string $ modelName ,
240256 InputInterface $ input ,
241257 ConsoleStyle $ io ,
242- Generator $ generator
258+ Generator $ generator ,
259+ PathGenerator $ pathGenerator
243260 ): ?ClassNameDetails {
244261 if (!$ this ->shouldGenerateIdentity ($ input )) {
245262 return null ;
@@ -251,7 +268,7 @@ private function generateIdentity(
251268 $ identityType = $ input ->getOption ('with-identity ' );
252269 $ identityClassNameDetails = $ generator ->createClassNameDetails (
253270 $ modelName ,
254- 'Domain \\Model \\ValueObject \\Identity \\' ,
271+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\ValueObject \\Identity \\' ) ,
255272 ucfirst ($ identityType ),
256273 );
257274
@@ -296,7 +313,7 @@ private function generateIdentity(
296313
297314 $ mappingTypeClassNameDetails = $ generator ->createClassNameDetails (
298315 $ modelName .ucfirst ($ identityType ),
299- 'Infrastructure \\Doctrine \\DBAL \\Type \\' ,
316+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\DBAL \\Type \\' ) ,
300317 'Type ' ,
301318 );
302319
@@ -360,12 +377,14 @@ private function generateIdentity(
360377 * @param InputInterface $input
361378 * @param ConsoleStyle $io
362379 * @param Generator $generator
380+ * @param PathGenerator $pathGenerator
363381 */
364382 private function generateEntityMappings (
365383 ClassNameDetails $ modelClassNameDetails ,
366384 InputInterface $ input ,
367385 ConsoleStyle $ io ,
368- Generator $ generator
386+ Generator $ generator ,
387+ PathGenerator $ pathGenerator
369388 ): void {
370389 if (!$ this ->shouldGenerateEntity ($ input )) {
371390 return ;
@@ -378,7 +397,7 @@ private function generateEntityMappings(
378397 $ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
379398 $ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
380399 'attribute ' ,
381- '%kernel.project_dir%/src/ Domain/Model ' ,
400+ $ pathGenerator -> path ( '%kernel.project_dir%/src ' , ' Domain/Model ') ,
382401 );
383402 $ generator ->dumpFile (DOCTRINE_CONFIG_PATH , $ newYaml );
384403 $ this ->classesToImport [] = ['Doctrine \\ORM \\Mapping ' => 'ORM ' ];
@@ -403,7 +422,7 @@ private function generateEntityMappings(
403422 $ this ->templateVariables ['as_entity ' ] = false ;
404423
405424 try {
406- $ mappingsDirectory = '/src/ Infrastructure/Doctrine/ORM/Mapping ' ;
425+ $ mappingsDirectory = $ pathGenerator -> path ( '/src ' , ' Infrastructure/Doctrine/ORM/Mapping ') ;
407426 $ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
408427 $ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
409428 'xml ' ,
@@ -417,6 +436,7 @@ private function generateEntityMappings(
417436 $ mappingsDirectory ,
418437 $ modelName
419438 );
439+
420440 $ generator ->generateFile (
421441 $ targetPath ,
422442 __DIR__ .'/../Resources/skeleton/doctrine/Mapping.tpl.xml.php ' ,
@@ -443,6 +463,7 @@ private function generateEntityMappings(
443463 * @param ClassNameDetails $modelClassNameDetails
444464 * @param InputInterface $input
445465 * @param Generator $generator
466+ * @throws \Exception
446467 */
447468 private function generateEntity (
448469 ClassNameDetails $ modelClassNameDetails ,
@@ -474,16 +495,18 @@ private function generateEntity(
474495 * @param InputInterface $input
475496 * @param ClassNameDetails $modelClassNameDetails
476497 * @param ?ClassNameDetails $identityClassNameDetails
498+ * @throws \Exception
477499 */
478500 private function generateRepository (
479501 Generator $ generator ,
480502 InputInterface $ input ,
503+ PathGenerator $ pathGenerator ,
481504 ClassNameDetails $ modelClassNameDetails ,
482505 ?ClassNameDetails $ identityClassNameDetails ,
483506 ): void {
484507 $ interfaceNameDetails = $ generator ->createClassNameDetails (
485508 $ input ->getArgument ('name ' ),
486- 'Domain \\Repository \\' ,
509+ $ pathGenerator -> namespacePrefix ( 'Domain \\Repository \\' ) ,
487510 'Repository ' ,
488511 );
489512
@@ -496,7 +519,7 @@ private function generateRepository(
496519
497520 $ implementationNameDetails = $ generator ->createClassNameDetails (
498521 $ input ->getArgument ('name ' ),
499- 'Infrastructure \\Doctrine \\ORM \\Repository \\' ,
522+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\ORM \\Repository \\' ) ,
500523 'Repository ' ,
501524 );
502525
0 commit comments