Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Generate / PluginConditionCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\PluginConditionCommand.
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 Symfony\Component\Console\Command\Command;
14 use Drupal\Core\Entity\EntityTypeRepository;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Generator\PluginConditionGenerator;
17 use Drupal\Console\Command\Shared\ModuleTrait;
18 use Drupal\Console\Command\Shared\ConfirmationTrait;
19 use Drupal\Console\Core\Style\DrupalStyle;
20 use Drupal\Console\Extension\Manager;
21 use Drupal\Console\Core\Utils\ChainQueue;
22 use Drupal\Console\Core\Utils\StringConverter;
23
24 /**
25  * Class PluginConditionCommand
26  *
27  * @package Drupal\Console\Command\Generate
28  */
29 class PluginConditionCommand extends Command
30 {
31     use CommandTrait;
32     use ModuleTrait;
33     use ConfirmationTrait;
34
35     /**
36  * @var Manager
37 */
38     protected $extensionManager;
39
40     /**
41  * @var PluginConditionGenerator
42 */
43     protected $generator;
44
45     /**
46      * @var ChainQueue
47      */
48     protected $chainQueue;
49
50     /**
51      * @var StringConverter
52      */
53     protected $stringConverter;
54
55
56     /**
57      * PluginConditionCommand constructor.
58      *
59      * @param Manager                  $extensionManager
60      * @param PluginConditionGenerator $generator
61      * @param ChainQueue               $chainQueue
62      * @param EntityTypeRepository     $entitytyperepository
63      * @param StringConverter          $stringConverter
64      */
65     public function __construct(
66         Manager $extensionManager,
67         PluginConditionGenerator $generator,
68         ChainQueue $chainQueue,
69         EntityTypeRepository $entitytyperepository,
70         StringConverter $stringConverter
71     ) {
72         $this->extensionManager = $extensionManager;
73         $this->generator = $generator;
74         $this->chainQueue = $chainQueue;
75         $this->entitytyperepository = $entitytyperepository;
76         $this->stringConverter = $stringConverter;
77         parent::__construct();
78     }
79
80     protected function configure()
81     {
82         $this
83             ->setName('generate:plugin:condition')
84             ->setDescription($this->trans('commands.generate.plugin.condition.description'))
85             ->setHelp($this->trans('commands.generate.plugin.condition.help'))
86             ->addOption('module', null, InputOption::VALUE_REQUIRED, $this->trans('commands.common.options.module'))
87             ->addOption(
88                 'class',
89                 null,
90                 InputOption::VALUE_REQUIRED,
91                 $this->trans('commands.generate.plugin.condition.options.class')
92             )
93             ->addOption(
94                 'label',
95                 null,
96                 InputOption::VALUE_REQUIRED,
97                 $this->trans('commands.generate.plugin.condition.options.label')
98             )
99             ->addOption(
100                 'plugin-id',
101                 null,
102                 InputOption::VALUE_REQUIRED,
103                 $this->trans('commands.generate.plugin.condition.options.plugin-id')
104             )
105             ->addOption(
106                 'context-definition-id',
107                 null,
108                 InputOption::VALUE_REQUIRED,
109                 $this->trans('commands.generate.plugin.condition.options.context-definition-id')
110             )
111             ->addOption(
112                 'context-definition-label',
113                 null,
114                 InputOption::VALUE_REQUIRED,
115                 $this->trans('commands.generate.plugin.condition.options.context-definition-label')
116             )
117             ->addOption(
118                 'context-definition-required',
119                 null,
120                 InputOption::VALUE_OPTIONAL,
121                 $this->trans('commands.generate.plugin.condition.options.context-definition-required')
122             );
123     }
124
125     /**
126      * {@inheritdoc}
127      */
128     protected function execute(InputInterface $input, OutputInterface $output)
129     {
130         $io = new DrupalStyle($input, $output);
131
132         // @see use Drupal\Console\Command\Shared\ConfirmationTrait::confirmGeneration
133         if (!$this->confirmGeneration($io)) {
134             return 1;
135         }
136
137         $module = $input->getOption('module');
138         $class_name = $input->getOption('class');
139         $label = $input->getOption('label');
140         $plugin_id = $input->getOption('plugin-id');
141         $context_definition_id = $input->getOption('context-definition-id');
142         $context_definition_label = $input->getOption('context-definition-label');
143         $context_definition_required = $input->getOption('context-definition-required')?'TRUE':'FALSE';
144
145         $this
146             ->generator
147             ->generate($module, $class_name, $label, $plugin_id, $context_definition_id, $context_definition_label, $context_definition_required);
148
149         $this->chainQueue->addCommand('cache:rebuild', ['cache' => 'discovery']);
150
151         return 0;
152     }
153
154     protected function interact(InputInterface $input, OutputInterface $output)
155     {
156         $io = new DrupalStyle($input, $output);
157
158         $entityTypeRepository = $this->entitytyperepository;
159
160         $entity_types = $entityTypeRepository->getEntityTypeLabels(true);
161
162         // --module option
163         $module = $input->getOption('module');
164         if (!$module) {
165             // @see Drupal\Console\Command\Shared\ModuleTrait::moduleQuestion
166             $module = $this->moduleQuestion($io);
167         }
168         $input->setOption('module', $module);
169
170         // --class option
171         $class = $input->getOption('class');
172         if (!$class) {
173             $class = $io->ask(
174                 $this->trans('commands.generate.plugin.condition.questions.class'),
175                 'ExampleCondition'
176             );
177             $input->setOption('class', $class);
178         }
179
180         // --plugin label option
181         $label = $input->getOption('label');
182         if (!$label) {
183             $label = $io->ask(
184                 $this->trans('commands.generate.plugin.condition.questions.label'),
185                 $this->stringConverter->camelCaseToHuman($class)
186             );
187             $input->setOption('label', $label);
188         }
189
190         // --plugin-id option
191         $pluginId = $input->getOption('plugin-id');
192         if (!$pluginId) {
193             $pluginId = $io->ask(
194                 $this->trans('commands.generate.plugin.condition.questions.plugin-id'),
195                 $this->stringConverter->camelCaseToUnderscore($class)
196             );
197             $input->setOption('plugin-id', $pluginId);
198         }
199
200         $context_definition_id = $input->getOption('context-definition-id');
201         if (!$context_definition_id) {
202             $context_type = ['language' => 'Language', "entity" => "Entity"];
203             $context_type_sel = $io->choice(
204                 $this->trans('commands.generate.plugin.condition.questions.context-type'),
205                 array_values($context_type)
206             );
207             $context_type_sel = array_search($context_type_sel, $context_type);
208
209             if ($context_type_sel == 'language') {
210                 $context_definition_id = $context_type_sel;
211                 $context_definition_id_value = ucfirst($context_type_sel);
212             } else {
213                 $content_entity_types_sel = $io->choice(
214                     $this->trans('commands.generate.plugin.condition.questions.context-entity-type'),
215                     array_keys($entity_types)
216                 );
217
218                 $contextDefinitionIdList = $entity_types[$content_entity_types_sel];
219                 $context_definition_id_sel = $io->choice(
220                     $this->trans('commands.generate.plugin.condition.questions.context-definition-id'),
221                     array_values($contextDefinitionIdList)
222                 );
223
224                 $context_definition_id_value = array_search(
225                     $context_definition_id_sel,
226                     $contextDefinitionIdList
227                 );
228
229                 $context_definition_id = 'entity:' . $context_definition_id_value;
230             }
231             $input->setOption('context-definition-id', $context_definition_id);
232         }
233
234         $context_definition_label = $input->getOption('context-definition-label');
235         if (!$context_definition_label) {
236             $context_definition_label = $io->ask(
237                 $this->trans('commands.generate.plugin.condition.questions.context-definition-label'),
238                 $context_definition_id_value?:null
239             );
240             $input->setOption('context-definition-label', $context_definition_label);
241         }
242
243         $context_definition_required = $input->getOption('context-definition-required');
244         if (empty($context_definition_required)) {
245             $context_definition_required = $io->confirm(
246                 $this->trans('commands.generate.plugin.condition.questions.context-definition-required'),
247                 true
248             );
249             $input->setOption('context-definition-required', $context_definition_required);
250         }
251     }
252 }