Skip to content

Commit 4673b6d

Browse files
author
janvt
authored
feat: add base-path
2 parents 977ca21 + 7c92a52 commit 4673b6d

8 files changed

+242
-46
lines changed

.github/workflows/test.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,15 @@ jobs:
1818
- uses: php-actions/composer@v6
1919
- name: PHPUnit Tests
2020
uses: php-actions/phpunit@v3
21+
env:
22+
XDEBUG_MODE: coverage
2123
with:
2224
version: 9.6.5
2325
php_version: 8.2.3
2426
configuration: phpunit.xml
2527
php_extensions: "xdebug"
26-
coverage-text: true
27-
coverage-clover: reports/coverage.xml
28+
coverage_text: true
29+
coverage_clover: reports/coverage.xml
2830

2931
# SonarCloud
3032
- name: SonarCloud Scan

sonar-project.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ sonar.projectKey=geekcell_ddd-symfony-bundle
22
sonar.organization=geekcell
33

44
sonar.sources=src
5-
sonar.exclusions=tests/**
5+
sonar.exclusions=tests/**, src/Maker/Make*, src/Maker/AbstractBaseMakerCQRS.php
66
sonar.tests=tests
77
sonar.php.coverage.reportPaths=reports/coverage.xml

src/Maker/AbstractBaseMakerCQRS.php

+30-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Console\Command\Command;
1616
use Symfony\Component\Console\Input\InputArgument;
1717
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
1819

1920
abstract class AbstractBaseMakerCQRS extends AbstractMaker implements InputAwareMakerInterface
2021
{
@@ -45,11 +46,12 @@ function getClassSuffix(): string
4546
}
4647

4748
/**
49+
* @param PathGenerator $pathGenerator
4850
* @return string
4951
*/
50-
function getNamespacePrefix(): string
52+
function getNamespacePrefix(PathGenerator $pathGenerator): string
5153
{
52-
return 'Application\\' . $this->getClassSuffix() . '\\';
54+
return $pathGenerator->namespacePrefix('Application\\' . $this->getClassSuffix() . '\\');
5355
}
5456

5557
/**
@@ -63,6 +65,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
6365
InputArgument::REQUIRED,
6466
'The name of the ' . $this->getTarget() . ' class (e.g. <fg=yellow>Customer</>)',
6567
)
68+
->addOption(
69+
'base-path',
70+
null,
71+
InputOption::VALUE_REQUIRED,
72+
'Base path from which to generate model & config.',
73+
null
74+
)
6675
;
6776
}
6877

@@ -78,21 +87,30 @@ public function configureDependencies(DependencyBuilder $dependencies, InputInte
7887
*/
7988
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void
8089
{
90+
if (null === $input->getOption('base-path')) {
91+
$basePath = $io->ask(
92+
'Which base path should be used? Default is "' . PathGenerator::DEFAULT_BASE_PATH . '"',
93+
PathGenerator::DEFAULT_BASE_PATH,
94+
);
95+
$input->setOption('base-path', $basePath);
96+
}
8197
}
8298

8399
/**
84100
* @inheritDoc
85101
*/
86102
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
87103
{
104+
$pathGenerator = new PathGenerator($input->getOption('base-path'));
105+
88106
$entityClassNameDetails = $generator->createClassNameDetails(
89107
$input->getArgument('name'),
90-
$this->getNamespacePrefix(),
108+
$this->getNamespacePrefix($pathGenerator),
91109
$this->getClassSuffix(),
92110
);
93111

94112
$this->generateEntity($entityClassNameDetails, $generator);
95-
$this->generateHandler($entityClassNameDetails, $generator);
113+
$this->generateHandler($entityClassNameDetails, $generator, $pathGenerator);
96114

97115
$this->writeSuccessMessage($io);
98116
}
@@ -101,6 +119,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
101119
* @param ClassNameDetails $queryClassNameDetails
102120
* @param Generator $generator
103121
* @return void
122+
* @throws \Exception
104123
*/
105124
private function generateEntity(ClassNameDetails $queryClassNameDetails, Generator $generator): void
106125
{
@@ -122,12 +141,17 @@ private function generateEntity(ClassNameDetails $queryClassNameDetails, Generat
122141
* @param ClassNameDetails $queryClassNameDetails
123142
* @param Generator $generator
124143
* @return void
144+
* @throws \Exception
125145
*/
126-
private function generateHandler(ClassNameDetails $queryClassNameDetails, Generator $generator): void
146+
private function generateHandler(
147+
ClassNameDetails $queryClassNameDetails,
148+
Generator $generator,
149+
PathGenerator $pathGenerator
150+
): void
127151
{
128152
$classNameDetails = $generator->createClassNameDetails(
129153
$queryClassNameDetails->getShortName(),
130-
$this->getNamespacePrefix(),
154+
$this->getNamespacePrefix($pathGenerator),
131155
'Handler',
132156
);
133157

src/Maker/MakeController.php

+19-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ public function configureCommand(Command $command, InputConfiguration $inputConf
7373
'Add a command bus dependency.',
7474
null
7575
)
76+
->addOption(
77+
'base-path',
78+
null,
79+
InputOption::VALUE_REQUIRED,
80+
'Base path from which to generate model & config.',
81+
null
82+
)
7683
;
7784
}
7885

@@ -103,16 +110,26 @@ public function interact(InputInterface $input, ConsoleStyle $io, Command $comma
103110
);
104111
$input->setOption('include-command-bus', $includeCommandBus);
105112
}
113+
114+
if (null === $input->getOption('base-path')) {
115+
$basePath = $io->ask(
116+
'Which base path should be used? Default is "' . PathGenerator::DEFAULT_BASE_PATH . '"',
117+
PathGenerator::DEFAULT_BASE_PATH,
118+
);
119+
$input->setOption('base-path', $basePath);
120+
}
106121
}
107122

108123
/**
109124
* @inheritDoc
110125
*/
111126
public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void
112127
{
128+
$pathGenerator = new PathGenerator($input->getOption('base-path'));
129+
113130
$classNameDetails = $generator->createClassNameDetails(
114131
$input->getArgument('name'),
115-
self::NAMESPACE_PREFIX,
132+
$pathGenerator->namespacePrefix(self::NAMESPACE_PREFIX),
116133
'Controller',
117134
);
118135

@@ -155,7 +172,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
155172
self::CONFIG_PATH,
156173
$templatePathConfig,
157174
[
158-
'path' => '../../src/Infrastructure/Http/Controller/',
175+
'path' => $pathGenerator->path('../../src/', 'Infrastructure/Http/Controller/'),
159176
'namespace' => str_replace('\\' . $classNameDetails->getShortName(), '', $classNameDetails->getFullName())
160177
]
161178
);

src/Maker/MakeModel.php

+35-14
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace GeekCell\DddBundle\Maker;
66

7-
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
87
use Doctrine\ORM\QueryBuilder;
98
use Doctrine\Persistence\ManagerRegistry;
109
use GeekCell\Ddd\Contracts\Domain\Repository;
@@ -31,7 +30,6 @@
3130
use Symfony\Component\Console\Input\InputInterface;
3231
use Symfony\Component\Console\Input\InputOption;
3332
use GeekCell\DddBundle\Infrastructure\Doctrine\Repository as OrmRepository;
34-
3533
use function Symfony\Component\String\u;
3634

3735
const 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

Comments
 (0)