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 ;
3130use Symfony \Component \Console \Input \InputInterface ;
3231use Symfony \Component \Console \Input \InputOption ;
3332use GeekCell \DddBundle \Infrastructure \Doctrine \Repository as OrmRepository ;
34-
3533use function Symfony \Component \String \u ;
3634
3735const DOCTRINE_CONFIG_PATH = 'config/packages/doctrine.yaml ' ;
@@ -112,6 +110,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
112110 'Adds the suffix "Model" to the model class name ' ,
113111 null
114112 )
113+ ->addOption (
114+ 'base-path ' ,
115+ null ,
116+ InputOption::VALUE_REQUIRED ,
117+ 'Base path from which to generate model & config. ' ,
118+ null
119+ )
115120 ;
116121 }
117122
@@ -199,6 +204,14 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
199204 );
200205 $ input ->setOption ('entity ' , $ asEntity );
201206 }
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+ }
202215 }
203216
204217 /**
@@ -209,19 +222,20 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
209222 /** @var string $modelName */
210223 $ modelName = $ input ->getArgument ('name ' );
211224 $ suffix = $ input ->getOption ('with-suffix ' ) ? 'Model ' : '' ;
225+ $ pathGenerator = new PathGenerator ($ input ->getOption ('base-path ' ));
212226
213227 $ modelClassNameDetails = $ generator ->createClassNameDetails (
214228 $ modelName ,
215- 'Domain \\Model \\' ,
229+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\' ) ,
216230 $ suffix ,
217231 );
218232
219233 $ this ->templateVariables ['class_name ' ] = $ modelClassNameDetails ->getShortName ();
220234
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 );
223237 $ this ->generateEntity ($ modelClassNameDetails , $ input , $ generator );
224- $ this ->generateRepository ($ generator , $ input , $ modelClassNameDetails , $ identityClassNameDetails );
238+ $ this ->generateRepository ($ generator , $ input , $ pathGenerator , $ modelClassNameDetails , $ identityClassNameDetails );
225239
226240 $ this ->writeSuccessMessage ($ io );
227241 }
@@ -239,7 +253,8 @@ private function generateIdentity(
239253 string $ modelName ,
240254 InputInterface $ input ,
241255 ConsoleStyle $ io ,
242- Generator $ generator
256+ Generator $ generator ,
257+ PathGenerator $ pathGenerator
243258 ): ?ClassNameDetails {
244259 if (!$ this ->shouldGenerateIdentity ($ input )) {
245260 return null ;
@@ -251,7 +266,7 @@ private function generateIdentity(
251266 $ identityType = $ input ->getOption ('with-identity ' );
252267 $ identityClassNameDetails = $ generator ->createClassNameDetails (
253268 $ modelName ,
254- 'Domain \\Model \\ValueObject \\Identity \\' ,
269+ $ pathGenerator -> namespacePrefix ( 'Domain \\Model \\ValueObject \\Identity \\' ) ,
255270 ucfirst ($ identityType ),
256271 );
257272
@@ -296,7 +311,7 @@ private function generateIdentity(
296311
297312 $ mappingTypeClassNameDetails = $ generator ->createClassNameDetails (
298313 $ modelName .ucfirst ($ identityType ),
299- 'Infrastructure \\Doctrine \\DBAL \\Type \\' ,
314+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\DBAL \\Type \\' ) ,
300315 'Type ' ,
301316 );
302317
@@ -360,12 +375,14 @@ private function generateIdentity(
360375 * @param InputInterface $input
361376 * @param ConsoleStyle $io
362377 * @param Generator $generator
378+ * @param PathGenerator $pathGenerator
363379 */
364380 private function generateEntityMappings (
365381 ClassNameDetails $ modelClassNameDetails ,
366382 InputInterface $ input ,
367383 ConsoleStyle $ io ,
368- Generator $ generator
384+ Generator $ generator ,
385+ PathGenerator $ pathGenerator
369386 ): void {
370387 if (!$ this ->shouldGenerateEntity ($ input )) {
371388 return ;
@@ -378,7 +395,7 @@ private function generateEntityMappings(
378395 $ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
379396 $ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
380397 'attribute ' ,
381- '%kernel.project_dir%/src/ Domain/Model ' ,
398+ $ pathGenerator -> path ( '%kernel.project_dir%/src ' , ' Domain/Model ') ,
382399 );
383400 $ generator ->dumpFile (DOCTRINE_CONFIG_PATH , $ newYaml );
384401 $ this ->classesToImport [] = ['Doctrine \\ORM \\Mapping ' => 'ORM ' ];
@@ -403,7 +420,7 @@ private function generateEntityMappings(
403420 $ this ->templateVariables ['as_entity ' ] = false ;
404421
405422 try {
406- $ mappingsDirectory = '/src/ Infrastructure/Doctrine/ORM/Mapping ' ;
423+ $ mappingsDirectory = $ pathGenerator -> path ( '/src ' , ' Infrastructure/Doctrine/ORM/Mapping ') ;
407424 $ newYaml = $ this ->doctrineUpdater ->updateORMDefaultEntityMapping (
408425 $ this ->fileManager ->getFileContents (DOCTRINE_CONFIG_PATH ),
409426 'xml ' ,
@@ -417,6 +434,7 @@ private function generateEntityMappings(
417434 $ mappingsDirectory ,
418435 $ modelName
419436 );
437+
420438 $ generator ->generateFile (
421439 $ targetPath ,
422440 __DIR__ .'/../Resources/skeleton/doctrine/Mapping.tpl.xml.php ' ,
@@ -443,6 +461,7 @@ private function generateEntityMappings(
443461 * @param ClassNameDetails $modelClassNameDetails
444462 * @param InputInterface $input
445463 * @param Generator $generator
464+ * @throws \Exception
446465 */
447466 private function generateEntity (
448467 ClassNameDetails $ modelClassNameDetails ,
@@ -474,16 +493,18 @@ private function generateEntity(
474493 * @param InputInterface $input
475494 * @param ClassNameDetails $modelClassNameDetails
476495 * @param ?ClassNameDetails $identityClassNameDetails
496+ * @throws \Exception
477497 */
478498 private function generateRepository (
479499 Generator $ generator ,
480500 InputInterface $ input ,
501+ PathGenerator $ pathGenerator ,
481502 ClassNameDetails $ modelClassNameDetails ,
482503 ?ClassNameDetails $ identityClassNameDetails ,
483504 ): void {
484505 $ interfaceNameDetails = $ generator ->createClassNameDetails (
485506 $ input ->getArgument ('name ' ),
486- 'Domain \\Repository \\' ,
507+ $ pathGenerator -> namespacePrefix ( 'Domain \\Repository \\' ) ,
487508 'Repository ' ,
488509 );
489510
@@ -496,7 +517,7 @@ private function generateRepository(
496517
497518 $ implementationNameDetails = $ generator ->createClassNameDetails (
498519 $ input ->getArgument ('name ' ),
499- 'Infrastructure \\Doctrine \\ORM \\Repository \\' ,
520+ $ pathGenerator -> namespacePrefix ( 'Infrastructure \\Doctrine \\ORM \\Repository \\' ) ,
500521 'Repository ' ,
501522 );
502523
0 commit comments