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