From aa01fc714d63fce66027ad73eb8ef73f62828639 Mon Sep 17 00:00:00 2001 From: Albert Volkman Date: Wed, 23 Oct 2013 18:07:42 -0400 Subject: [PATCH] Add form generator --- .../Command/GeneratorFormCommand.php | 146 ++++++++++++++++++ lib/Drupal/AppConsole/Console/Application.php | 1 + .../AppConsole/Generator/FormGenerator.php | 34 ++++ .../module/module.DefaultForm.php.twig | 65 ++++++++ 4 files changed, 246 insertions(+) create mode 100644 lib/Drupal/AppConsole/Command/GeneratorFormCommand.php create mode 100644 lib/Drupal/AppConsole/Generator/FormGenerator.php create mode 100644 lib/Drupal/AppConsole/Resources/skeleton/module/module.DefaultForm.php.twig diff --git a/lib/Drupal/AppConsole/Command/GeneratorFormCommand.php b/lib/Drupal/AppConsole/Command/GeneratorFormCommand.php new file mode 100644 index 000000000..3500eb277 --- /dev/null +++ b/lib/Drupal/AppConsole/Command/GeneratorFormCommand.php @@ -0,0 +1,146 @@ +setDefinition(array( + new InputOption('module','',InputOption::VALUE_REQUIRED, 'The name of the module'), + new InputOption('name','',InputOption::VALUE_OPTIONAL, 'Form name'), + new InputOption('services','',InputOption::VALUE_OPTIONAL, 'Load services'), + new InputOption('routing', '', InputOption::VALUE_NONE, 'Update routing'), + )) + ->setDescription('Generate form') + ->setHelp('The generate:form command helps you generate a new form.') + ->setName('generate:form'); + } + + /** + * Execute method + * @param InputInterface $input [description] + * @param OutputInterface $output [description] + * @return [type] [description] + */ + protected function execute(InputInterface $input, OutputInterface $output) { + $dialog = $this->getDialogHelper(); + + $module = $input->getOption('module'); + $services = $input->getOption('services'); + $update_routing = $input->getOption('routing'); + $name = $input->getOption('name'); + + $map_service = array(); + foreach ($services as $service) { + $class = get_class($this->getContainer()->get($service)); + $map_service[$service] = array( + 'name' => $service, + 'machine_name' => str_replace('.', '_', $service), + 'class' => $class, + 'short' => end(explode('\\',$class)) + ); + } + + $generator = $this->getGenerator(); + $generator->generate($module, $name, $controller, $map_service); + + if ($update_routing) { + echo "update"; + } + + $dialog->writeGeneratorSummary($output, $errors); + } + + /** + * [interact description] + * @param InputInterface $input [description] + * @param OutputInterface $output [description] + * @return [type] [description] + */ + protected function interact(InputInterface $input, OutputInterface $output) { + + $dialog = $this->getDialogHelper(); + $dialog->writeSection($output, 'Welcome to the Drupal form generator'); + + $d = $this->getHelperSet()->get('dialog'); + + // Module name + $modules = $this->getModules(); + $module = $d->askAndValidate( + $output, + $dialog->getQuestion('Enter your module: '), + function($module) use ($modules){ + return Validators::validateModuleExist($module, $modules); + }, + false, + '', + $modules + ); + $input->setOption('module', $module); + + // Module name + $name = $this->getName(); + $name = $dialog->ask($output, $dialog->getQuestion('Enter the form name', 'DefaultForm'), 'DefaultForm'); + $input->setOption('name', $name); + + // Services + $service_collection = array(); + $services = $this->getServices(); + while(true){ + $service = $d->askAndValidate( + $output, + $dialog->getQuestion('Enter your service: '), + function($service) use ($services){ + return Validators::validateServiceExist($service, $services); + }, + false, + null, + $services + ); + + if ($service == null) { + break; + } + array_push($service_collection, $service); + $service_key = array_search($service, $services, true); + if ($service_key >= 0) + unset($services[$service_key]); + } + $input->setOption('services', $service_collection); + + // Routing + /** + * Generate routing + * @var [type] + */ + $routing = $input->getOption('routing'); + if (!$routing && $dialog->askConfirmation($output, $dialog->getQuestion('Update routing file?', 'yes', '?'), true)) { + $routing = true; + } + $input->setOption('routing', $routing); + + } + + /** + * Get a filesystem + * @return [type] Drupal Filesystem + */ + protected function createGenerator() { + return new FormGenerator(); + } + +} + diff --git a/lib/Drupal/AppConsole/Console/Application.php b/lib/Drupal/AppConsole/Console/Application.php index f9fd28fbc..89c7a1a2c 100644 --- a/lib/Drupal/AppConsole/Console/Application.php +++ b/lib/Drupal/AppConsole/Console/Application.php @@ -77,6 +77,7 @@ public function getDefaultCommands() { $commands = parent::getDefaultCommands(); $commands[] = new \Drupal\AppConsole\Command\GeneratorModuleCommand(); $commands[] = new \Drupal\AppConsole\Command\GeneratorControllerCommand(); + $commands[] = new \Drupal\AppConsole\Command\GeneratorFormCommand(); $commands[] = new \Drupal\AppConsole\Command\ServicesCommand(); return $commands; } diff --git a/lib/Drupal/AppConsole/Generator/FormGenerator.php b/lib/Drupal/AppConsole/Generator/FormGenerator.php new file mode 100644 index 000000000..71e534f11 --- /dev/null +++ b/lib/Drupal/AppConsole/Generator/FormGenerator.php @@ -0,0 +1,34 @@ + $name, + 'services' => $services, + 'module' => $module + ); + + $this->renderFile( + 'module/module.DefaultForm.php.twig', + $path_controller . '/'. $name .'.php', + $parameters + ); + + $this->renderFile('module/module.routing.yml.twig', DRUPAL_ROOT.'/modules/'.$module.'/'.$module.'.routing.yml', $parameters); + + } + +} diff --git a/lib/Drupal/AppConsole/Resources/skeleton/module/module.DefaultForm.php.twig b/lib/Drupal/AppConsole/Resources/skeleton/module/module.DefaultForm.php.twig new file mode 100644 index 000000000..1ba463af4 --- /dev/null +++ b/lib/Drupal/AppConsole/Resources/skeleton/module/module.DefaultForm.php.twig @@ -0,0 +1,65 @@ +{{service.machine_name}} = ${{service.machine_name}}; +{% endfor %} + } + + public static function create(ContainerInterface $container){ + return new static( +{% for service in services %} + $container->get('{{service.name}}'){% if loop.last == false %},{% endif %} +{% endfor %} + ); + } + {% endif %} + + /** + * {@inheritdoc} + */ + public function getFormID() { + return ''; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + public function submitForm(array &$form, array &$form_state) { + } +}