|
31 | 31 | use Symfony\Bundle\MakerBundle\Util\ClassSource\Model\ClassProperty; |
32 | 32 | use Symfony\Bundle\MakerBundle\Util\ClassSourceManipulator; |
33 | 33 | use Symfony\Bundle\MakerBundle\Util\CliOutputHelper; |
| 34 | +use Symfony\Bundle\MakerBundle\Util\EnumHelper; |
34 | 35 | use Symfony\Bundle\MakerBundle\Validator; |
35 | 36 | use Symfony\Bundle\MercureBundle\DependencyInjection\MercureExtension; |
36 | 37 | use Symfony\Component\Console\Command\Command; |
@@ -292,7 +293,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen |
292 | 293 |
|
293 | 294 | break; |
294 | 295 | default: |
295 | | - throw new \Exception('Invalid relation type'); |
| 296 | + throw new \Exception('Invalid relation type.'); |
296 | 297 | } |
297 | 298 |
|
298 | 299 | // save the inverse side if it's being mapped |
@@ -431,7 +432,7 @@ private function askForNextField(ConsoleStyle $io, array $fields, string $entity |
431 | 432 | $classProperty->scale = $io->ask('Scale (number of decimals to store: 100.00 would be 2)', '0', Validator::validateScale(...)); |
432 | 433 | } elseif ('enum' === $type) { |
433 | 434 | // ask for valid backed enum class |
434 | | - $classProperty->enumType = $io->ask('Enum class', null, Validator::classIsBackedEnum(...)); |
| 435 | + $classProperty->enumType = $this->askEnumDetails($io); |
435 | 436 |
|
436 | 437 | // set type according to user decision |
437 | 438 | $classProperty->type = $io->confirm('Can this field store multiple enum values', false) ? 'simple_array' : 'string'; |
@@ -556,6 +557,35 @@ private function createEntityClassQuestion(string $questionText): Question |
556 | 557 | return $question; |
557 | 558 | } |
558 | 559 |
|
| 560 | + private function askEnumDetails(ConsoleStyle $io): string |
| 561 | + { |
| 562 | + $targetEnumClass = null; |
| 563 | + while (null === $targetEnumClass) { |
| 564 | + $question = $this->createEnumQuestion('Enum class (e.g. <fg=yellow>App\Enum\Foo</>)'); |
| 565 | + |
| 566 | + $answeredEnumClass = $io->askQuestion($question); |
| 567 | + |
| 568 | + if (enum_exists($answeredEnumClass)) { |
| 569 | + $targetEnumClass = $answeredEnumClass; |
| 570 | + } else { |
| 571 | + $io->error(\sprintf('Unknown enum "%s"', $answeredEnumClass)); |
| 572 | + } |
| 573 | + } |
| 574 | + |
| 575 | + return $targetEnumClass; |
| 576 | + } |
| 577 | + |
| 578 | + private function createEnumQuestion(string $questionText): Question |
| 579 | + { |
| 580 | + $question = new Question($questionText); |
| 581 | + $question->setValidator(Validator::classIsBackedEnum(...)); |
| 582 | + |
| 583 | + $enumHelper = new EnumHelper($this->fileManager->getRootDirectory().'/src', 'App'); |
| 584 | + $question->setAutocompleterValues($enumHelper->getAllEnums()); |
| 585 | + |
| 586 | + return $question; |
| 587 | + } |
| 588 | + |
559 | 589 | private function askRelationDetails(ConsoleStyle $io, string $generatedEntityClass, string $type, string $newFieldName): EntityRelation |
560 | 590 | { |
561 | 591 | // ask the targetEntity |
|
0 commit comments