From 51fef83889cded5b03894d1b613a2cda02fb4dec Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Thu, 24 Mar 2022 16:20:42 +0100 Subject: [PATCH] add page maker --- src/core/Maker/MakePage.php | 176 ++++++++++++++++++ .../Resources/maker/page/PageEntity.tpl.php | 38 ++++ 2 files changed, 214 insertions(+) create mode 100644 src/core/Maker/MakePage.php create mode 100644 src/core/Resources/maker/page/PageEntity.tpl.php diff --git a/src/core/Maker/MakePage.php b/src/core/Maker/MakePage.php new file mode 100644 index 0000000..ec452d3 --- /dev/null +++ b/src/core/Maker/MakePage.php @@ -0,0 +1,176 @@ +addArgument( + 'page-class', + InputArgument::OPTIONAL, + 'Choose a name for your page class (e.g. ExamplePage)' + ) + ->setHelp('') + ; + } + + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator) + { + $pageClassNameDetails = $generator->createClassNameDetails( + $input->getArgument('page-class'), + 'Entity\\Page\\', + 'Page' + ); + + $blocks = []; + + $isFirstField = true; + $blocks = []; + + while (true) { + $newBlock = $this->askForNextBlock($io, $blocks, $isFirstField); + $isFirstField = false; + + if (null === $newBlock) { + break; + } + + $blocks[$newBlock['name']] = $newBlock; + } + + $options = [ + 'entity' => $pageClassNameDetails->getFullName(), + 'blocks' => $blocks, + ]; + + $controllerPath = $generator->generateController( + $pageClassNameDetails->getFullName(), + __DIR__.'/../Resources/maker/page/PageEntity.tpl.php', + $options + ); + + $generator->writeChanges(); + + $this->writeSuccessMessage($io); + $io->text('Register the page in config/packages/app.yaml: '); + $io->text(<<< EOF + + core: + site: + pages: + {$pageClassNameDetails->getFullName()}: + name: {$pageClassNameDetails->getShortName()} + templates: + - {name: "Default", file: "path/to/template.html.twig"} + +EOF +); + } + + private function askForNextBlock(ConsoleStyle $io, array $blocks, bool $isFirstField) + { + $io->writeln(''); + + if ($isFirstField) { + $questionText = 'New property name (press to stop adding fields)'; + } else { + $questionText = 'Add another property? Enter the property name (or press to stop adding fields)'; + } + + $blockName = $io->ask($questionText, null, function ($name) use ($blocks) { + if (!$name) { + return $name; + } + + if (isset($blocks[$name])) { + throw new \InvalidArgumentException(sprintf('The "%s" block already exists.', $name)); + } + + return $name; + }); + + if (!$blockName) { + return null; + } + + $type = null; + $defaultType = 'text'; + $snakeCasedField = Str::asSnakeCase($blockName); + $types = [ + 'text' => null, + 'textarea' => null, + 'choice' => null, + 'collection' => 'BlockEntity\\CollectionBlock::class', + 'file' => 'BlockEntity\\FileBlock::class', + 'file_picker' => null, + 'image' => 'BlockEntity\\FileBlock::class', + ]; + + while (null === $type) { + $question = new Question('Field type (enter ? to see all types)', $defaultType); + $question->setAutocompleterValues(array_keys($types)); + $type = $io->askQuestion($question); + + if ('?' === $type) { + $this->printAvailableTypes($io, array_keys($types)); + $io->writeln(''); + + $type = null; + } elseif (!\in_array($type, array_keys($types))) { + $this->printAvailableTypes($io, array_keys($types)); + $io->error(sprintf('Invalid type "%s".', $type)); + $io->writeln(''); + + $type = null; + } + } + + return [ + 'name' => $blockName, + 'type' => 'Block\\'.Str::asCamelCase($type).'BlockType::class', + 'class' => $types[$type], + 'camelCase' => Str::asCamelCase($blockName), + ]; + } + + private function printAvailableTypes(ConsoleStyle $io, array $types) + { + $io->writeln('Types'); + + foreach ($types as $type) { + $io->writeln(sprintf(' * %s', $type)); + } + } + + public function configureDependencies(DependencyBuilder $dependencies) + { + $dependencies->addClassDependency( + Annotation::class, + 'doctrine/annotations' + ); + } +} diff --git a/src/core/Resources/maker/page/PageEntity.tpl.php b/src/core/Resources/maker/page/PageEntity.tpl.php new file mode 100644 index 0000000..9368350 --- /dev/null +++ b/src/core/Resources/maker/page/PageEntity.tpl.php @@ -0,0 +1,38 @@ + + +namespace ; + +use App\Core\Entity\Site\Page\Page; +use App\Core\Entity\Site\Page as BlockEntity; +use App\Core\Form\Site\Page as Block; +use Doctrine\ORM\Mapping as ORM; +use Symfony\Component\Form\FormBuilderInterface; + +/** + * @ORM\Entity + */ +class extends Page +{ + public function buildForm(FormBuilderInterface $builder, array $options) + { + $builder + + ->add('', ) + + ; + + } + + + public function set(BlockEntity\Block $block) + { + return $this->setBlock($block); + } + + public function get() + { + return $this->getBlock('', ); + } + + +}