Version 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                 '',
126                 InputOption::VALUE_REQUIRED,
127                 $this->trans('commands.common.options.module')
128             )
129             ->addOption(
130                 'class',
131                 '',
132                 InputOption::VALUE_OPTIONAL,
133                 $this->trans('commands.generate.form.options.class')
134             )
135             ->addOption(
136                 'form-id',
137                 '',
138                 InputOption::VALUE_OPTIONAL,
139                 $this->trans('commands.generate.form.options.form-id')
140             )
141             ->addOption(
142                 'services',
143                 '',
144                 InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
145                 $this->trans('commands.common.options.services')
146             )
147             ->addOption(
148                 'inputs',
149                 '',
150                 InputOption::VALUE_OPTIONAL,
151                 $this->trans('commands.common.options.inputs')
152             )
153             ->addOption(
154                 'path',
155                 '',
156                 InputOption::VALUE_OPTIONAL,
157                 $this->trans('commands.generate.form.options.path')
158             )
159             ->addOption(
160                 'menu_link_gen',
161                 '',
162                 InputOption::VALUE_OPTIONAL,
163                 $this->trans('commands.generate.form.options.menu_link_gen')
164             )
165             ->addOption(
166                 'menu_link_title',
167                 '',
168                 InputOption::VALUE_OPTIONAL,
169                 $this->trans('commands.generate.form.options.menu_link_title')
170             )
171             ->addOption(
172                 'menu_parent',
173                 '',
174                 InputOption::VALUE_OPTIONAL,
175                 $this->trans('commands.generate.form.options.menu_parent')
176             )
177             ->addOption(
178                 'menu_link_desc',
179                 '',
180                 InputOption::VALUE_OPTIONAL,
181                 $this->trans('commands.generate.form.options.menu_link_desc')
182             );
183     }
184
185     /**
186      * {@inheritdoc}
187      */
188     protected function execute(InputInterface $input, OutputInterface $output)
189     {
190         $module = $input->getOption('module');
191         $services = $input->getOption('services');
192         $path = $input->getOption('path');
193         $class_name = $input->getOption('class');
194         $form_id = $input->getOption('form-id');
195         $form_type = $this->formType;
196         $menu_link_gen = $input->getOption('menu_link_gen');
197         $menu_parent = $input->getOption('menu_parent');
198         $menu_link_title = $input->getOption('menu_link_title');
199         $menu_link_desc = $input->getOption('menu_link_desc');
200
201         // if exist form generate config file
202         $inputs = $input->getOption('inputs');
203         $build_services = $this->buildServices($services);
204
205         $this
206             ->generator
207             ->generate($module, $class_name, $form_id, $form_type, $build_services, $inputs, $path, $menu_link_gen, $menu_link_title, $menu_parent, $menu_link_desc);
208
209         $this->chainQueue->addCommand('router:rebuild', []);
210     }
211
212     /**
213      * {@inheritdoc}
214      */
215     protected function interact(InputInterface $input, OutputInterface $output)
216     {
217         $io = new DrupalStyle($input, $output);
218
219         // --module option
220         $module = $input->getOption('module');
221         if (!$module) {
222             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
223             $module = $this->moduleQuestion($io);
224             $input->setOption('module', $module);
225         }
226
227         // --class option
228         $className = $input->getOption('class');
229         if (!$className) {
230             $className = $io->ask(
231                 $this->trans('commands.generate.form.questions.class'),
232                 'DefaultForm'
233             );
234             $input->setOption('class', $className);
235         }
236
237         // --form-id option
238         $formId = $input->getOption('form-id');
239         if (!$formId) {
240             $formId = $io->ask(
241                 $this->trans('commands.generate.form.questions.form-id'),
242                 $this->stringConverter->camelCaseToMachineName($className)
243             );
244             $input->setOption('form-id', $formId);
245         }
246
247         // --services option
248         // @see use Drupal\Console\Command\Shared\ServicesTrait::servicesQuestion
249         $services = $this->servicesQuestion($io);
250         $input->setOption('services', $services);
251
252         // --inputs option
253         $inputs = $input->getOption('inputs');
254         if (!$inputs) {
255             // @see \Drupal\Console\Command\Shared\FormTrait::formQuestion
256             $inputs = $this->formQuestion($io);
257             $input->setOption('inputs', $inputs);
258         }
259
260         $path = $input->getOption('path');
261         if (!$path) {
262             if ($this->formType == 'ConfigFormBase') {
263                 $form_path = '/admin/config/{{ module_name }}/{{ class_name_short }}';
264                 $form_path = sprintf(
265                     '/admin/config/%s/%s',
266                     $module,
267                     strtolower($this->stringConverter->removeSuffix($className))
268                 );
269             } else {
270                 $form_path = sprintf(
271                     '/%s/form/%s',
272                     $module,
273                     $this->stringConverter->camelCaseToMachineName($this->stringConverter->removeSuffix($className))
274                 );
275             }
276             $path = $io->ask(
277                 $this->trans('commands.generate.form.questions.path'),
278                 $form_path,
279                 function ($path) {
280                     if (count($this->routeProvider->getRoutesByPattern($path)) > 0) {
281                         throw new \InvalidArgumentException(
282                             sprintf(
283                                 $this->trans(
284                                     'commands.generate.form.messages.path-already-added'
285                                 ),
286                                 $path
287                             )
288                         );
289                     }
290
291                     return $path;
292                 }
293             );
294             $input->setOption('path', $path);
295         }
296
297         // --link option for links.menu
298         if ($this->formType == 'ConfigFormBase') {
299             $menu_options = $this->menuQuestion($io, $className);
300             $menu_link_gen = $input->getOption('menu_link_gen');
301             $menu_link_title = $input->getOption('menu_link_title');
302             $menu_parent = $input->getOption('menu_parent');
303             $menu_link_desc = $input->getOption('menu_link_desc');
304             if (!$menu_link_gen || !$menu_link_title || !$menu_parent || !$menu_link_desc) {
305                 $input->setOption('menu_link_gen', $menu_options['menu_link_gen']);
306                 $input->setOption('menu_link_title', $menu_options['menu_link_title']);
307                 $input->setOption('menu_parent', $menu_options['menu_parent']);
308                 $input->setOption('menu_link_desc', $menu_options['menu_link_desc']);
309             }
310         }
311     }
312 }