|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Fd\FractalBundle\Maker; |
| 4 | + |
| 5 | +use League\Fractal\TransformerAbstract; |
| 6 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 7 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 8 | +use Symfony\Bundle\MakerBundle\Doctrine\DoctrineHelper; |
| 9 | +use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException; |
| 10 | +use Symfony\Bundle\MakerBundle\Generator; |
| 11 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 12 | +use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
| 13 | +use Symfony\Bundle\MakerBundle\Str; |
| 14 | +use Symfony\Bundle\MakerBundle\Validator; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Input\InputArgument; |
| 17 | +use Symfony\Component\Console\Input\InputInterface; |
| 18 | +use Symfony\Component\Console\Input\InputOption; |
| 19 | +use Symfony\Component\Console\Question\Question; |
| 20 | + |
| 21 | +final class MakeFractalTransformer extends AbstractMaker |
| 22 | +{ |
| 23 | + use MakerTrait; |
| 24 | + |
| 25 | + private $doctrineHelper; |
| 26 | + |
| 27 | + public function __construct(DoctrineHelper $doctrineHelper) |
| 28 | + { |
| 29 | + $this->doctrineHelper = $doctrineHelper; |
| 30 | + } |
| 31 | + |
| 32 | + public static function getCommandName(): string |
| 33 | + { |
| 34 | + return 'make:fractal-transformer'; |
| 35 | + } |
| 36 | + |
| 37 | + public static function getCommandDescription(): string |
| 38 | + { |
| 39 | + return 'Create a transformer class for transforming data.'; |
| 40 | + } |
| 41 | + |
| 42 | + public function configureCommand(Command $command, InputConfiguration $inputConf) |
| 43 | + { |
| 44 | + $command |
| 45 | + ->setDescription('Creates a new transformer class') |
| 46 | + ->addArgument('entity-class', InputArgument::OPTIONAL, sprintf('The class name of the entity to create Transformer (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm()))) |
| 47 | + ->addArgument('class-name', InputArgument::OPTIONAL, sprintf('The class name of new Transformer (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm()))) |
| 48 | + ->addOption('no-entity', null, InputOption::VALUE_NONE, 'Use this option to generate a plank transformer') |
| 49 | + ->setHelp(file_get_contents(__DIR__ . '/../Resources/help/MakeFractalTransformer.txt')); |
| 50 | + |
| 51 | + $inputConf->setArgumentAsNonInteractive('entity-class'); |
| 52 | + $inputConf->setArgumentAsNonInteractive('class-name'); |
| 53 | + } |
| 54 | + |
| 55 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command) |
| 56 | + { |
| 57 | + if ($input->getOption('no-entity')) { |
| 58 | + $io->block([ |
| 59 | + 'Note: You have choose to generate a blank Transformer.', |
| 60 | + ], null, 'fg=yellow'); |
| 61 | + $classname = $io->ask(sprintf('The class name of new Transformer (e.g. <fg=yellow>%s</>)', Str::asClassName(Str::getRandomTerm())), null, [Validator::class, 'notBlank']); |
| 62 | + |
| 63 | + $input->setArgument('class-name', $classname); |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + $argument = $command->getDefinition()->getArgument('entity-class'); |
| 68 | + $question = $this->createEntityClassQuestion($argument->getDescription()); |
| 69 | + $value = $io->askQuestion($question); |
| 70 | + |
| 71 | + $input->setArgument('entity-class', $value); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) |
| 76 | + { |
| 77 | + $noEntity = $input->getOption('no-entity'); |
| 78 | + $classname = !$noEntity ? null : Str::asClassName($input->getArgument('class-name')); |
| 79 | + |
| 80 | + $entityClassDetails = !$noEntity ? $generator->createClassNameDetails( |
| 81 | + Validator::entityExists($input->getArgument('entity-class'), $this->doctrineHelper->getEntitiesForAutocomplete()), |
| 82 | + 'Entity\\' |
| 83 | + ) : null; |
| 84 | + |
| 85 | + $transformerClassDetails = !$noEntity ? $generator->createClassNameDetails( |
| 86 | + $entityClassDetails->getShortName(), |
| 87 | + 'Transformer\\', |
| 88 | + 'Transformer' |
| 89 | + ) : $generator->createClassNameDetails($classname, 'Transformer\\', 'Transformer'); |
| 90 | + |
| 91 | + $generator->generateClass( |
| 92 | + $transformerClassDetails->getFullName(), |
| 93 | + __DIR__ . '/../Resources/skeleton/Transformer.tpl.php', |
| 94 | + [ |
| 95 | + 'no_entity' => $noEntity, |
| 96 | + 'entity_class_name' => $entityClassDetails ? $entityClassDetails->getShortName() : null, |
| 97 | + 'entity_variable_name' => $entityClassDetails ? Str::asLowerCamelCase($entityClassDetails->getShortName()) : null, |
| 98 | + 'entity_full_class_name' => $entityClassDetails ? $entityClassDetails->getFullName(): null, |
| 99 | + ] |
| 100 | + ); |
| 101 | + |
| 102 | + $generator->writeChanges(); |
| 103 | + |
| 104 | + $this->writeSuccessMessage($io); |
| 105 | + |
| 106 | + $io->text([ |
| 107 | + 'Next: Open your new transformer class and start customizing it.', |
| 108 | + ]); |
| 109 | + } |
| 110 | + |
| 111 | + public function configureDependencies(DependencyBuilder $dependencies) |
| 112 | + { |
| 113 | + $dependencies->addClassDependency(TransformerAbstract::class, 'fd6130/fractal-bundle'); |
| 114 | + } |
| 115 | + |
| 116 | + private function createEntityClassQuestion(string $questionText): Question |
| 117 | + { |
| 118 | + $question = new Question($questionText); |
| 119 | + $question->setValidator([Validator::class, 'notBlank']); |
| 120 | + $question->setAutocompleterValues($this->doctrineHelper->getEntitiesForAutocomplete()); |
| 121 | + |
| 122 | + return $question; |
| 123 | + } |
| 124 | +} |
0 commit comments