forked from hechoendrupal/drupal-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorFormCommand.php
More file actions
152 lines (132 loc) · 5.03 KB
/
Copy pathGeneratorFormCommand.php
File metadata and controls
152 lines (132 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* @file
* Contains Drupal\AppConsole\Command\GeneratorFormCommand.
*/
namespace Drupal\AppConsole\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\AppConsole\Command\Helper\ServicesTrait;
use Drupal\AppConsole\Command\Helper\ModuleTrait;
use Drupal\AppConsole\Command\Helper\FormTrait;
use Drupal\AppConsole\Generator\FormGenerator;
abstract class GeneratorFormCommand extends GeneratorCommand
{
use ModuleTrait;
use ServicesTrait;
use FormTrait;
private $formType;
private $commandName;
protected function setFormType($formType)
{
return $this->formType = $formType;
}
protected function setCommandName($commandName)
{
return $this->commandName = $commandName;
}
protected function configure()
{
$this
->setName($this->commandName)
->setDescription(sprintf(
$this->trans('commands.generate.form.description'),
$this->formType
))
->setHelp(sprintf(
$this->trans('commands.generate.form.help'),
$this->commandName,
$this->formType
))
->addOption('module', '', InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
->addOption('class-name', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.form.options.class-name'))
->addOption('form-id', '', InputOption::VALUE_OPTIONAL,
$this->trans('commands.generate.form.options.form-id'))
->addOption('services', '', InputOption::VALUE_OPTIONAL, $this->trans('commands.common.options.services'))
->addOption('inputs', '', InputOption::VALUE_OPTIONAL, $this->trans('commands.common.options.inputs'))
->addOption('routing', '', InputOption::VALUE_NONE, $this->trans('commands.generate.form.options.routing'));
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$module = $input->getOption('module');
$services = $input->getOption('services');
$update_routing = $input->getOption('routing');
$class_name = $input->getOption('class-name');
$form_id = $input->getOption('form-id');
// if exist form generate config file
$inputs = $input->getOption('inputs');
$build_services = $this->buildServices($services);
$this
->getGenerator()
->generate($module, $class_name, $form_id, $build_services, $inputs, $update_routing);
$this->getHelper('chain')->addCommand('router:rebuild');
}
/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$dialog = $this->getDialogHelper();
// --module option
$module = $input->getOption('module');
if (!$module) {
// @see Drupal\AppConsole\Command\Helper\ModuleTrait::moduleQuestion
$module = $this->moduleQuestion($output, $dialog);
}
$input->setOption('module', $module);
// --class-name option
$class_name = $input->getOption('class-name');
if (!$class_name) {
$class_name = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.form.questions.class-name'), 'DefaultForm'),
'DefaultForm'
);
}
$input->setOption('class-name', $class_name);
// --form-id option
$form_id = $input->getOption('form-id');
if (!$form_id) {
$form_id = $this->getStringUtils()->camelCaseToMachineName($class_name);
$form_id = $dialog->ask(
$output,
$dialog->getQuestion($this->trans('commands.generate.form.questions.form-id'), $form_id),
$form_id
);
}
$input->setOption('form-id', $form_id);
// --services option
// @see use Drupal\AppConsole\Command\Helper\ServicesTrait::servicesQuestion
$services_collection = $this->servicesQuestion($output, $dialog);
$input->setOption('services', $services_collection);
// --inputs option
$inputs = $input->getOption('inputs');
if (!$inputs) {
// @see \Drupal\AppConsole\Command\Helper\FormTrait::formQuestion
$inputs = $this->formQuestion($output, $dialog);
}
$input->setOption('inputs', $inputs);
// --routing option
$routing = $input->getOption('routing');
if (!$routing && $dialog->askConfirmation(
$output,
$dialog->getQuestion($this->trans('commands.generate.form.questions.routing'), 'yes', '?'),
true)
) {
$routing = true;
}
$input->setOption('routing', $routing);
}
/**
* @return \Drupal\AppConsole\Generator\FormGenerator.
*/
protected function createGenerator()
{
return new FormGenerator();
}
}