Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / FormCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Generate\FormCommand.
6  */
7
8 namespace Drupal\Console\Command\Generate;
9
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Input\InputOption;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Drupal\Console\Command\Shared\ServicesTrait;
14 use Drupal\Console\Command\Shared\ModuleTrait;
15 use Drupal\Console\Command\Shared\MenuTrait;
16 use Drupal\Console\Command\Shared\FormTrait;
17 use Symfony\Component\Console\Command\Command;
18 use Drupal\Console\Core\Style\DrupalStyle;
19 use Drupal\Console\Core\Command\Shared\ContainerAwareCommandTrait;
20 use Drupal\Console\Generator\FormGenerator;
21 use Drupal\Console\Extension\Manager;
22 use Drupal\Console\Core\Utils\ChainQueue;
23 use Drupal\Console\Core\Utils\StringConverter;
24 use Drupal\Core\Render\ElementInfoManager;
25 use Drupal\Core\Routing\RouteProviderInterface;
26
27 abstract class FormCommand extends Command
28 {
29     use ContainerAwareCommandTrait;
30     use ModuleTrait;
31     use ServicesTrait;
32     use FormTrait;
33     use MenuTrait;
34
35     private $formType;
36     private $commandName;
37
38     /**
39  * @var Manager
40 */
41     protected $extensionManager;
42
43     /**
44  * @var FormGenerator
45 */
46     protected $generator;
47
48     /**
49  * @var ChainQueue
50 */
51     protected $chainQueue;
52
53     /**
54      * @var StringConverter
55      */
56     protected $stringConverter;
57
58     /**
59      * @var ElementInfoManager
60      */
61     protected $elementInfoManager;
62
63     /**
64      * @var RouteProviderInterface
65      */
66     protected $routeProvider;
67
68
69     /**
70      * FormCommand constructor.
71      *
72      * @param Manager                $extensionManager
73      * @param FormGenerator          $generator
74      * @param ChainQueue             $chainQueue
75      * @param StringConverter        $stringConverter
76      * @param ElementInfoManager     $elementInfoManager
77      * @param RouteProviderInterface $routeProvider
78      */
79     public function __construct(
80         Manager $extensionManager,
81         FormGenerator $generator,
82         ChainQueue $chainQueue,
83         StringConverter $stringConverter,
84         ElementInfoManager $elementInfoManager,
85         RouteProviderInterface $routeProvider
86     ) {
87         $this->extensionManager = $extensionManager;
88         $this->generator = $generator;
89         $this->chainQueue = $chainQueue;
90         $this->stringConverter = $stringConverter;
91         $this->elementInfoManager = $elementInfoManager;
92         $this->routeProvider = $routeProvider;
93         parent::__construct();
94     }
95
96     protected function setFormType($formType)
97     {
98         return $this->formType = $formType;
99     }
100
101     protected function setCommandName($commandName)
102     {
103         return $this->commandName = $commandName;
104     }
105
106     protected function configure()
107     {
108         $this
109             ->setName($this->commandName)
110             ->setDescription(
111                 sprintf(
112                     $this->trans('commands.generate.form.description'),
113                     $this->formType
114                 )
115             )
116             ->setHelp(
117                 sprintf(
118                     $this->trans('commands.generate.form.help'),
119                     $this->commandName,
120                     $this->formType
121                 )
122             )
123             ->addOption(
124                 'module',
125                 null,
126                 InputOption::VALUE_REQUIRED,
127                 $this->trans('commands.common.options.module')
128             )
129             ->addOption(
130                 'class',
131                 null,
132                 InputOption::VALUE_OPTIONAL,
133                 $this->trans('commands.generate.form.options.class')
134             )
135             ->addOption(
136                 'form-id',
137                 null,
138                 InputOption::VALUE_OPTIONAL,
139                 $this->trans('commands.generate.form.options.form-id')
140             )
141             ->addOption(
142                 'services',
143                 null,
144                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
145                 $this->trans('commands.common.options.services')
146             )
147             ->addOption(
148                 'config-file',
149                 null,
150                 InputOption::VALUE_NONE,
151                 $this->trans('commands.generate.form.options.config-file')
152             )
153             ->addOption(
154                 'inputs',
155                 null,
156                 InputOption::VALUE_OPTIONAL,
157                 $this->trans('commands.common.options.inputs')
158             )
159             ->addOption(
160                 'path',
161                 null,
162                 InputOption::VALUE_OPTIONAL,
163                 $this->trans('commands.generate.form.options.path')
164             )
165             ->addOption(
166                 'menu_link_gen',
167                 null,
168                 InputOption::VALUE_OPTIONAL,
169                 $this->trans('commands.generate.form.options.menu_link_gen')
170             )
171             ->addOption(
172                 'menu_link_title',
173                 null,
174                 InputOption::VALUE_OPTIONAL,
175                 $this->trans('commands.generate.form.options.menu_link_title')
176             )
177             ->addOption(
178                 'menu_parent',
179                 null,
180                 InputOption::VALUE_OPTIONAL,
181                 $this->trans('commands.generate.form.options.menu_parent')
182             )
183             ->addOption(
184                 'menu_link_desc',
185                 null,
186                 InputOption::VALUE_OPTIONAL,
187                 $this->trans('commands.generate.form.options.menu_link_desc')
188             );
189     }
190
191     /**
192      * {@inheritdoc}
193      */
194     protected function execute(InputInterface $input, OutputInterface $output)
195     {
196         $module = $input->getOption('module');
197         $services = $input->getOption('services');
198         $path = $input->getOption('path');
199         $config_file = $input->getOption('config-file');
200         $class_name = $input->getOption('class');
201         $form_id = $input->getOption('form-id');
202         $form_type = $this->formType;
203         $menu_link_gen = $input->getOption('menu_link_gen');
204         $menu_parent = $input->getOption('menu_parent');
205         $menu_link_title = $input->getOption('menu_link_title');
206         $menu_link_desc = $input->getOption('menu_link_desc');
207
208         // if exist form generate config file
209         $inputs = $input->getOption('inputs');
210         $build_services = $this->buildServices($services);
211
212         $this
213             ->generator
214             ->generate($module, $class_name, $form_id, $form_type, $build_services, $config_file, $inputs, $path, $menu_link_gen, $menu_link_title, $menu_parent, $menu_link_desc);
215
216         $this->chainQueue->addCommand('router:rebuild', []);
217     }
218
219     /**
220      * {@inheritdoc}
221      */
222     protected function interact(InputInterface $input, OutputInterface $output)
223     {
224         $io = new DrupalStyle($input, $output);
225
226         // --module option
227         $module = $input->getOption('module');
228         if (!$module) {
229             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
230             $module = $this->moduleQuestion($io);
231             $input->setOption('module', $module);
232         }
233
234         // --class option
235         $className = $input->getOption('class');
236         if (!$className) {
237             $className = $io->ask(
238                 $this->trans('commands.generate.form.questions.class'),
239                 'DefaultForm'
240             );
241             $input->setOption('class', $className);
242         }
243
244         // --form-id option
245         $formId = $input->getOption('form-id');
246         if (!$formId) {
247             $formId = $io->ask(
248                 $this->trans('commands.generate.form.questions.form-id'),
249                 $this->stringConverter->camelCaseToMachineName($className)
250             );
251             $input->setOption('form-id', $formId);
252         }
253
254         // --services option
255         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
256         $services = $this->servicesQuestion($io);
257         $input->setOption('services', $services);
258         
259         // --config_file option
260         $config_file = $input->getOption('config-file');
261
262         if (!$config_file) {
263             $config_file = $io->confirm(
264                 $this->trans('commands.generate.form.questions.config-file'),
265                 true
266             );
267             $input->setOption('config-file', $config_file);
268         }
269
270         // --inputs option
271         $inputs = $input->getOption('inputs');
272         if (!$inputs) {
273             // @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
274             $inputs = $this->formQuestion($io);
275             $input->setOption('inputs', $inputs);
276         }
277
278         $path = $input->getOption('path');
279         if (!$path) {
280             if ($this->formType == 'ConfigFormBase') {
281                 $form_path = '/admin/config/{{ module_name }}/{{ class_name_short }}';
282                 $form_path = sprintf(
283                     '/admin/config/%s/%s',
284                     $module,
285                     strtolower($this->stringConverter->removeSuffix($className))
286                 );
287             } else {
288                 $form_path = sprintf(
289                     '/%s/form/%s',
290                     $module,
291                     $this->stringConverter->camelCaseToMachineName($this->stringConverter->removeSuffix($className))
292                 );
293             }
294             $path = $io->ask(
295                 $this->trans('commands.generate.form.questions.path'),
296                 $form_path,
297                 function ($path) {
298                     if (count($this->routeProvider->getRoutesByPattern($path)) > 0) {
299                         throw new \InvalidArgumentException(
300                             sprintf(
301                                 $this->trans(
302                                     'commands.generate.form.messages.path-already-added'
303                                 ),
304                                 $path
305                             )
306                         );
307                     }
308
309                     return $path;
310                 }
311             );
312             $input->setOption('path', $path);
313         }
314
315         // --link option for links.menu
316         if ($this->formType == 'ConfigFormBase') {
317             $menu_options = $this->menuQuestion($io, $className);
318             $menu_link_gen = $input->getOption('menu_link_gen');
319             $menu_link_title = $input->getOption('menu_link_title');
320             $menu_parent = $input->getOption('menu_parent');
321             $menu_link_desc = $input->getOption('menu_link_desc');
322             if (!$menu_link_gen || !$menu_link_title || !$menu_parent || !$menu_link_desc) {
323                 $input->setOption('menu_link_gen', $menu_options['menu_link_gen']);
324                 $input->setOption('menu_link_title', $menu_options['menu_link_title']);
325                 $input->setOption('menu_parent', $menu_options['menu_parent']);
326                 $input->setOption('menu_link_desc', $menu_options['menu_link_desc']);
327             }
328         }
329     }
330 }