Skip to content

Commit 4f365a5

Browse files
authored
Merge pull request #34 from geekcell/fix/optional-identity
2 parents 84dbb1a + 94cdabc commit 4f365a5

File tree

3 files changed

+27
-24
lines changed

3 files changed

+27
-24
lines changed

src/Maker/MakeModel.php

+23-24
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
221221
$identityClassNameDetails = $this->generateIdentity($modelName, $input, $io, $generator);
222222
$this->generateEntityMappings($modelClassNameDetails, $input, $io, $generator);
223223
$this->generateEntity($modelClassNameDetails, $input, $generator);
224-
$this->generateRepository($modelClassNameDetails, $identityClassNameDetails, $input, $generator);
224+
$this->generateRepository($generator, $input, $modelClassNameDetails, $identityClassNameDetails);
225225

226226
$this->writeSuccessMessage($io);
227227
}
@@ -423,9 +423,9 @@ private function generateEntityMappings(
423423
[
424424
'model_class' => $modelClassNameDetails->getFullName(),
425425
'has_identity' => $hasIdentity,
426-
'type_name' => $this->templateVariables['type_name'],
426+
'type_name' => $hasIdentity ?? $this->templateVariables['type_name'],
427427
'table_name' => $tableName,
428-
'identity_column_name' => $this->templateVariables['identity_type'],
428+
'identity_column_name' => $hasIdentity ?? $this->templateVariables['identity_type'],
429429
],
430430
);
431431
} catch (YamlManipulationFailedException $e) {
@@ -470,17 +470,16 @@ private function generateEntity(
470470
/**
471471
* Generate model repository
472472
*
473-
* @param ClassNameDetails $modelClassNameDetails
474-
* @param ClassNameDetails $identityClassNameDetails
475-
* @param InputInterface $input
476473
* @param Generator $generator
477-
* @throws \Exception
474+
* @param InputInterface $input
475+
* @param ClassNameDetails $modelClassNameDetails
476+
* @param ?ClassNameDetails $identityClassNameDetails
478477
*/
479478
private function generateRepository(
480-
ClassNameDetails $modelClassNameDetails,
481-
ClassNameDetails $identityClassNameDetails,
479+
Generator $generator,
482480
InputInterface $input,
483-
Generator $generator
481+
ClassNameDetails $modelClassNameDetails,
482+
?ClassNameDetails $identityClassNameDetails,
484483
): void {
485484
$interfaceNameDetails = $generator->createClassNameDetails(
486485
$input->getArgument('name'),
@@ -489,10 +488,10 @@ private function generateRepository(
489488
);
490489

491490
$this->generateRepositoryInterface(
491+
$generator,
492492
$interfaceNameDetails,
493493
$modelClassNameDetails,
494494
$identityClassNameDetails,
495-
$generator
496495
);
497496

498497
$implementationNameDetails = $generator->createClassNameDetails(
@@ -503,17 +502,17 @@ private function generateRepository(
503502

504503
$interfaceClassName = $interfaceNameDetails->getShortName() . 'Interface';
505504
$templateVars = [
506-
'use_statements' => new UseStatementGenerator([
505+
'use_statements' => new UseStatementGenerator(array_filter([
507506
$modelClassNameDetails->getFullName(),
508-
$identityClassNameDetails->getFullName(),
507+
$identityClassNameDetails?->getFullName(),
509508
ManagerRegistry::class,
510509
QueryBuilder::class,
511510
[ OrmRepository::class => 'OrmRepository' ],
512511
[ $interfaceNameDetails->getFullName() => $interfaceClassName ],
513-
]),
512+
])),
514513
'interface_class_name' => $interfaceClassName,
515514
'model_class_name' => $modelClassNameDetails->getShortName(),
516-
'identity_class_name' => $identityClassNameDetails->getShortName()
515+
'identity_class_name' => $identityClassNameDetails?->getShortName()
517516
];
518517

519518
$templatePath = __DIR__.'/../Resources/skeleton/model/Repository.tpl.php';
@@ -529,26 +528,26 @@ private function generateRepository(
529528
/**
530529
* Generate model repository
531530
*
532-
* @param ClassNameDetails $classNameDetails
533-
* @param ClassNameDetails $entityClassNameDetails
534-
* @param ClassNameDetails $identityClassNameDetails
535531
* @param Generator $generator
532+
* @param ClassNameDetails $classNameDetails
533+
* @param ClassNameDetails $modelClassNameDetails
534+
* @param ?ClassNameDetails $identityClassNameDetails
536535
* @throws \Exception
537536
*/
538537
private function generateRepositoryInterface(
538+
Generator $generator,
539539
ClassNameDetails $classNameDetails,
540540
ClassNameDetails $modelClassNameDetails,
541-
ClassNameDetails $identityClassNameDetails,
542-
Generator $generator
541+
?ClassNameDetails $identityClassNameDetails,
543542
): void {
544543
$templateVars = [
545-
'use_statements' => new UseStatementGenerator([
544+
'use_statements' => new UseStatementGenerator(array_filter([
546545
$modelClassNameDetails->getFullName(),
547-
$identityClassNameDetails->getFullName(),
546+
$identityClassNameDetails?->getFullName(),
548547
Repository::class,
549-
]),
548+
])),
550549
'model_class_name' => $modelClassNameDetails->getShortName(),
551-
'identity_class_name' => $identityClassNameDetails->getShortName()
550+
'identity_class_name' => $identityClassNameDetails?->getShortName()
552551
];
553552

554553
$templatePath = __DIR__.'/../Resources/skeleton/model/RepositoryInterface.tpl.php';

src/Resources/skeleton/model/Repository.tpl.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66

77
class <?= $class_name ?> extends OrmRepository implements <?= $interface_class_name ?>
88
{
9+
<?php if ($identity_class_name): ?>
910
public function findById(<?= $identity_class_name ?> $id): ?<?= $model_class_name ?>
1011
{
1112
// TODO: Implement me!
1213

1314
return null;
1415
}
16+
<?php endif; ?>
1517

1618
// public function findByExampleField($value): self
1719
// {

src/Resources/skeleton/model/RepositoryInterface.tpl.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@
66

77
interface <?= $class_name ?> extends Repository
88
{
9+
<?php if ($identity_class_name): ?>
910
public function findById(<?= $identity_class_name ?> $id): ?<?= $model_class_name ?>;
11+
<?php endif; ?>
1012
}

0 commit comments

Comments
 (0)